iOS 11核心NFC - 任何示例代码? [英] iOS 11 Core NFC - any sample code?

查看:408
本文介绍了iOS 11核心NFC - 任何示例代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在iPhone 7上安装了第一个iOS 11测试版,我对尝试NFC感兴趣。在设置中没有任何关于它。我想知道是否有任何示例代码显示如何读取标记。任何人都可以在代码片段中展示如何使用Core NFC SDK吗?

I just installed the first iOS 11 beta to an iPhone 7 and am interested in trying the NFC. There's nothing about it in settings. I am wondering if there's any sample code out there showing how to read a tag. Can anyone show how to use the Core NFC SDK, in a code snippet?

推荐答案

在Apple Developer网站上,创建一个新的应用程序ID并确保已启用 NFC标签阅读

In the Apple Developer site, create a new App ID and make sure that NFC Tag Reading is enabled.

将以下行添加到.plist文件中:

Add the following lines to your .plist file:

<key>NFCReaderUsageDescription</key>
<string>NFC Tag!</string>

以及这些权利文件:

<key>com.apple.developer.nfc.readersession.formats</key>
    <array>
        <string>NDEF</string>
    </array>

它应该在相应的文件中看起来像这样:

It should look something like this in the corresponding files:

此外,还可以通过Xcode中的功能选项卡启用Core NFC。

Also Core NFC can be enabled via the Capabilities tab in Xcode.

导入 CoreNFC

#import <CoreNFC/CoreNFC.h>

并设置代理:

@interface YourViewController : UIViewController <NFCNDEFReaderSessionDelegate>

在viewDidLoad中:

In 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);
        }
    }        
}

您还必须添加 didInvalidateWithError 委托回调或你不符合协议:

You must also add the didInvalidateWithError delegate callback or you'll not conform with protocol:

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

}

您可以通过以下方式停止阅读:

You can stop the reader with:

[session invalidateSession];



Swift 3/4



导入 CoreNFC

import CoreNFC

并设置委托:

class YourViewController: UIViewController, NFCNDEFReaderSessionDelegate

在viewDidLoad中:

In viewDidLoad:

override func viewDidLoad() {
        super.viewDidLoad()

        let session = NFCNDEFReaderSession(delegate: self,
                      queue: DispatchQueue(label: "queueName", attributes: .concurrent), invalidateAfterFirstRead: false)  
        session?.begin()
    }

在委托回调中:

func readerSession(_ session: NFCNDEFReaderSession, didDetectNDEFs messages: [NFCNDEFMessage]) {
  for message in messages {
    for record in message.records {
      print(record.payload)
    }
  }
}

您可以通过以下方式停止阅读器:

You can stop the reader with:

session.invalidateSession



用法



启动后查看你应该立即看到iOS NFC阅读器对话框,如下所示:

Usage

After launching the view you should immediately see the iOS NFC reader dialog like so:

一旦出现此对话框,您将有大约一秒钟将iPhone放在您想要阅读的NFC标签附近。否则,该字段被停用(这似乎是苹果公司的一个错误)。我经常需要取消并重试以获得一致的读数。 此处提供更多详情

Once this dialog appears you have about a second to place the iPhone near the NFC tag you want to read. Otherwise, the field is deactivated (this seems to be a bug on Apple's end). I often needed to cancel and retry to get consistent readings. More details here.

这篇关于iOS 11核心NFC - 任何示例代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆