如何将我的设备令牌 (NSData) 转换为 NSString? [英] How can I convert my device token (NSData) into an NSString?

查看:30
本文介绍了如何将我的设备令牌 (NSData) 转换为 NSString?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实施推送通知.我想将我的 APNS 令牌保存为字符串.

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);
}

第一行代码打印空值.第二个打印令牌.如何将我的 newDeviceToken 作为 NSString 获取?

The first line of code prints null. the second prints the token. How can I get my newDeviceToken as an NSString?

推荐答案

注意 - 这在使用 iOS 13 或更高版本的 SDK 编译时不起作用

使用这个:

NSString * deviceTokenString = [[[[deviceToken description]
                         stringByReplacingOccurrencesOfString: @"<" withString: @""] 
                        stringByReplacingOccurrencesOfString: @">" withString: @""] 
                       stringByReplacingOccurrencesOfString: @" " withString: @""];
        
NSLog(@"The generated device token string is : %@",deviceTokenString);

这篇关于如何将我的设备令牌 (NSData) 转换为 NSString?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆