iOS NFC读取tag功能实现

首先引用框架

#import <CoreNFC/CoreNFC.h>

遵守协议

<NFCTagReaderSessionDelegate>

设置属性

@property(strong,nonatomic)NFCTagReaderSession *session;

@property(strong,nonatomic)id<NFCMiFareTag> currentTag;

做好机型以及系统版本判断

if (@available(iOS 13.0, *)) {
            if (NFCNDEFReaderSession.readingAvailable) {
                self.session = [[NFCTagReaderSession alloc]
                                    initWithPollingOption:(NFCPollingISO14443 | NFCPollingISO15693 | NFCPollingISO15693) delegate:self queue:dispatch_get_main_queue()];
                self.session.alertMessage = @"读取卡片,请将卡片靠近手机";
                [self.session beginSession]; //开始识别 弹出识别提示框
            }else{
                [SVProgressHUD showInfoWithStatus:@"NFC功能只支持iphone7以及iOS13.0以上设备"];
            }
        }else{
            [SVProgressHUD showInfoWithStatus:@"NFC功能只支持iphone7以及iOS13.0以上设备"];
        }

实现代理方法以及转化data数据

- (void)tagReaderSession:(NFCTagReaderSession *)session didDetectTags:(NSArray<__kindof id<NFCTag>> *)tags API_AVAILABLE(ios(13.0)) API_UNAVAILABLE(watchos, macos, tvos){
    _currentTag = [tags firstObject];
    NSData *data ;
    if (self.currentTag.type == NFCTagTypeMiFare) {
        id<NFCMiFareTag> mifareTag = [self.currentTag asNFCMiFareTag];
        data = mifareTag.identifier;
    }else if (self.currentTag.type == NFCTagTypeISO15693){
        id<NFCISO15693Tag> mifareTag = [self.currentTag asNFCISO15693Tag];
        data = mifareTag.identifier;
    }else if (self.currentTag.type == NFCTagTypeISO15693){
        id<NFCISO15693Tag> mifareTag = [self.currentTag asNFCISO15693Tag];
        data = mifareTag.identifier;
    }else{
        [SVProgressHUD showErrorWithStatus:@"未识别出NFC格式"];
    }
    
    NSString *str = [self convertDataBytesToHex:data];
    
    //识别成功处理
    
    [session invalidateSession];
    
}



- (NSString *)convertDataBytesToHex:(NSData *)dataBytes {
    if (!dataBytes || [dataBytes length] == 0) {
        return @"";
    }
    NSMutableString *hexStr = [[NSMutableString alloc] initWithCapacity:[dataBytes length]];
    [dataBytes enumerateByteRangesUsingBlock:^(const void *bytes, NSRange byteRange, BOOL *stop) {
        unsigned char *dataBytes = (unsigned char *)bytes;
        for (NSInteger i = 0; i < byteRange.length; i ++) {
            NSString *singleHexStr = [NSString stringWithFormat:@"%x", (dataBytes[i]) & 0xff];
            if ([singleHexStr length] == 2) {
                [hexStr appendString:singleHexStr];
            } else {
                [hexStr appendFormat:@"0%@", singleHexStr];
            }
        }
    }];
    return hexStr;
}

- (void)tagReaderSession:(NFCTagReaderSession *)session didInvalidateWithError:(NSError *)error{
    if (error.code == 200) {
        return;
    }
    [SVProgressHUD showErrorWithStatus:error.localizedDescription];
    [session invalidateSession];
}

参考链接:https://blog.csdn.net/Han_SM/article/details/103046741

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值