天天看點

swift3.0擷取友盟device_token

使用swift3.0內建友盟推送卡在擷取device_token這一步上了

//正常OC代碼這樣擷取
在 didRegisterForRemoteNotificationsWithDeviceToken 中添加如下語句

NSLog(@"%@",[[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]
                  stringByReplacingOccurrencesOfString: @">" withString: @""]
                 stringByReplacingOccurrencesOfString: @" " withString: @""]);
           

而轉成swift3.0,一開始我是這麼寫的

let token: String = deviceToken.description.replacingOccurrences(of: "<", with: "").replacingOccurrences(of: ">", with: "").replacingOccurrences(of: " ", with: "")

//結果列印出來的值都是32bytes
           

網上找了好久才找到一個答案,感謝部落客

swift3更新後擷取deviceToken列印為32bytes處理

//将Data轉化為NSData就行了
let device = NSData(data: deviceToken)
let deviceId = device.description.replacingOccurrences(of:"<", with:"").replacingOccurrences(of:">", with:"").replacingOccurrences(of:" ", with:"")