遲來的《Core NFC》

1、介紹

NFC技術是由非接觸式射頻識別(RFID)演變而來,由飛利浦半導體(現恩智浦半導體公司)、諾基亞和索尼共同研製開發,其基礎是RFID及互連技術。近場通訊(Near Field Communication,NFC)是一種短距高頻的無線電技術,在13.56MHz頻率運行於10釐米距離內。其傳輸速度有106 Kbit/秒、212 Kbit/秒或者424 Kbit/秒三種。api

從iPhone的歷代機型配置中,能夠看出在iPhone6及以上機型都擁有了支持NFC功能的固件,可是一直不開放給開發者使用,直到iOS11纔開放僅有的具備讀取NDEF格式的功能,而且須要iPhone7或者iPhone7 Plus才支持使用。數組

官方介紹,NFC有五種的類型,如圖安全

2、API詳解

1. 會話

1.1 NFCReaderSession

//是否已經啓動並準備使用
@property(nonatomic, getter=isReady, readonly) BOOL ready;

//開始會話
- (void)beginSession;

//關閉會話
- (void)invalidateSession;

//告知用戶在程序中使用NFC的說明,當掃描時該信息將顯示給用戶,該屬性與NFCReaderUsageDescription鍵中的不同
@property(nonatomic, copy) NSString *alertMessage;

1.2 NFCNDEFReaderSession

從類的名稱能夠看出,這個類是用來讀取NDEF格式數據的會話的。繼承於NFCReaderSession
根據文檔顯示,一次只能在系統中激活一個NFC NDEF會話,若是建立多個的話,系統會將其放在隊列中,按照順序執行session

/* 初始化會話
 * delegate 會話的回調
 * queue 線程
 * invalidateAfterFirstRead 第一NDEF讀取完畢後是否中止會話
 *
 * 當會話爲讀取多個NDEF時,每次讀取成功都會經過代理回調
 * 讀取多個NDEF時,會話會一直到調用終止活動API或者超時等纔會中止會話
 *
 * 若是第一個NDEF讀取成功便自動終止,這種狀況下,經過readerSession:didInvalidateWithError:回調,此時狀態爲NFCReaderSessionInvalidationErrorFirstNDEFTagRead
 */
- (instancetype)initWithDelegate:(id<NFCNDEFReaderSessionDelegate>)delegate queue:(dispatch_queue_t)queue invalidateAfterFirstRead:(BOOL)invalidateAfterFirstRead;
//設備是否支持NFC讀取標籤
@property(class, nonatomic, readonly) BOOL readingAvailable;
//讀取到信息時回調
- (void)readerSession:(NFCNDEFReaderSession *)session didDetectNDEFs:(NSArray<NFCNDEFMessage *> *)messages;

//發生錯誤或者無效時回調
- (void)readerSession:(NFCNDEFReaderSession *)session didInvalidateWithError:(NSError *)error;

2. 標籤

2.1 NFCTag

//檢測到的標籤是否可用
@property(nonatomic, getter=isAvailable, readonly) BOOL available;

//獲取會話
@property(nonatomic, weak, readonly) id<NFCReaderSession> session;

//標籤類型
@property(nonatomic, readonly, assign) NFCTagType type;

2.2 NFCTagCommandConfiguration

//最大的重置次數,最多爲256,默認爲0
@property(nonatomic, assign) NSUInteger maximumRetries;

//重試的時間間隔,默認爲0
@property(nonatomic, assign) NSTimeInterval retryInterval;

3. NDEFMessage

3.1 NFCNDEFPayload 消息載體

//標識,由NDEF規範定義
@property(nonatomic, copy) NSData *identifier;
//載體中的數據,由NDEF規範定義
@property(nonatomic, copy) NSData *payload;
//載體的類型,由NDEF規範定義
@property(nonatomic, copy) NSData *type;
//載體的類型名稱格式,由NDEF規範定義
@property(nonatomic, assign) NFCTypeNameFormat typeNameFormat;

NFCTypeNameFormat定義的類型:ide

  • TypeNameFormatAbsoluteURI 統一使用資源標識標準
  • NFCTypeNameFormatEmpty 空信息
  • NFCTypeNameFormatMedia RFC 2046 定義的媒體類型
  • NFCTypeNameFormatNFCExternal
  • NFCTypeNameFormatNFCWellKnown
  • NFCTypeNameFormatUnchanged
  • NFCTypeNameFormatUnknown 未知

3.2 NFCNDEFMessage

//NFCNDEFPayload數組
@property(nonatomic, copy) NSArray<NFCNDEFPayload *> *records;

4. Errors

typedef NS_ERROR_ENUM(NFCErrorDomain, NFCReaderError) {
    NFCReaderErrorUnsupportedFeature = 1,  //不支持此功能
    NFCReaderErrorSecurityViolation, //安全問題
    NFCReaderTransceiveErrorTagConnectionLost = 100,//標籤鏈接丟失
    NFCReaderTransceiveErrorRetryExceeded,//重連次數過多
    NFCReaderTransceiveErrorTagResponseError,//標籤響應錯誤
    NFCReaderSessionInvalidationErrorUserCanceled = 200,//用戶取消會話
    NFCReaderSessionInvalidationErrorSessionTimeout,//會話時間超時
    NFCReaderSessionInvalidationErrorSessionTerminatedUnexpectedly,//會話意外終止
    NFCReaderSessionInvalidationErrorSystemIsBusy,//系統正忙,會話失敗
    NFCReaderSessionInvalidationErrorFirstNDEFTagRead,//讀取的第一個NDEF
    NFCTagCommandConfigurationErrorInvalidParameters = 300,//標籤配置無效參數
};

3、注意事項

  • 會話要求當前程序必須在前臺運行,而且處於可視化界面
  • 若是是程序處於後臺或者不處於可視化界面,則中止掃描
  • 每次掃描的限制時間爲60秒,60秒後可再次初始化會話對象進行再次掃描

4、示例

1.配置開發證書atom

2.在TARGETS>Capabilities中打開Near Field Communication Tag Readingspa

3.在TARGETS>Info中配置NFC使用說明線程

4.代碼邏輯3d

#import "ViewController.h"
#import <CoreNFC/CoreNFC.h>

@interface ViewController ()<NFCNDEFReaderSessionDelegate>{
    NFCNDEFReaderSession *_session;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //若是但願讀取多個標籤invalidateAfterFirstRead設置爲No
    _session = [[NFCNDEFReaderSession alloc] initWithDelegate:self queue:nil invalidateAfterFirstRead:YES];
}

//開始掃描
- (IBAction)startSession:(id)sender {
    [_session beginSession];
}

//結束掃描
- (IBAction)endSession:(id)sender {
    [_session invalidateSession];
}

//掃描到的回調
-(void)readerSession:(NFCNDEFReaderSession *)session didDetectNDEFs:(NSArray<NFCNDEFMessage *> *)messages{
    
}

//錯誤回調
-(void)readerSession:(NFCNDEFReaderSession *)session didInvalidateWithError:(NSError *)error{
    
}

@end
相關文章
相關標籤/搜索