I think this is the best code to convert an APNs token to String from Data.
let token = deviceToken.map { String(format: "%.2hhx", $0) }.joined()
Usage
// Define initializer
extension String {
public init(deviceToken: Data) {
self = deviceToken.map { String(format: "%.2hhx", $0) }.joined()
}
}
func application(_ application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data!) {
// Use like this:
let token = String(deviceToken: deviceToken)
}
Full Japanese version: Swiftใงใใใทใฅ้็ฅ็จใฎDevice Token(Dataๅ)ใ16้ฒๆฐๆๅญๅใธๅคๆใใๆนๆณ - Qiita
Top comments (0)