Has the device token format provided by APN been changed in my application?

I don’t know why ... but my previous functional push notification registration callback returns a strange device token. Can anyone help figure this out? As far as I know, I have not changed any code regarding this process.

The following code:

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

It returns me this result:

 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 anyone know this format or have an idea of ​​what is going on?

UPDATE Oddly enough, it seems that my device token is indeed contained in the [devToken description] if I extract each : and the character following it .... and I assume that [32]: is just an indicator of the length of the string. I still can not find any reason for this.

Paraphrased question: Has the [NSData description] output format changed?

+4
source share
1 answer

You should not rely on the NSData description method (which is actually the NSObject description method) to provide the same results from each version of iOS to the next. Apple may change what this description displays.

The device token is actually HEX in NSData format. You need to convert 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 in 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 
+8
source

Source: https://habr.com/ru/post/1434064/


All Articles