iOS 11 CoreNFC 使用

CoreNFC 有以下几点要注意

  1. 开启一个session,并且同时只能开启一个

  2. App完全在前台模式,切入后台失效

  3. session最多扫存活60s,超时必须重启新session

  4. NFC读取权限

  5. 目前只支持7、7P以及之后
    接下来我们开始Demo

第一步:

新建一个工程,然后打开NFC的配置


第二步:

在你的info.plist中添加:
Privacy - NFC Scan Usage Description
NFC usage description
com.apple.developer.nfc.readersession.formats
NDEF

第三步:

导入CoreNFC.framework 的框架

image.png

第四步:

实现代码:

#import <CoreNFC/CoreNFC.h>
#import "ViewController.h"

@interface ViewController ()<NFCNDEFReaderSessionDelegate>

@property(nonatomic,strong)NFCNDEFReaderSession * nfcSession;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.nfcSession = [[NFCNDEFReaderSession alloc] initWithDelegate:self queue:dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT) invalidateAfterFirstRead:NO];
    [self.nfcSession beginSession];
    
}
-(void)readerSession:(NFCNDEFReaderSession *)session didDetectNDEFs:(NSArray<NFCNDEFMessage *> *)messages{
    [messages enumerateObjectsUsingBlock:^(NFCNDEFMessage * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj.records enumerateObjectsUsingBlock:^(NFCNDEFPayload * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSLog(@"typeNameFormat:%d",obj.typeNameFormat);
            NSLog(@"payload:%@",obj.payload);
            NSLog(@"type:%@",obj.type);
            NSLog(@"identifier:%@",obj.identifier);
        }];
    }];
}
-(void)readerSession:(NFCNDEFReaderSession *)session didInvalidateWithError:(nonnull NSError *)error{
    NSLog(@"error:%@",error.localizedDescription);
}
@end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容