一: 申请微信小程序NFC的URL Scheme
直达链接:https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/NFC.html
二: 换起NFC功能
@property (strong, nonatomic) NFCTagReaderSession *session;
if (NFCTagReaderSession.readingAvailable == NO)
[GHLoadingView showPopViewMessage:@"该机型不支持NFC功能!" success:NO andDismissTime:0.5];
return;
}
self.session = [[NFCTagReaderSession alloc] initWithPollingOption:(NFCPollingISO14443 | NFCPollingISO15693 | NFCPollingISO15693) delegate:self queue:dispatch_get_main_queue()];
self.session.alertMessage = @"查找卡片,请将卡片靠近手机";
[self.session beginSession]; //开始识别 弹出识别提示框
三: 写入数据
#pragma mark --- NFCTagReaderSessionDelegate ---
- (void)tagReaderSession:(NFCTagReaderSession *)session didDetectTags:(NSArray<__kindof id<NFCTag>> *)tags API_AVAILABLE(ios(13.0)) {
id<NFCMiFareTag> cuurentTag = [tags firstObject];
id<NFCMiFareTag> mifareTag = [cuurentTag asNFCMiFareTag];
if (mifareTag.type != NFCTagTypeMiFare){
[self invalidateSessionWitgMsg:@"NFC类型不可用" success:NO];
return;
}
// 先建立连接
MJWeakSelf
[self.session connectToTag:mifareTag completionHandler:^(NSError * _Nullable error) {
// 获取卡片状态:判断是否可以写入
if (error){
[weakSelf invalidateSessionWitgMsg:@"NFC连接失败" success:NO];
return;
}
[cuurentTag queryNDEFStatusWithCompletionHandler:^(NFCNDEFStatus status, NSUInteger capacity, NSError * _Nullable error) {
// 写入
if (status != NFCNDEFStatusReadWrite) {
[weakSelf invalidateSessionWitgMsg:@"NFC无法写入" success:NO];
return;
}
NSMutableArray *nfcArray = [NSMutableArray arrayWithCapacity:2];
{
NSString *type = @"U";
NSData *typeData = [type dataUsingEncoding:NSUTF8StringEncoding];
NSString *identifier = @"1001";
NSData *identifierData = [identifier dataUsingEncoding:NSUTF8StringEncoding];
NSString *payloadStr = @"URL Scheme";
NSData *payloadData = [payloadStr dataUsingEncoding:NSUTF8StringEncoding];
Byte byte[] = {0x00};
NSData *data = [NSData dataWithBytes:byte length:1];
NSMutableData *tmpData = [[NSMutableData alloc] init];
[tmpData appendData:data];
[tmpData appendData:payloadData];
NFCNDEFPayload *NDEFPayload = [[NFCNDEFPayload alloc] initWithFormat:NFCTypeNameFormatNFCWellKnown type:typeData identifier:identifierData payload:tmpData];
[nfcArray addObject:NDEFPayload];
}
{
NSString *type = @"android.com:pkg";
NSData *typeData = [type dataUsingEncoding:NSUTF8StringEncoding];
NSString *identifier = @"1001";
NSData *identifierData = [identifier dataUsingEncoding:NSUTF8StringEncoding];
NSString *payloadStr = @"com.tencent.mm";
NSData *payloadData = [payloadStr dataUsingEncoding:NSUTF8StringEncoding];
NFCNDEFPayload *NDEFPayload = [[NFCNDEFPayload alloc] initWithFormat:NFCTypeNameFormatNFCExternal type:typeData identifier:identifierData payload:payloadData];
[nfcArray addObject:NDEFPayload];
}
NFCNDEFMessage *message = [[NFCNDEFMessage alloc] initWithNDEFRecords:nfcArray];
[cuurentTag writeNDEF:message completionHandler:^(NSError * _Nullable error) {
NSString *msg = error?@"NFC写入失败":@"NFC写入成功";
[weakSelf invalidateSessionWitgMsg:msg success:error==nil];
}];
}];
}];
}
四: 参数传递
如果想要实现NFC唤起小程序打开指定界面,且传入参数就得在服务端实现请求scheme
接口参数:
NSDictionary *dic = @{
@"jump_wxa":@{
@"query":@"code=xxx&p=xxx",
@"env_version":@"develop",
@"path":@"page/index"
},
@"model_id":model_id,
@"sn":@"xxxxxxx"
};
query:
传入你想传的参数,必须符合url参数规则(xx=xx&xx=xx)
env_version:
换起的小程序环境版本,
path:
唤起的小程序指定页面路径
sn:
设备的sn,可以根据实际情况传入,没有限制,比如传一个当前任务的id,同一个sn只会生成唯一的scheme
注意:在切换env_version环境的时候必须也更换sn拼接规则,不然同一个sn在开发环境生成的scheme在正式环境不会重新生成,这样会唤起开发版的小程序.sn的拼接最好写成:env_version+ID