I am implementing push notifications. I'd like to save my APNS Token as a String.
- (void)application:(UIApplication *)application
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)newDeviceToken
{
NSString *tokenString = [NSString stringWithUTF8String:[newDeviceToken bytes]]; //[[NSString alloc]initWithData:newDeviceToken encoding:NSUTF8StringEncoding];
NSLog(@"%@", tokenString);
NSLog(@"%@", newDeviceToken);
}
The first line of code prints null. the second prints the token. How can I get my newDeviceToken as an NSString?
This is a little bit shorter solution:
Functional Swift version
One liner:
Here's in a reusable and self documenting extension form:
Alternatively, use
reduce("", combine: +)
instead ofjoinWithSeparator("")
to be seen as a functional master by your peers.Edit: I changed String($0, radix: 16) to String(format: "%02x", $0), because one digit numbers needed to having a padding zero
(I don't know yet how to mark a question as a duplicate of this other one, so I just posted my answer again)
It's my solution and It works well in my app:
NSData
toNSString
withstringWithFormat
Use excellent category!
// .h file
// .m file
@end
// AppDelegate.m
Works fine!