Swift 获取device token

在做推送通知的时候发现一个尴尬的事情,在测试推送时候需要获取device token,按照友盟的文档,我做了如下操作
1
2
3
4
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenStr = deviceToken.description.replacingOccurrences(of: "<", with: "").replacingOccurrences(of: ">", with: "").replacingOccurrences(of: " ", with: "")
print(tokenStr)
}

1.png

打印出来什么鬼。。。
看了一下发现deviceToken参数和OC中的类型不相同,Data、NSData之分,同时使用description输出却的不同,果断这样处理,中间转换一下
1
2
let deviceTokenData = NSData.init(data: deviceToken)
let tokenStr = deviceTokenData.description.replacingOccurrences(of: "<", with: "").replacingOccurrences(of: ">", with: "").replacingOccurrences(of: " ", with: "")