iOS 11-如何从CoreNFC读取/解析NDEF消息? [英] iOS 11 - How does one read/parse the NDEF Message from CoreNFC?

查看:160
本文介绍了iOS 11-如何从CoreNFC读取/解析NDEF消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆URL标记,其内容为" http://WEBSITE.com " .假设WEBSITE是youtube,所以 http://youtube.com .当我在Android等设备上扫描它们时,它会保留http或https.

I have a bunch of tags that are URL tags that have the content "http://WEBSITE.com". Let's say WEBSITE is youtube so http://youtube.com. When I scan them on Android etc, it keeps the http or https.

我正在尝试使用Core NFC框架扫描这些相同的标签.我扫描了它们,然后得到了一堆字节,这些字节是使用带有UTF8编码的NSSString initWithData进行转换的.我回来了\ ^ Cyoutube.com.我想获取 http://youtube.com .

I'm trying to scan these same tags using the Core NFC framework. I scan them and I get a bunch of bytes that I convert using NSSString initWithData with UTF8 Encoding. I get back \^Cyoutube.com. I want to get http://youtube.com.

如何截取有效载荷以获得所需的东西?如果我假设字符串前面是http,那么我应该如何知道它是http还是https甚至是ftp?

How can I interept the payload to get what I need? If I'm to assume the http in front of the string, how am I supposed to know if it is http or https or even ftp?

以下纯文本记录的答案代码存在问题.为"hello world"制作文本记录时,从控制台获得以下输出:

I'm having issues with the below answer code for pure text records. When making a text record for "hello world" I get the following outputs from the console:

2017-06-09 12:45:35.151806-0400 testNFC [2963:190724]有效负载字符串: https://www.enhello 世界

2017-06-09 12:45:35.151806-0400 testNFC[2963:190724] Payload string:https://www.enhello world

2017-06-09 12:45:35.154959-0400 testNFC [2963:190724]有效载荷数据:< 02656e68 656c6c6f 20776f72 6c64>

2017-06-09 12:45:35.154959-0400 testNFC[2963:190724] Payload data:<02656e68 656c6c6f 20776f72 6c64>

要获取我使用的字符串

NSString * nfcMessage = [nfcType stringByAppendingString:[[[[NSString alloc] initWithData:payload.payload encoding:NSUTF8StringEncoding] substringFromIndex:1]];

NSString *nfcMessage = [nfcType stringByAppendingString:[[[NSString alloc] initWithData:payload.payload encoding:NSUTF8StringEncoding] substringFromIndex:1]];

nfcType是函数的返回值,但对于None情况,我返回@";

nfcType is the return from your function but for the None case I return @"";

我希望与世界打个招呼.

I'm expecting to just get hello world.

推荐答案

为此,您首先需要确保已正确格式化NDEF标签.您可以使用Android手机或这些阅读器配件

For this, you will first need to make sure you have properly formatted NDEF tags. You can use an Android phone or one of these reader accessories along with an NDEF writing app.

实施以下方法:

- (NSString *)getType:(NSData *)NDEFData {

    NSString *firstByte = [self getFirstByte:NDEFData];

    if ([firstByte isEqualToString:@"00"]) {
        return @"None";
    } else if ([firstByte isEqualToString:@"01"]) {
        return @"http://www.";
    } else if ([firstByte isEqualToString:@"02"]) {
        return @"https://www.";
    } else if ([firstByte isEqualToString:@"03"]) {
        return @"http://";
    } else if ([firstByte isEqualToString:@"04"]) {
        return @"https://";
    } else if ([firstByte isEqualToString:@"05"]) {
        return @"tel:";
    } else if ([firstByte isEqualToString:@"06"]) {
        return @"mailto:";
    } else if ([firstByte isEqualToString:@"07"]) {
        return @"ftp://anonymous:anonymous@";
    } else if ([firstByte isEqualToString:@"08"]) {
        return @"ftp://ftp.";
    } else if ([firstByte isEqualToString:@"09"]) {
        return @"ftps://";
    } else if ([firstByte isEqualToString:@"0A"]) {
        return @"sftp://";
    } else if ([firstByte isEqualToString:@"0B"]) {
        return @"smb://";
    } else if ([firstByte isEqualToString:@"0C"]) {
        return @"nfs://";
    } else if ([firstByte isEqualToString:@"0D"]) {
        return @"ftp://";
    } else if ([firstByte isEqualToString:@"0E"]) {
        return @"dav://";
    } else if ([firstByte isEqualToString:@"0F"]) {
        return @"news:";
    } else if ([firstByte isEqualToString:@"10"]) {
        return @"telnet://";
    } else if ([firstByte isEqualToString:@"11"]) {
        return @"imap:";
    } else if ([firstByte isEqualToString:@"12"]) {
        return @"rtsp://";
    } else if ([firstByte isEqualToString:@"13"]) {
        return @"urn:";
    } else if ([firstByte isEqualToString:@"14"]) {
        return @"pop:";
    } else if ([firstByte isEqualToString:@"15"]) {
        return @"sip:";
    } else if ([firstByte isEqualToString:@"16"]) {
        return @"sips:";
    } else if ([firstByte isEqualToString:@"17"]) {
        return @"tftp:";
    } else if ([firstByte isEqualToString:@"18"]) {
        return @"btspp://";
    } else if ([firstByte isEqualToString:@"19"]) {
        return @"btl2cap://";
    } else if ([firstByte isEqualToString:@"1A"]) {
        return @"btgoep://";
    } else if ([firstByte isEqualToString:@"1B"]) {
        return @"tcpobex://";
    } else if ([firstByte isEqualToString:@"1C"]) {
        return @"irdaobex://";
    } else if ([firstByte isEqualToString:@"1D"]) {
        return @"file://";
    } else if ([firstByte isEqualToString:@"1E"]) {
        return @"urn:epc:id:";
    } else if ([firstByte isEqualToString:@"1F"]) {
        return @"urn:epc:tag:";
    } else if ([firstByte isEqualToString:@"20"]) {
        return @"urn:epc:pat:";
    } else if ([firstByte isEqualToString:@"21"]) {
        return @"urn:epc:raw:";
    } else if ([firstByte isEqualToString:@"22"]) {
        return @"urn:epc:";
    } else if ([firstByte isEqualToString:@"23"]) {
        return @"urn:nfc:";
    }

    return @"";

}

/*!
* gets the the NDEF content
*/
- (NSString *)getNDEFContent:(NSData *)data {
     NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
     return [dataString substringFromIndex:2];
}

/*!
 * gets the first byte of the input NSData
 */
- (NSString *)getFirstByte:(NSData *)data {
     return [[self dataToHexString:data] substringToIndex:2];
}

/*!
 * transforms NSData to NSString
 */
- (NSString *)dataToHexString:(NSData *)data;
{
    // get the length of the data
    NSUInteger bytesCount = data.length;
    if (bytesCount) {
        // string with all the Hex characters
        const char *hexChars = "0123456789ABCDEF";
        // put bytes into an array and initialize the response array
        const unsigned char *dataBuffer = data.bytes;
        char *chars = malloc(sizeof(char) * (bytesCount * 2 + 1));
        char *s = chars;
        // go through data bytes making the transformations so a hex will literally translate to a string, so for example 0x0A will translate to "0A"
        for (unsigned i = 0; i < bytesCount; ++i) {
            // get hexChars character at binary AND between the current byte and 0xF0 bitwise to the right by 4 index and assign it to the current chars pointer
            *s++ = hexChars[((*dataBuffer & 0xF0) >> 4)];
            // get hexChars character at binary AND between the current byte and 0x0F index and assign it to the current chars pointer
            *s++ = hexChars[(*dataBuffer & 0x0F)];
            dataBuffer++;
        }
        *s = '\0';
        // chars to string
        NSString *hexString = [NSString stringWithUTF8String:chars];
        free(chars);
        return hexString;
    }
    return @"";
}

并调用getType方法:

[self getType:yourNDEFPayloadNSData]

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆