IOS开发初窥苹果NFC功能

从iPhone的历代机型配置中,可以看出在iPhone6及以上机型都拥有了支持NFC功能的固件,但是一直不开放给开发者使用,直到iOS11才开放仅有的具有读取NDEF格式的功能,并且需要iPhone7或者iPhone7 Plus以上的机型才支持使用。干货来了.........

1、配置开发证书记得选上标记项

IOS开发初窥苹果NFC功能_第1张图片

2.在TARGETS>Capabilities中打开Near Field Communication

IOS开发初窥苹果NFC功能_第2张图片
图片发自App

3.在TARGETS>Info中配置NFC使用说明

IOS开发初窥苹果NFC功能_第3张图片
图片发自App

4.代码逻辑

导入#import

#import "ViewController.h"

#import

#import

@interface ViewController ()

@property (nonatomic, strong) NFCNDEFReaderSession *session;

@property (weak, nonatomic) IBOutlet UILabel *myLabel;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    UIButton *queryBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    queryBtn.frame = CGRectMake(50, 50, 100, 40);

    queryBtn.backgroundColor = [UIColor redColor];

    [queryBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [queryBtn setTitle:@"开始查询" forState:UIControlStateNormal];

    [queryBtn addTarget:self action:@selector(ClickQuery) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:queryBtn];

   

    UIButton *endQueryBtn = [UIButton buttonWithType:UIButtonTypeCustom];

    endQueryBtn.frame = CGRectMake(200, 50, 100, 40);

    endQueryBtn.backgroundColor = [UIColor redColor];

    [endQueryBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    [endQueryBtn setTitle:@"结束查询" forState:UIControlStateNormal];

    [endQueryBtn addTarget:self action:@selector(ClickEndQuery) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:endQueryBtn];

   

}

#pragma mark -- 开始查询

-(void)ClickQuery{

 

    //如果希望读取多个标签invalidateAfterFirstRead设置为NO

    self.session = [[NFCNDEFReaderSession alloc] initWithDelegate:self queue:dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT) invalidateAfterFirstRead:YES];

    NSLog(@"%@",[NSThread currentThread]);

    [self.session beginSession];

   

}

#pragma mark -- 结束查询

-(void)ClickEndQuery{

    [SVProgressHUD setBackgroundColor:[UIColor blackColor]];

    [SVProgressHUD setForegroundColor:[UIColor whiteColor]];

    [SVProgressHUD showWithStatus:@"结束查询"];

    [SVProgressHUD dismissWithDelay:1.0];

   

    [self.session invalidateSession];

}

#pragma mark --

//扫描到的回调

-(void)readerSession:(NFCNDEFReaderSession *)session didDetectNDEFs:(NSArray *)messages{

   

    for (NFCNDEFMessage *message in messages) {

        for (NFCNDEFPayload *payload in message.records) {

           

            NSLog(@"Payload data=%@",payload.payload);

           

            NSString *str = [[NSString alloc] initWithData:payload.payload encoding:NSUTF8StringEncoding];

           

            //回到主线程

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                [self.myLabel setText:str];

            }];

           

        }

    }

}

//错误回调

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

    NSLog(@"error = %@", error);

 

}

@end

5.下面是操作效果图!老铁记得准备标签啊

图片发自App



IOS开发初窥苹果NFC功能_第4张图片
图片发自App


IOS开发初窥苹果NFC功能_第5张图片
图片发自App

你可能感兴趣的:(IOS开发初窥苹果NFC功能)