Skip to content

Commit

Permalink
obj-c support
Browse files Browse the repository at this point in the history
  • Loading branch information
jvk75 authored Oct 14, 2017
1 parent 6cc834c commit 6979106
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 64 deletions.
2 changes: 1 addition & 1 deletion 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.1'
s.version = '0.3.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 Down
16 changes: 10 additions & 6 deletions NFCNDEFParse/NDEFMessageWithWellKnownTypeUri.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,23 @@ import CoreNFC
/// - not nil if payload can be created as URL type
/// - **string** : String (
/// - string representation of the payload
public class NFCForumWellKnownTypeUri: NFCForumWellKnownTypeUriProtocol {
@objc public class NFCForumWellKnownTypeUri: NSObject, NFCForumWellKnownTypeUriProtocol {

public var type: NFCForumWellKnownType = .uri
@objc public var type: NFCForumWellKnownTypeEnum = .uri

public var url: URL?
public var string: String?
@objc public var url: URL?
@objc public var string: String?

public var description: String {
return "- \(self.type): \n\tstring: \(string ?? "%EMPTY%") \n\turl: \(url?.absoluteString ?? "%EMPTY%")"
@objc public var recordDescription: String {
return "- \(self.type.description): \n\tstring: \(string ?? "nil") \n\turl: \(url?.absoluteString ?? "nil")"
}

public init?(payload: Data) {
super.init()
let bytes = [UInt8](payload)
guard bytes.count > 2 else {
return
}
let uriIdentifierByte = bytes[0]
let textBytes = bytes[1...]
if let textString = String(bytes: textBytes, encoding: .utf8) {
Expand Down
61 changes: 48 additions & 13 deletions NFCNDEFParse/NDEFMessageWithWellKnownTypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,73 @@ import CoreNFC
/// - text
/// - uri
/// - smart poster (title, uri, action, size)
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
@objc public enum NFCForumWellKnownTypeEnum: Int {
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) {
if let string = String(data: data, encoding: .utf8) {
self = NFCForumWellKnownType(rawValue: string) ?? .unknown
switch string {
case "T":
self = .text
case "U":
self = .uri
case "Sp":
self = .smartPoster
case "act":
self = .action
case "s":
self = .size
case "t":
self = .type
default:
self = .unknown
}
} else {
self = .unknown
}
}

var description: String {
switch self {
case .text:
return "Text"
case .uri:
return "Uri"
case .smartPoster:
return "Smart Poster"
case .action:
return "Action"
case .size:
return "Size"
case .type:
return "Type"
default:
return "Unknown"
}
}
}

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

public var records: [NFCForumWellKnownTypeProtocol] = []
public var types: [NFCTypeNameFormat] = []
@objc public var records: [NFCForumWellKnownTypeProtocol] = []
@objc public var types: [UInt8] = []

public init?(records: [NFCNDEFPayload]) {
super.init()
self.records = records.flatMap({ record in
self.types.append(record.typeNameFormat)
let type = NFCForumWellKnownType(data: record.type)
self.types.append(record.typeNameFormat.rawValue)
let type = NFCForumWellKnownTypeEnum(data: record.type)
switch type {
case .text:
return NFCForumWellKnownTypeText(payload: record.payload)
Expand Down
23 changes: 13 additions & 10 deletions NFCNDEFParse/NFCForumWellKnownTypeProtocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,47 @@
import Foundation
import CoreNFC

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

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

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


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

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

/// - **action**: NFCSmartPosterActionRecord
/// - **action**: NFCSmartPosterActionRecord / **actionByte**: UInt8 (for ObjC)
/// - **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}
}
@objc public protocol NFCForumWellKnownTypeActionProtocolObjC: NFCForumWellKnownTypeProtocol {
var actionByte: UInt8 {get}
}

51 changes: 28 additions & 23 deletions NFCNDEFParse/NFCForumWellKnownTypeSmartPoster.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,31 @@ import CoreNFC
/// (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: NFCForumWellKnownTypeSmartPosterProtocol {
public class NFCForumWellKnownTypeSmartPoster: NSObject, NFCForumWellKnownTypeSmartPosterProtocol {

public var type: NFCForumWellKnownType = .smartPoster
public var type: NFCForumWellKnownTypeEnum = .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"
public var recordDescription: String {
let recordDescriptions = records.map({ $0.recordDescription }).joined(separator: "\n")
return "- \(self.type.description): {\n\(recordDescriptions)\n}\n"
}

private var payloadFirstByte: Int = 0

public init?(payload: Data) {
super.init()
let allBytes = [UInt8](payload)
guard allBytes.count > 3 else {
return
}
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 type = NFCForumWellKnownTypeEnum(data: Data(bytes: bytes[3...(2+typeLength)]))

let payloadLength: Int
switch type {
Expand All @@ -53,9 +57,9 @@ public class NFCForumWellKnownTypeSmartPoster: NFCForumWellKnownTypeSmartPosterP
case .uri:
newRecords.append(NFCForumWellKnownTypeUri(payload: payload))
case .size:
newRecords.append(NFCForumWellKnownTypeSize(payload: payload))
newRecords.append(NFCForumWellKnownSubTypeSize(payload: payload))
case .action:
newRecords.append(NFCForumWellKnownTypeAction(payload: payload))
newRecords.append(NFCForumWellKnownSubTypeAction(payload: payload))
default:
continue
}
Expand All @@ -66,7 +70,7 @@ public class NFCForumWellKnownTypeSmartPoster: NFCForumWellKnownTypeSmartPosterP
}

// MARK: - Smart Poster Action Record Enum
public enum NFCSmartPosterActionRecord: UInt8 {
@objc public enum NFCSmartPosterActionRecord: UInt8 {
case execute = 0x00
case save = 0x01
case open = 0x02
Expand All @@ -90,18 +94,21 @@ public enum NFCSmartPosterActionRecord: UInt8 {
/// - **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 {
@objc public class NFCForumWellKnownSubTypeAction: NSObject, NFCForumWellKnownTypeActionProtocol, NFCForumWellKnownTypeActionProtocolObjC {

public var type: NFCForumWellKnownType = .action
@objc public var type: NFCForumWellKnownTypeEnum = .action

public var description: String {
return self.action?.description ?? "nil"
@objc public var recordDescription: String {
return "-\(type.description)\naction: " + (self.action?.description ?? "nil")
}

public var action: NFCSmartPosterActionRecord?

@objc public var actionByte: UInt8 = 0xFF

public init?(payload: Data) {
super.init()
let bytes = [UInt8](payload)
self.actionByte = bytes[0]
if let byte = bytes.first {
self.action = NFCSmartPosterActionRecord(rawValue: byte)
}
Expand All @@ -113,20 +120,18 @@ public class NFCForumWellKnownTypeAction: NFCForumWellKnownTypeProtocol {
/// Smart Poster Size Record
/// - **size** : Int
/// - value of the size payload
public class NFCForumWellKnownTypeSize: NFCForumWellKnownTypeProtocol {
public class NFCForumWellKnownSubTypeSize: NSObject, NFCForumWellKnownTypeSizeProtocol {

public var type: NFCForumWellKnownType = .action
public var type: NFCForumWellKnownTypeEnum = .action

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

public var size: Int?
public var size: Int = 0

public init?(payload: Data) {
super.init()
let bytes = [UInt8](payload)
if let byte = bytes.first {
self.size = Int(byte)
Expand Down
16 changes: 10 additions & 6 deletions NFCNDEFParse/NFCForumWellKnownTypeText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,26 @@ import CoreNFC
/// - string representation of the payload
/// - **locale** : String
/// - language code of the payload string
public class NFCForumWellKnownTypeText: NFCForumWellKnownTypeTextProtocol {
@objc public class NFCForumWellKnownTypeText: NSObject, NFCForumWellKnownTypeTextProtocol {

public var type: NFCForumWellKnownType = .text
@objc public var type: NFCForumWellKnownTypeEnum = .text

public var string: String?
public var locale: String?
@objc public var string: String?
@objc public var locale: String?

public var description: String {
return "- \(self.type): \n\tstring: \(string ?? "%EMPTY%") \n\tlocale: \(locale ?? "%EMPTY%")"
@objc public var recordDescription: String {
return "- \(self.type.description): \n\tstring: \(string ?? "nil") \n\tlocale: \(locale ?? "nil")"
}

private var localeLength: Int = 0
private var encoding: String.Encoding = .utf8

public init?(payload: Data) {
super.init()
let bytes = [UInt8](payload)
guard bytes.count > 1 else {
return
}
let statusByte = bytes[0]
parseStatusByte(byte: statusByte)
let localeBytes = bytes[1...localeLength]
Expand Down
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Smart Poster - NFCForum-SmartPoster_RTD_1.0 2006-07-24 (title, uri, action, size

## Requirements

Core NFC requires iOS11
Core NFC requires iOS11 (and Xcode 9)

## Installation

Expand All @@ -25,27 +25,35 @@ pod 'NFCNDEFParse'

## Usage

Import the library.
### Import the library.

Swift:

```
import NFCNDEFParse
```

Create array for the messages
Objective-C:

```
@import NFCNDEFParse;
```

### Create array for the messages

```
var data: [NDEFMessageWithWellKnownTypes] = []
```

In CoreNFC callback create the "well know types" data array.
### In CoreNFC callback create the "well know types" data array.

```
func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
data = messages.flatMap({ NDEFMessageWithWellKnownTypes(records: $0.records) })
}
```

Loop through the data array to print out the values.
### Loop through the data array to print out the values.

```
data.forEach({ message in
Expand Down

0 comments on commit 6979106

Please sign in to comment.