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

后端 未结 30 1686
挽巷
挽巷 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:14

    In iOS 13 the description will be in different format. Kindly use below code to fetch the device token.

    - (NSString *)fetchDeviceToken:(NSData *)deviceToken {
        NSUInteger len = deviceToken.length;
        if (len == 0) {
            return nil;
        }
        const unsigned char *buffer = deviceToken.bytes;
        NSMutableString *hexString  = [NSMutableString stringWithCapacity:(len * 2)];
        for (int i = 0; i < len; ++i) {
            [hexString appendFormat:@"%02x", buffer[i]];
        }
        return [hexString copy];
    }
    

提交回复
热议问题