Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Did the format of the Device Token provided by APNs suddenly changed in my app?

Not sure why... but my previously functional push notification registration callback is returning a strange device token. Can anyone help make any sense of it? To my knowledge, I haven't changed any code regarding this process.

The following code:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken
{
    NSLog(@"device token: %@", [devToken description]);
}

Returns me this output:

device token: [32]: 8d 21:! 30:0 c3 ec 69:i f4 <--REDACTED--> 24:$ d5 26:& 64:d fb 27:' 79:y fc dc 10 ae 77:w b0 21:! 5b:[ 

Does anybody recognize this format or have any idea what is going on?

UPDATE Strangely enough, it seems that my device token is actually contained in [devToken description] if I extract every : and character following it.... and I'm guessing that [32]: is just an indicator of the length of the String. I still can't find any reasoning for this though.

Rephrased Question: Did the [NSData description] output format change?

like image 980
achan Avatar asked Feb 19 '23 04:02

achan


1 Answers

You shouldn't rely on the description method of NSData (which is actually the description method of NSObject) to provide the same results from each iOS version to the next. Apple can change what this description outputs.

The device token is actually HEX in NSData format. You need to convert the NSData. You can use something like the following:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    {

        [[NSUserDefaults standardUserDefaults] setObject:[deviceToken stringWithHexBytes] forKey:@"DeviceToken"];
    }

The stringWithHexBytes method is a category on NSData as follows:

NSData+Hex.h

    @interface NSData (Hex)

- (NSString *) stringWithHexBytes; 


@end

NSData+Hex.m

    #import "NSData+Hex.h"

@implementation NSData (Hex)

- (NSString*) stringWithHexBytes 
{
    NSMutableString *stringBuffer = [NSMutableString stringWithCapacity:([self length] * 2)];
    const unsigned char *dataBuffer = [self bytes];

    for (int i = 0; i < [self length]; ++i)
    {
        [stringBuffer appendFormat:@"%02X", (unsigned long)dataBuffer[ i ]];
    }

    return [[stringBuffer retain] autorelease];
}

@end
like image 50
radesix Avatar answered Apr 28 '23 14:04

radesix