ios - swift3.0 device token转换问题

查看:176
本文介绍了ios - swift3.0 device token转换问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

swift3.0调用注册方法
// 注册获得device Token

    UIApplication.shared.registerForRemoteNotifications()

收到回调通知
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

    
    let device = deviceToken.description.replacingOccurrences(of: "<", with: "").replacingOccurrences(of: ">", with: "").replacingOccurrences(of: " ", with: "")
    print(device)
    print("Device Token:\(String(describing: deviceToken))")
}

然而打印的数据一直是
32bytes
Device Token:32 bytes

难道我上面的转换方式写错了吗 为什么不应该是一个正常的字符串的系列号之类
PS:OC写法
NSString *deviceTokenString2 = [[[[deviceTokendescription] stringByReplacingOccurrencesOfString:@"<"withString:@""]

                                                            stringByReplacingOccurrencesOfString:@">" withString:@""]

                                                            stringByReplacingOccurrencesOfString:@" " withString:@""];

NSLog(@"方式2:%@", deviceTokenString2);

解决方案

看样子,大家都没人遇到,或者太简单没人理会。
不过最后还是找到答案了,
转换方法没错,只是
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
返回的 deviceToken有点问题 不知道是苹果的问题 亦或者怎么样
不过只要补充一句
let nsdataStr = NSData.init(data: deviceToken)
重新实例化一份
然后执行
let datastr = nsdataStr.description.replacingOccurrences(of: "<", with: "").replacingOccurrences(of: ">", with: "").replacingOccurrences(of: " ", with: "")

     print("deviceToken:\(datastr)")

就能输出想要的结果了

这篇关于ios - swift3.0 device token转换问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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