开发语言:objective-c
问题描述:
使用 wakdev 的 NFC工具-iOS即NFC Tools 读取标签中自己写入的 NDEF 数据,缺少"类型"说明符和语言环境的使用情况
原代码:
NSString *type = @"Q";
NSData *typeData = [type dataUsingEncoding:NSUTF8StringEncoding];
NSString *identifier = @"123456789";
NSData *identifierData = [identifier dataUsingEncoding:NSUTF8StringEncoding];
NSString *payload1 = @"Hello World!";
NSData *payloadData1 = [payload1 dataUsingEncoding:NSUTF8StringEncoding];
if (@available(iOS 13.0, *)) {
NFCNDEFPayload *NDEFPayload1=[[NFCNDEFPayload alloc]initWithFormat:NFCTypeNameFormatNFCWellKnown type:typeData identifier:identifierData payload:payloadData1];
NFCNDEFMessage *essage = [[NFCNDEFMessage alloc]initWithNDEFRecords:@[NDEFPayload1]];
}
查资料看到一个答案,不过是用Swift写的
var payloadData = Data([0x02,0x65,0x6E]) // 0x02 +'en'=区域说明符;0x7a,0x68 = 'zh';0x65,0x6e = 'en'
payloadData.append("Text To Write".data(using: .utf8)!)
let payload = NFCNDEFPayload.init(
format: NFCTypeNameFormat.nfcWellKnown,
type: "T".data(using: .utf8)!,
identifier: Data.init(count: 0),
payload: payloadData,
chunkSize: 0)
通过以下网站可查询十六进制码对应的字符:
ASCII码表在线查询
https://www.litefeel.com/tools/ascii.php
修改后的代码
NSString *type = @"Q";
NSData *typeData = [type dataUsingEncoding:NSUTF8StringEncoding];
NSString *identifier = @"123456789";
NSData *identifierData = [identifier dataUsingEncoding:NSUTF8StringEncoding];
NSString *payload1 = @"Hello World!";
NSData *payloadData1 = [payload1 dataUsingEncoding:NSUTF8StringEncoding];
Byte byte[] = {0x02,0x65,0x6E};
NSData *areaSpecifierData = [NSData dataWithBytes:byte length:3];
NSMutableData *payloadData = [[NSMutableData alloc] initWithData:areaSpecifierData];
[payloadData appendData:payloadData1];
if (@available(iOS 13.0, *)) {
NFCNDEFPayload *NDEFPayload1 = [[NFCNDEFPayload alloc]initWithFormat:NFCTypeNameFormatNFCWellKnown type:typeData identifier:identifierData payload:payloadData];
NFCNDEFMessage *essage = [[NFCNDEFMessage alloc]initWithNDEFRecords:@[NDEFPayload1]];
}
有不对的地方,还请大家指正
网友评论