美文网首页
iOS11 Core NFC

iOS11 Core NFC

作者: 缭雾 | 来源:发表于2017-06-07 21:23 被阅读838次

iOS11 Core NFC

iPhone6开始支持NFC(Near Field Communication ),但是最近苹果最近(才)开放了NFC的部分接口。

可以实现检测NFC标签(NFC tags)并读取包含NDEF(NFC Data Exchange Format)数据。

最起码能当读卡器玩了吧。

概述

Core NFC可以读取NFC tags,提供给用户有关其物理环境和实体对象的更多信息。例如,小明在逛商场,他可以通过应用程序的NFC功能获取到一些相关的商品信息。

Note
读取NFC NDEF tag当前只支持iPhone7/7+

使用Core NFC,您可以读取符合NFC数据交换格式(NDEF)的五种标签(type1到5)。

想要阅读标签,需要创建一个NFCNDEFReaderSession的实例并设置代理。session会对NFC标签进行轮询,当找到NDEF的消息时,会通过代理回调。代理收到消息后会,我们将session置为invalid([session invalidateSession]).

实现

调试环境 Xcode9 beta + iPhone7

需要注意的CoreNFC当前没有X86的版本,需要真机调试,否则报错。。。 (Xcode9正式版没有这个问题)


2017060763145coreNFC.png
  1. 新建AppleId,勾选NFC Tag Reading

    2017060729452.png
  2. 新建工程配置好BundleId,与AppleId相匹配。添加 20170607628875.png
  3. info.plist添加

    <key>NFCReaderUsageDescription</key>
    <string>We are going to use you NFC!</string>
    
  4. .entitlements文件添加内容: (Xcode9正式版,直接勾选target->Capabilities->Near Field Communication Tag Reading即可)

        <key>com.apple.developer.nfc.readersession.formats</key>
        <array>
            <string>NDEF</string>
        </array>
    
    20170607123073.png 20170607170904.png
  5. 代码实现比较简单

#import <CoreNFC/CoreNFC.h>

@interface ViewController ()<NFCNDEFReaderSessionDelegate>

@end

@implementation ViewController

- (IBAction)clicked:(id)sender {
    
    NFCNDEFReaderSession *session = [[NFCNDEFReaderSession alloc] initWithDelegate:self queue:nil invalidateAfterFirstRead:NO];
    //NSLog(@" ready ? %@", @([session  isReady]));
    [session beginSession];
}

#pragma mark - NFCNDEFReaderSessionDelegate

- (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);
            //[session invalidateSession];
        dispatch_async(dispatch_get_main_queue(), ^{
            self.showingLabel.text = [[NSString alloc]initWithData:payload.payload encoding:NSUTF8StringEncoding];
        });
        }
    }
}

- (void)readerSession:(NFCNDEFReaderSession *)session didInvalidateWithError:(NSError *)error{
    NSLog(@"%@",error);
}
  1. 运行

    2017060793692img.png

ok,先这样了。 明天用公司的卡片试试。。

// ---更新 6.9
在iPhone7上做测试,发现直到超时都没有读取出到数据,项目配置没检查出问题。
怀疑是否是卡片的原因?
或者等出iOS11正式出了,在进行测试吧

--- 更新 2017-09-21

iOS11正式版出来了,现在能读取到NFC标签中的数据了

参考
https://developer.apple.com/documentation/corenfc
https://stackoverflow.com/questions/44380305/ios-11-core-nfc-any-sample-code

相关文章

  • iOS11 Core NFC

    iOS11 Core NFC iPhone6开始支持NFC(Near Field Communication )...

  • iOS11新特性NFC检测

    Core NFC是在iOS11中引入,用于处理NFC阅读Tag。目前NFC只开启了读权限,据分析应该是为了Appl...

  • iOS11 Core NFC

    About Core NFC Core NFC支持的读取数据类型: Core NFC框架特性/要求 目前支持NFC...

  • 迟来的《Core NFC》

    迟来的《Core NFC》 迟来的《Core NFC》

  • iOS NFC开发(OC、swift双语实现)

    Core NFC在iOS 11中引入,用于处理NFC阅读Tag。由于目前只开放了读的权限,所以Core NFC是非...

  • iOS NFC开发——Core NFC

    Core NFC在iOS 11中引入,用于处理NFC阅读Tag。由于目前只开放了读的权限,所以Core NFC是非...

  • iOS NFC — CoreNFC

    Core NFC在iOS 11中引入,用于处理NFC阅读Tag。由于目前只开放了读的权限,所以Core NFC是非...

  • Core NFC

    预研NFC 在预研公司L28T手环用NFC来上课签到上课的功能,看完供应商提供的芯片文档,得知苹果要求的NFC数据...

  • NFC的使用

    一、NFC的使用范围 苹果在iOS11上推出了NFC的功能,开发者可以根据自身的需要使用这个功能进行开发。NFC有...

  • NFC的使用说明

    一、NFC的使用范围 苹果在iOS11上推出了NFC的功能,开发者可以根据自身的需要使用这个功能进行开发。NFC有...

网友评论

      本文标题:iOS11 Core NFC

      本文链接:https://www.haomeiwen.com/subject/wtwafxtx.html