一.環(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同步.

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

右鍵 .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