iOS 11 Core NFC - 有示例代码吗?

iOS 11 Core NFC - any sample code?

我刚刚将第一个 iOS 11 beta 安装到 iPhone 7,并且有兴趣尝试 NFC。设置中没有关于它的任何内容。我想知道是否有任何示例代码显示如何读取标签。谁能在代码片段中展示如何使用 Core NFC SDK?

在 Apple Developer 站点中,创建一个新的 App ID 并确保 NFC Tag Reading 已启用。

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

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

并将这些添加到权利文件中:

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

在相应的文件中应该是这样的:

也可以通过 Xcode 中的“功能”选项卡启用 Core NFC。

Objective-c

导入CoreNFC

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

您还必须添加 didInvalidateWithError 委托回调,否则您将不符合协议:

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

}

您可以通过以下方式停止 reader:

[session invalidateSession];

Swift 3/4

导入CoreNFC

import CoreNFC

并设置委托:

class YourViewController: UIViewController, NFCNDEFReaderSessionDelegate

在 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)
    }
  }
}

您可以通过以下方式停止 reader:

session.invalidateSession

用法

启动视图后,您应该立即看到 iOS NFC reader 对话框,如下所示:

出现此对话框后,您有大约一秒钟的时间将 iPhone 放在您要读取的 NFC 标签附近。否则,该字段将被停用(这似乎是 Apple 端的一个错误)。我经常需要取消并重试以获得一致的读数。 More details here

要解决此问题,您可以将 com.apple.developer.nfc.readersession.formats 密钥添加到您的权利文件中。密钥应与启用的 NFS 类型的数组相关联。 例如,您可以尝试以下操作:

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

它对我有用。

您需要确保使用说明到位,并在 Apple Developer Center 内将功能添加到应用程序中。 我有一个基于我的经验的教程(Swift 4)。可在此处获取:Core NFC Tutorial

第二个 Xcode 测试版已更新。

从开发人员中心向您的应用程序添加 NFC 功能。 Identifiers -> App IDs -> 启用 "NFC Tag Reading".

如果您的项目没有授权文件,让 Xcode 为您创建一个,只需激活然后停用 Xcode -> 项目目标 -> 功能中的任何功能。您将在项目导航器中找到一个新的 [AppName].entitlements 文件。右键单击该文件并 select "Open as -> Source Code"。在 <dict></dict>:

之间手动输入以下条目
<key>com.apple.developer.nfc.readersession.formats</key>
<array>
    <string>NDEF</string>
</array>

一旦 Xcode 9 允许从功能 selection 中启用 NFC 标签读取,此步骤就会过时,因为您所要做的就是在那里启用它。当前(第一个)Beta 版本不支持此功能。

您还需要输入隐私警告的使用说明 iOS 将显示给用户。 (目前(测试版 1)当设备准备好扫描并显示包含此消息的本机系统对话框时,将显示此警告。但是,这似乎是未开发的。)打开目标的 Info.plist 和开始输入 "Privacy",您可以通过点击 return 向下滚动到 "Privacy - NFC Usage Description" 到 select。在右栏中为您的用户输入有意义的解释。

现在您应该可以导入 CoreNFC,在 Swift:

import CoreNFC

然后前往 Apple 的 documentation

重要:如果编译器 return 使用 No such module 'CoreNFC' 出现错误,请检查您是否 select 编辑了一个实际 iOS 11 个要构建的设备,而不是模拟器。它还必须是 iPhone 7 或 7 plus。这可能会在未来的版本中改变,但测试 NFC 只会完全使用实际硬件。 (cp. Core Bluetooth,您可以在模拟器上 运行 但不能测试实际功能。) Beta 2 不再有这个问题。但是,实际测试仍然需要 iPhone 7/p 形式的实际硬件。

我使用这些答案中的资源将 Core NFC 添加到项目中。另一件没有注意到的事情是,即使您通过授权手动添加功能,Xcode 似乎也不会查看文件,除非您打开了功能。这可能是由于 Xcode 9 Beta 1 没有将 Core NFC 作为许多人示例项目的功能切换。因此,如果您仍然遇到问题,请确保至少打开一项其他功能!在我这样做之前,我立即看到意外终止错误 return。

我会将其作为评论保留下来,但还没有足够的声誉来这样做。认为这很重要,需要注意。

只是为了丰富以前的答案,重要的是要牢记 NFCNDEFReaderSession class:

的这些注意事项
  • Reader 用于处理 NFC 数据交换格式 (NDEF) 标签的会话。此会话需要您进程中的“com.apple.developer.nfc.readersession.formats”权限。此外,您的应用程序的 Info.plist 必须包含非空的使用说明字符串。
  • NDEF reader 会话将自动扫描并检测 NFC 论坛 包含有效 NDEF 消息的标签。 NFC 论坛标签类型 1 到 5 支持 NDEF 格式。模态系统 UI 将呈现 一旦调用 -beginSession 以通知会话开始;这 UI sheet 会话失效时自动解除 由用户或调用 -invalidateSession.
  • 打开的会话在调用 -beginSession 后有 60 秒的时间限制-readerSession:didInvalidateWithError: 到达时限会return NFCReaderSessionInvalidationErrorSessionTimeout 报错
  • 系统中只允许 1 个活动 reader 会话-readerSession:didInvalidateWithError: 将 return NFCReaderSessionInvalidationErrorSystemIsBusy 当一个新的 reader 会话由 -beginSession 启动时,当有一个活动的 reader 会话时。
  • 当用户点击 UI. 上的完成按钮时,
  • -readerSession:didInvalidateWithError: 将 return NFCReaderSessionInvalidationErrorUserCanceled
  • -readerSession:didInvalidateWithError:将returnNFCReaderSessionInvalidationErrorSessionTerminatedUnexpectedly客户端应用程序进入后台状态时-readerSession:didInvalidateWithError: 将 return NFCReaderErrorUnsupportedFeature
    1. reader 模式功能在硬件上不可用
    2. 客户端应用程序没有所需的权利。

我的两分钱:

1) 在 xcode 9.0 (beta 4 9M189t ) 下 如果您已经添加了功能, 无需手动添加:

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

自动完成

2) 如果不使用 iPhone 7 或您在模拟器中则不会崩溃:

您将被召入:

func readerSession(_ session: NFCNDEFReaderSession, didInvalidateWithError error: Error) {
        print(error.localizedDescription)
    }

它将显示: "Feature not supported"

3) 不要错过:

self.nfcSession?.begin() // will trigger callback

所以:

    final private func setup(){
    self.nfcSession = NFCNDEFReaderSession(delegate: self, queue: nil, invalidateAfterFirstRead: true)
    self.nfcSession?.alertMessage = "Put your NFC TAG over iPhone.."
    self.nfcSession?.begin() // will trigger callback

}

4) 如果用户取消,您将得到:

"Session is invalidated by user cancellation"

在 didInvalidateWithError 回调中。