From 6cc834c21b9bc44adb0c3e2f6a44c9ef425f61db Mon Sep 17 00:00:00 2001 From: Jari Kalinainen Date: Wed, 11 Oct 2017 23:36:36 +0300 Subject: [PATCH] sp action and size --- NFCNDEFParse.podspec | 4 +- .../NDEFMessageWithWellKnownTypeUri.swift | 4 +- .../NDEFMessageWithWellKnownTypes.swift | 6 +- .../NFCForumWellKnownTypeProtocols.swift | 27 ++++-- .../NFCForumWellKnownTypeSmartPoster.swift | 89 +++++++++++++++++-- NFCNDEFParse/NFCForumWellKnownTypeText.swift | 4 +- README.md | 2 +- 7 files changed, 112 insertions(+), 24 deletions(-) diff --git a/NFCNDEFParse.podspec b/NFCNDEFParse.podspec index 28fc4f4..01b42fc 100644 --- a/NFCNDEFParse.podspec +++ b/NFCNDEFParse.podspec @@ -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. @@ -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' diff --git a/NFCNDEFParse/NDEFMessageWithWellKnownTypeUri.swift b/NFCNDEFParse/NDEFMessageWithWellKnownTypeUri.swift index 310ecf7..e66f58b 100644 --- a/NFCNDEFParse/NDEFMessageWithWellKnownTypeUri.swift +++ b/NFCNDEFParse/NDEFMessageWithWellKnownTypeUri.swift @@ -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 { diff --git a/NFCNDEFParse/NDEFMessageWithWellKnownTypes.swift b/NFCNDEFParse/NDEFMessageWithWellKnownTypes.swift index 4db6618..b10b441 100644 --- a/NFCNDEFParse/NDEFMessageWithWellKnownTypes.swift +++ b/NFCNDEFParse/NDEFMessageWithWellKnownTypes.swift @@ -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" @@ -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 { diff --git a/NFCNDEFParse/NFCForumWellKnownTypeProtocols.swift b/NFCNDEFParse/NFCForumWellKnownTypeProtocols.swift index 9ee48a5..1cee372 100644 --- a/NFCNDEFParse/NFCForumWellKnownTypeProtocols.swift +++ b/NFCNDEFParse/NFCForumWellKnownTypeProtocols.swift @@ -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} +} + diff --git a/NFCNDEFParse/NFCForumWellKnownTypeSmartPoster.swift b/NFCNDEFParse/NFCForumWellKnownTypeSmartPoster.swift index aff4dd1..4b9cdcb 100644 --- a/NFCNDEFParse/NFCForumWellKnownTypeSmartPoster.swift +++ b/NFCNDEFParse/NFCForumWellKnownTypeSmartPoster.swift @@ -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 { @@ -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) + } + } +} diff --git a/NFCNDEFParse/NFCForumWellKnownTypeText.swift b/NFCNDEFParse/NFCForumWellKnownTypeText.swift index c951ea1..b0752bc 100644 --- a/NFCNDEFParse/NFCForumWellKnownTypeText.swift +++ b/NFCNDEFParse/NFCForumWellKnownTypeText.swift @@ -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 { diff --git a/README.md b/README.md index 7902906..40c7963 100644 --- a/README.md +++ b/README.md @@ -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