Skip to content

Commit

Permalink
sp action and size
Browse files Browse the repository at this point in the history
  • Loading branch information
jvk75 committed Oct 11, 2017
1 parent 424e598 commit 6cc834c
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 24 deletions.
4 changes: 2 additions & 2 deletions NFCNDEFParse.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'NFCNDEFParse'
s.version = '0.2.0'
s.version = '0.2.1'
s.summary = 'NFC Forum Well Known Type Data Parser for iOS11 and Core NFC'

# This description is used to generate tags and improve search results.
Expand All @@ -22,7 +22,7 @@ NFC Forum Well Known Type Data Parser for iOS11 and Core NFC.
Supports parsing of types:
Text - NFCForum-TS-RTD_Text_1.0 2006-07-24
Uri - NFCForum-TS-RTD_URI_1.0 2006-07-24
Smart Poster - NFCForum-SmartPoster_RTD_1.0 2006-07-24 (with some limitations)
Smart Poster - NFCForum-SmartPoster_RTD_1.0 2006-07-24 (title, uri, action, size)
DESC

s.homepage = 'https://github.com/jvk75/NFCNDEFParse'
Expand Down
4 changes: 2 additions & 2 deletions NFCNDEFParse/NDEFMessageWithWellKnownTypeUri.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import Foundation
import CoreNFC

/// NFCForum-TS-RTD_URI_1.0 2006-07-24
/// - url : URL
/// - **url** : URL
/// - not nil if payload can be created as URL type
/// - string : String (
/// - **string** : String (
/// - string representation of the payload
public class NFCForumWellKnownTypeUri: NFCForumWellKnownTypeUriProtocol {

Expand Down
6 changes: 3 additions & 3 deletions NFCNDEFParse/NDEFMessageWithWellKnownTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import CoreNFC
/// Currently supported types
/// - text
/// - uri
/// - smart poster (title, uri)
/// - smart poster (title, uri, action, size)
public enum NFCForumWellKnownType: String {
case text = "T"
case uri = "U"
Expand All @@ -32,9 +32,9 @@ public enum NFCForumWellKnownType: String {
}

/// Class that contains records of NFC Forum Well Known Types
/// - records : [NFCForumWellKnownTypeProtocol]
/// - **records** : [NFCForumWellKnownTypeProtocol]
/// - collection of the NFCForumWellKnownTypes
/// - types : [NFCForumWellKnownTypeProtocol]
/// - **types** : [NFCTypeNameFormat]
/// - collection of the record types
public class NDEFMessageWithWellKnownTypes {

Expand Down
27 changes: 20 additions & 7 deletions NFCNDEFParse/NFCForumWellKnownTypeProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,44 @@
import Foundation
import CoreNFC

/// - type : NFCForumWellKnownType
/// - description : String
/// - **type** : NFCForumWellKnownType
/// - **description** : String
/// - record description (values)
public protocol NFCForumWellKnownTypeProtocol {
var type: NFCForumWellKnownType {get}
var description: String {get}
}

/// - string : String
/// - locale : String
/// - **string** : String
/// - **locale** : String
public protocol NFCForumWellKnownTypeTextProtocol: NFCForumWellKnownTypeProtocol {
var string: String? {get}
var locale: String? {get}
}

/// - url : URL
/// - locale : String
/// - **url** : URL
/// - **locale** : String
public protocol NFCForumWellKnownTypeUriProtocol: NFCForumWellKnownTypeProtocol {
var url: URL? {get}
var string: String? {get}
}


/// - records: [NFCForumWellKnownTypeProtocol]
/// - **records**: [NFCForumWellKnownTypeProtocol]
public protocol NFCForumWellKnownTypeSmartPosterProtocol: NFCForumWellKnownTypeProtocol {
var records: [NFCForumWellKnownTypeProtocol] {get}
}

/// - **size**: Int
public protocol NFCForumWellKnownTypeSizeProtocol: NFCForumWellKnownTypeProtocol {
var size: Int? {get}
}

/// - **action**: NFCSmartPosterActionRecord
/// - **execute** : Do the action (send the SMS, launch the browser, make the telephone call)
/// - **save** : Save for later (store the SMS in INBOX, put the URI in a bookmark, save the telephone number in contacts)
/// - **open** : Open for editing (open an SMS in the SMS editor, open the URI in an URI editor, open the telephone number for editing).
public protocol NFCForumWellKnownTypeActionProtocol: NFCForumWellKnownTypeProtocol {
var action: NFCSmartPosterActionRecord? {get}
}

89 changes: 82 additions & 7 deletions NFCNDEFParse/NFCForumWellKnownTypeSmartPoster.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import CoreNFC

/// NFCForum-SmartPoster_RTD_1.0 2006-07-24
/// (only URI and Text records supported, Action and Size records are ignored, Type and Icon records may cause problems)
/// - records : [NFCForumWellKnownTypeProtocol]
/// - **records** : [NFCForumWellKnownTypeProtocol]
/// - collection of the NFCForumWellKnownTypes
public class NFCForumWellKnownTypeSmartPoster: NFCForumWellKnownTypeSmartPosterProtocol {

Expand Down Expand Up @@ -46,15 +46,90 @@ public class NFCForumWellKnownTypeSmartPoster: NFCForumWellKnownTypeSmartPosterP

let payloadLastByte = (2+typeLength) + payloadLength

let payload = bytes[(2+typeLength + 1)...payloadLastByte]
if type == .uri {
newRecords.append(NFCForumWellKnownTypeUri(payload: Data(bytes: payload)))
}
if type == .text {
newRecords.append(NFCForumWellKnownTypeText(payload: Data(bytes: payload)))
let payload = Data(bytes: bytes[(2+typeLength + 1)...payloadLastByte])
switch type {
case .text:
newRecords.append(NFCForumWellKnownTypeText(payload: payload))
case .uri:
newRecords.append(NFCForumWellKnownTypeUri(payload: payload))
case .size:
newRecords.append(NFCForumWellKnownTypeSize(payload: payload))
case .action:
newRecords.append(NFCForumWellKnownTypeAction(payload: payload))
default:
continue
}
payloadFirstByte += payloadLastByte + 1
}
self.records = newRecords.flatMap({ $0 })
}
}

// MARK: - Smart Poster Action Record Enum
public enum NFCSmartPosterActionRecord: UInt8 {
case execute = 0x00
case save = 0x01
case open = 0x02

var description: String {
switch self {
case .execute:
return "do the action"
case .save:
return "save for later"
case .open:
return "open for editing"
}
}
}

// MARK: - Smart Poster Action Record
/// NFCForum-SmartPoster_RTD_1.0 2006-07-24
/// Smart Poster Action Record
/// - **action** : NFCSmartPosterActionRecord
/// - **execute** : Do the action (send the SMS, launch the browser, make the telephone call)
/// - **save** : Save for later (store the SMS in INBOX, put the URI in a bookmark, save the telephone number in contacts)
/// - **open** : Open for editing (open an SMS in the SMS editor, open the URI in an URI editor, open the telephone number for editing).
public class NFCForumWellKnownTypeAction: NFCForumWellKnownTypeProtocol {

public var type: NFCForumWellKnownType = .action

public var description: String {
return self.action?.description ?? "nil"
}

public var action: NFCSmartPosterActionRecord?

public init?(payload: Data) {
let bytes = [UInt8](payload)
if let byte = bytes.first {
self.action = NFCSmartPosterActionRecord(rawValue: byte)
}
}
}

// MARK: - Smart Poster Size Record
/// NFCForum-SmartPoster_RTD_1.0 2006-07-24
/// Smart Poster Size Record
/// - **size** : Int
/// - value of the size payload
public class NFCForumWellKnownTypeSize: NFCForumWellKnownTypeProtocol {

public var type: NFCForumWellKnownType = .action

public var description: String {
if let size = self.size {
return "\(size)"
}
return "nil"
}

public var size: Int?

public init?(payload: Data) {
let bytes = [UInt8](payload)
if let byte = bytes.first {
self.size = Int(byte)
}
}
}
4 changes: 2 additions & 2 deletions NFCNDEFParse/NFCForumWellKnownTypeText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import Foundation
import CoreNFC

/// NFCForum-TS-RTD_Text_1.0 2006-07-24
/// - string : String
/// - **string** : String
/// - string representation of the payload
/// - locale : String
/// - **locale** : String
/// - language code of the payload string
public class NFCForumWellKnownTypeText: NFCForumWellKnownTypeTextProtocol {

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Text - NFCForum-TS-RTD_Text_1.0 2006-07-24

Uri - NFCForum-TS-RTD_URI_1.0 2006-07-24

Smart Poster - NFCForum-SmartPoster_RTD_1.0 2006-07-24 (with some limitations)
Smart Poster - NFCForum-SmartPoster_RTD_1.0 2006-07-24 (title, uri, action, size)

## Requirements

Expand Down

0 comments on commit 6cc834c

Please sign in to comment.