iOS 11 Core NFC使用詳解

一.環(huán)境

系統(tǒng):iOS 11+;(現(xiàn)在還沒推送,可以使用iTunes通過固件升級);

開發(fā)工具:Xcode9.0+;

iOS11固件跟Xcode9.0測試版下載地址 https://developer.apple.com/download/

支持機型:iPhone7 ,iPhone7P;

二.配置項目

1.進入開發(fā)者中心設置AppID支持NFC Tag Reading,并且設置項目的Bundle Identifier跟新設置的AppID同步.

設置AppID

2.創(chuàng)建 .entitlements 文件(可以直接創(chuàng)建,也可以到target->capabilities里面打開任意開關,.entitlements文件會自動創(chuàng)建)。

創(chuàng)建 .entitlements 文件

右鍵 .entitlements文件點擊 ?“Open As Source Code” 添加以下代碼:

添加成功后entitlements文件顯示如下:

然后在Info.plist文件中添加"Privacy - NFC Scan Usage Description"權限,當使用NFC時,將向用戶顯示此消息.

三.使用

導入 CoreNFC.h (只有在真機導入才不報錯)

遵循協(xié)議 NFCNDEFReaderSessionDelegate

@property (nonatomic, strong) NFCNDEFReaderSession *ndefSession;

//床架會話

- (void)creatReader { ? ? ? ?

self.ndefSession = [[NFCNDEFReaderSession alloc] initWithDelegate:self queue:nil invalidateAfterFirstRead:NO];? ? ?

?? [_ndefSession beginSession];? ?

?}

//代理獲取傳輸?shù)男畔?/p>

- (void)readerSession:(NFCNDEFReaderSession *)session didDetectNDEFs:(NSArray*)messages

{

NSLog(@"Called: %s", __func__);

for(NFCNDEFMessage *message in messages)

{

for(NFCNDEFPayload *payload in message.records)

{

NSLog(@"TNF:%d", payload.typeNameFormat);

NSLog(@"Type: %@", payload.type.description);

NSLog(@"payload: %@", payload.payload.description);

dispatch_async(dispatch_get_main_queue(),^{

NSString *str =[NSString stringWithFormat:@"TNF: %d\nType: %@\nPayload: %@", payload.typeNameFormat, payload.type.description, payload.payload.description];

});

}

}

}

//獲取錯誤信息

- (void)readerSession:(NFCNDEFReaderSession *)session didInvalidateWithError:(NSError *)error

{

NSLog(@"Called: %s", __func__);

NSLog(@"error: %@", error.localizedDescription);

dispatch_async(dispatch_get_main_queue(),^{

});

_ndefSession = nil;

}


準備掃描



掃描成功界面


4.參考

測試demo: ?https://github.com/bellx2/ios11NFCTest/

常見問題及解決辦法:?https://stackoverflow.com/questions/44380305/ios-11-core-nfc-any-sample-code

Core NFC文檔:?https://developer.apple.com/documentation/corenfc


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容