Skip to content

Commit

Permalink
smart poster implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jari Kalinainen committed Oct 10, 2017
1 parent f57ebe2 commit 5a46540
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 18 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.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.
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
Smart Poster - NFCForum-SmartPoster_RTD_1.0 2006-07-24 (with some limitations)
DESC

s.homepage = 'https://github.com/jvk75/NFCNDEFParse'
Expand Down
6 changes: 5 additions & 1 deletion NFCNDEFParse/NDEFMessageWithWellKnownTypeUri.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
5 changes: 5 additions & 0 deletions NFCNDEFParse/NDEFMessageWithWellKnownTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 10 additions & 0 deletions NFCNDEFParse/NFCForumWellKnownTypeProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}
}

45 changes: 41 additions & 4 deletions NFCNDEFParse/NFCForumWellKnownTypeSmartPoster.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
}
6 changes: 5 additions & 1 deletion NFCNDEFParse/NFCForumWellKnownTypeText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 6 additions & 10 deletions 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 (TBA)
Smart Poster - NFCForum-SmartPoster_RTD_1.0 2006-07-24 (with some limitations)

## Requirements

Expand Down Expand Up @@ -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)
})
})
```
Expand Down

0 comments on commit 5a46540

Please sign in to comment.