How can I convert my device token (NSData) into an NSString?

后端 未结 30 1990
挽巷
挽巷 2020-11-28 18:31

I am implementing push notifications. I\'d like to save my APNS Token as a String.

- (void)application:(UIApplication *)application
didRegisterForRemoteNotif         


        
30条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 19:30

    This is a little bit shorter solution:

    NSData *token = // ...
    const uint64_t *tokenBytes = token.bytes;
    NSString *hex = [NSString stringWithFormat:@"%016llx%016llx%016llx%016llx",
                     ntohll(tokenBytes[0]), ntohll(tokenBytes[1]),
                     ntohll(tokenBytes[2]), ntohll(tokenBytes[3])];
    

提交回复
热议问题