從iPhone的歷代機(jī)型配置中猫胁,可以看出在iPhone6及以上機(jī)型都擁有了支持NFC功能的固件,但是一直不開放給開發(fā)者使用,直到iOS11才開放僅有的具有讀取NDEF格式的功能磕道,并且需要iPhone7或者iPhone7 Plus以上的機(jī)型才支持使用示弓。干貨來了.........
1讳侨、配置開發(fā)證書記得選上標(biāo)記項(xiàng)
2.在TARGETS>Capabilities中打開Near Field Communication
3.在TARGETS>Info中配置NFC使用說明
4.代碼邏輯
導(dǎo)入#import <CoreNFC/CoreNFC.h>
#import "ViewController.h"
#import <SVProgressHUD.h>
#import <CoreNFC/CoreNFC.h>
@interface ViewController ()<NFCNDEFReaderSessionDelegate>
@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:@"結(jié)束查詢" forState:UIControlStateNormal];
? ? [endQueryBtn addTarget:self action:@selector(ClickEndQuery) forControlEvents:UIControlEventTouchUpInside];
? ? [self.view addSubview:endQueryBtn];
? ?
}
#pragma mark -- 開始查詢
-(void)ClickQuery{
?
? ? //如果希望讀取多個(gè)標(biāo)簽invalidateAfterFirstRead設(shè)置為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 -- 結(jié)束查詢
-(void)ClickEndQuery{
? ? [SVProgressHUD setBackgroundColor:[UIColor blackColor]];
? ? [SVProgressHUD setForegroundColor:[UIColor whiteColor]];
? ? [SVProgressHUD showWithStatus:@"結(jié)束查詢"];
? ? [SVProgressHUD dismissWithDelay:1.0];
? ?
? ? [self.session invalidateSession];
}
#pragma mark -- <NFCNDEFReaderSessionDelegate>
//掃描到的回調(diào)
-(void)readerSession:(NFCNDEFReaderSession *)session didDetectNDEFs:(NSArray<NFCNDEFMessage *> *)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];
? ? ? ? ? ? }];
? ? ? ? ? ?
? ? ? ? }
? ? }
}
//錯(cuò)誤回調(diào)
-(void)readerSession:(NFCNDEFReaderSession *)session didInvalidateWithError:(NSError *)error{
? ? NSLog(@"error = %@", error);
?
}
@end
5.下面是操作效果圖!老鐵記得準(zhǔn)備標(biāo)簽啊