iOS

iOS 13解析DeviceToken(包括swift和objective-c)

简介:iOS 13解析Apple apns 远程推送的deviceToken data发生了改变,这里记录下

iOS 13 Apple apns 远程推送获取的deviceToken data解析为String 记录:

下面为swift代码

// APNs注册成功
    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        print("Get deviceToken from APNs success.")

        // 将Data类型转化为hex类型的字符串
        let devieTokenString = deviceToken.reduce("", { $0 + String(format: "%02x", $1) })
        print(devieTokenString)
    }

下面为objective-c代码

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

    NSLog(@"DeviceToken :%@",deviceToken);

    NSMutableString *deviceTokenString = [NSMutableString string];
    const char *bytes = deviceToken.bytes;
    NSInteger iCount = deviceToken.length;
    for (int i = 0; i < iCount; i++) {
        [deviceTokenString appendFormat:@"%02x", bytes[i]&0x000000FF];
    }
    NSLog(@"deviceTokenString:%@", deviceTokenString);

}

推荐阅读

目录