iOS NFC
程序员文章站
2023-10-31 14:16:40
#import @interface ViewController () @property (nonatomic,retain) UIButton *beginTestBtn; @property (nonatomic,retain) UILabel *textLabel; @end @imple... ......
#import <corenfc/corenfc.h> @interface viewcontroller ()<nfcndefreadersessiondelegate> @property (nonatomic,retain) uibutton *begintestbtn; @property (nonatomic,retain) uilabel *textlabel; @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; _begintestbtn = [[uibutton alloc]initwithframe:cgrectmake(100, 100, 100, 50)]; [_begintestbtn settitle:@"开始读取" forstate:uicontrolstatenormal]; [_begintestbtn settitlecolor:[uicolor bluecolor] forstate:uicontrolstatenormal]; _begintestbtn.titlelabel.textalignment = nstextalignmentleft; [_begintestbtn addtarget:self action:@selector(begintestbtnaction) forcontrolevents:uicontroleventtouchupinside]; _textlabel = [[uilabel alloc]initwithframe:cgrectmake(120, 180, 200, 50)]; _textlabel.text = @"待读取"; _textlabel.textcolor = [uicolor orangecolor]; [self.view addsubview:_textlabel]; nslog(@"进入vc"); [self.view addsubview:_begintestbtn]; [self.view addsubview:_textlabel]; // do any additional setup after loading the view, typically from a nib. } -(void)begintestbtnaction { /** 三个参数 第一个参数:代理对象 第二个参数:线程 第三个参数:session读取一个还是多个ndef。yes:读取一个结束,no:读取多个 */ nfcndefreadersession *session = [[nfcndefreadersession alloc] initwithdelegate:self queue:dispatch_queue_create(null, dispatch_queue_concurrent) invalidateafterfirstread:yes]; [session beginsession]; } /** 代理 */ - (void) readersession:(nonnull nfcndefreadersession *)session diddetectndefs:(nonnull nsarray<nfcndefmessage *> *)messages { __weak typeof(self) weakself=self; dispatch_async(dispatch_get_main_queue(), ^{ weakself.textlabel.text = @"读取成功"; }); for (nfcndefmessage *message in messages) { for (nfcndefpayload *payload in message.records) { nslog(@"payload data:%@",payload.payload); } } } - (void)readersession:(nfcndefreadersession *)session didinvalidatewitherror:(nserror *)error{ nslog(@"error:%@",error); __weak typeof(self) weakself=self; dispatch_async(dispatch_get_main_queue(), ^{ weakself.textlabel.text = @"读取失败"; }); }
推荐阅读
-
ipad怎么分屏?iPad升级ios9分屏功能怎么用?
-
ipad6什么时候上市?iPad6将会在ios8之后发布
-
iPad升级iOS9卡在滑动来升级怎么办?iPad升级iOS9卡在滑动来升级解决方法
-
腾讯吃鸡和苹果打架 升级完iOS 13后简直没法玩
-
iOS 13滑行键入体验:无比顺滑的原生输入法
-
iPhone 6s老机体验iOS 13:有喜有忧有建议
-
使用CSS3编写类似iOS中的复选框及带开关的按钮
-
Aiseesoft FoneEraser如何安装激活?ios数据永久删除软件激活教程
-
IOS开发(38)之Objective-c的@property 详解
-
iOS微信上线新功能:“按住说话”上滑可转文字