From 5a46540807e5bb1bda5497ecc3072804df1be6ad Mon Sep 17 00:00:00 2001 From: Jari Kalinainen Date: Tue, 10 Oct 2017 16:06:06 +0300 Subject: [PATCH] smart poster implementation --- NFCNDEFParse.podspec | 4 +- .../NDEFMessageWithWellKnownTypeUri.swift | 6 ++- .../NDEFMessageWithWellKnownTypes.swift | 5 +++ .../NFCForumWellKnownTypeProtocols.swift | 10 +++++ .../NFCForumWellKnownTypeSmartPoster.swift | 45 +++++++++++++++++-- NFCNDEFParse/NFCForumWellKnownTypeText.swift | 6 ++- README.md | 16 +++---- 7 files changed, 74 insertions(+), 18 deletions(-) diff --git a/NFCNDEFParse.podspec b/NFCNDEFParse.podspec index ad20dca..28fc4f4 100644 --- a/NFCNDEFParse.podspec +++ b/NFCNDEFParse.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'NFCNDEFParse' - s.version = '0.1.2' + s.version = '0.2.0' 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 +Smart Poster - NFCForum-SmartPoster_RTD_1.0 2006-07-24 (with some limitations) DESC s.homepage = 'https://github.com/jvk75/NFCNDEFParse' diff --git a/NFCNDEFParse/NDEFMessageWithWellKnownTypeUri.swift b/NFCNDEFParse/NDEFMessageWithWellKnownTypeUri.swift index 90ccd03..310ecf7 100644 --- a/NFCNDEFParse/NDEFMessageWithWellKnownTypeUri.swift +++ b/NFCNDEFParse/NDEFMessageWithWellKnownTypeUri.swift @@ -19,7 +19,11 @@ public class NFCForumWellKnownTypeUri: NFCForumWellKnownTypeUriProtocol { public var url: URL? public var string: String? - + + public var description: String { + return "- \(self.type): \n\tstring: \(string ?? "%EMPTY%") \n\turl: \(url?.absoluteString ?? "%EMPTY%")" + } + public init?(payload: Data) { let bytes = [UInt8](payload) let uriIdentifierByte = bytes[0] diff --git a/NFCNDEFParse/NDEFMessageWithWellKnownTypes.swift b/NFCNDEFParse/NDEFMessageWithWellKnownTypes.swift index 42fa6ab..39dc01d 100644 --- a/NFCNDEFParse/NDEFMessageWithWellKnownTypes.swift +++ b/NFCNDEFParse/NDEFMessageWithWellKnownTypes.swift @@ -16,6 +16,9 @@ public enum NFCForumWellKnownType: String { case text = "T" case uri = "U" case smartPoster = "Sp" + case action = "act" // smart poster sub type + case size = "s" // smart poster sub type + case type = "t" // smart poster sub type case unknown init(data: Data) { @@ -44,6 +47,8 @@ public class NDEFMessageWithWellKnownTypes { return NFCForumWellKnownTypeText(payload: record.payload) case .uri: return NFCForumWellKnownTypeUri(payload: record.payload) + case .smartPoster: + return NFCForumWellKnownTypeSmartPoster(payload: record.payload) default: return nil } diff --git a/NFCNDEFParse/NFCForumWellKnownTypeProtocols.swift b/NFCNDEFParse/NFCForumWellKnownTypeProtocols.swift index 3ad463a..9ee48a5 100644 --- a/NFCNDEFParse/NFCForumWellKnownTypeProtocols.swift +++ b/NFCNDEFParse/NFCForumWellKnownTypeProtocols.swift @@ -9,8 +9,11 @@ import Foundation import CoreNFC /// - type : NFCForumWellKnownType +/// - description : String +/// - record description (values) public protocol NFCForumWellKnownTypeProtocol { var type: NFCForumWellKnownType {get} + var description: String {get} } /// - string : String @@ -26,3 +29,10 @@ public protocol NFCForumWellKnownTypeUriProtocol: NFCForumWellKnownTypeProtocol var url: URL? {get} var string: String? {get} } + + +/// - records: [NFCForumWellKnownTypeProtocol] +public protocol NFCForumWellKnownTypeSmartPosterProtocol: NFCForumWellKnownTypeProtocol { + var records: [NFCForumWellKnownTypeProtocol] {get} +} + diff --git a/NFCNDEFParse/NFCForumWellKnownTypeSmartPoster.swift b/NFCNDEFParse/NFCForumWellKnownTypeSmartPoster.swift index 07263a4..aff4dd1 100644 --- a/NFCNDEFParse/NFCForumWellKnownTypeSmartPoster.swift +++ b/NFCNDEFParse/NFCForumWellKnownTypeSmartPoster.swift @@ -9,15 +9,52 @@ import Foundation 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] /// - collection of the NFCForumWellKnownTypes -public class NFCForumWellKnownTypeSmartPoster: NFCForumWellKnownTypeProtocol { +public class NFCForumWellKnownTypeSmartPoster: NFCForumWellKnownTypeSmartPosterProtocol { public var type: NFCForumWellKnownType = .smartPoster - public var records: [NFCForumWellKnownTypeProtocol] = [] - + public var records: [NFCForumWellKnownTypeProtocol] + + public var description: String { + let recordDescriptions = records.map({ $0.description }).joined(separator: "\n") + return "- \(self.type): {\n\(recordDescriptions)\n}\n" + } + + private var payloadFirstByte: Int = 0 + public init?(payload: Data) { - //TBA + let allBytes = [UInt8](payload) + var newRecords: [NFCForumWellKnownTypeProtocol?] = [] + while payloadFirstByte < allBytes.count { + let bytes = Array(allBytes[payloadFirstByte...]) + + let typeLength = Int(bytes[1]) + let type = NFCForumWellKnownType(data: Data(bytes: bytes[3...(2+typeLength)])) + + let payloadLength: Int + switch type { + case .action: + payloadLength = 1 + case .size: + payloadLength = 4 + default: + payloadLength = Int(bytes[2]) + } + + 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))) + } + payloadFirstByte += payloadLastByte + 1 + } + self.records = newRecords.flatMap({ $0 }) } } diff --git a/NFCNDEFParse/NFCForumWellKnownTypeText.swift b/NFCNDEFParse/NFCForumWellKnownTypeText.swift index 0a0b4b9..c951ea1 100644 --- a/NFCNDEFParse/NFCForumWellKnownTypeText.swift +++ b/NFCNDEFParse/NFCForumWellKnownTypeText.swift @@ -19,7 +19,11 @@ public class NFCForumWellKnownTypeText: NFCForumWellKnownTypeTextProtocol { public var string: String? public var locale: String? - + + public var description: String { + return "- \(self.type): \n\tstring: \(string ?? "%EMPTY%") \n\tlocale: \(locale ?? "%EMPTY%")" + } + private var localeLength: Int = 0 private var encoding: String.Encoding = .utf8 diff --git a/README.md b/README.md index 2aea04b..7902906 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 (TBA) +Smart Poster - NFCForum-SmartPoster_RTD_1.0 2006-07-24 (with some limitations) ## Requirements @@ -48,15 +48,11 @@ func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NF Loop through the data array to print out the values. ``` -data.records.forEach({ record in - if record.type == .text, let record = record as? NFCForumWellKnownTypeTextProtocol { - print(record.string) - print(record.locale) - } - if record.type == .uri, let record = record as? NFCForumWellKnownTypeUriProtocol { - print(record.string) - print(record.url) - } +data.forEach({ message in + print("message: ") + message.records.forEach({ record in + print(record.description) + }) }) ```