iphone如何使用CoreNFC
生活随笔
收集整理的這篇文章主要介紹了
iphone如何使用CoreNFC
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
由于本人的工作和移動支付有關,所以一直對NFC技術比較關注。看到WWDC會議IOS 11開放了CoreNFC,趕緊了解一下。
使用NFC前需要注意一下幾點:
-
需要開啟一個session,與其他session類似,同時只能開啟一個
-
需要App完全在前臺模式
-
每個session最多掃描60s,超時需再次開啟新session
-
配置讀取單個或多個Tag,配置為單個時,會在讀取到第一個Tag時自動結束session
-
隱私描述(后文會寫到如何配置)會在掃描頁面顯示
在蘋果開發者網站創建一個AppID, 并且確保NFC Tag Reading有效。
在.plist文件中加入以下項
<key>NFCReaderUsageDescription</key> <string>We are going to use you NFC!</string> <key>com.apple.developer.nfc.readersession.formats</key><array><string>NDEF</string></array>
導入framework
#import <CoreNFC/CoreNFC.h>
設置委托
@interface YourViewController : UIViewController <NFCNDEFReaderSessionDelegate>編輯代碼viewDidLoad
- (void)viewDidLoad {[super viewDidLoad];// Do any additional setup after loading the view, typically from a nib.NFCNDEFReaderSession *session = [[NFCNDEFReaderSession alloc] initWithDelegate:self queue:dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT) invalidateAfterFirstRead:NO];[session beginSession];}委托方法 - (void) readerSession:(nonnull NFCNDEFReaderSession *)session didDetectNDEFs:(nonnull NSArray<NFCNDEFMessage *> *)messages {for (NFCNDEFMessage *message in messages) {for (NFCNDEFPayload *payload in message.records) {NSLog(@"Payload data:%@",payload.payload);}} }
在授權文件中加入key <key>com.apple.developer.nfc.readersession.formats</key><array><string>NDEF</string></array>
NFC Tag淘寶上可以買到,挺便宜的。
Android手機裝個APP往Tag寫入數據就可以開始測試了。
Apple什么時候可以真正開放NFC的卡模擬方式就好了。只是開放讀卡器模式應用場景還是太受限了。
參考資料
http://jamesonquave.com/blog/core-nfc-tutorial-for-nfc-on-ios-devices/
https://developer.apple.com/documentation/corenfc
總結
以上是生活随笔為你收集整理的iphone如何使用CoreNFC的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Kotlin的hello world
- 下一篇: Core ML介绍 (Apple机器学习