didRegisterForRemoteNotificationsWithDeviceToken is nil

hi,


since Xcode beta 6 Appdelegate method after register

Data with Token is nil,but Data contains 32Bytes so i need my token


my register in

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {

let center = UNUserNotificationCenter.current()

center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in

}

UIApplication.shared.registerForRemoteNotifications()


return true

}

is done

// delegate call function

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {

// deviceToken contains 32Bytes but i want

// a String like this

// <9f26d46d c37a473c 4dd209d5 8684fb62 bfedbe66 24dca86b 885ac856 68521db9>

// any cast dit not work to String



// print(deviceToken.debugDescription) is nil

print(deviceToken.description) is nil


let token = String(data: deviceToken), encoding: String.Encoding.utf8)


// token is nil

print (token) //is nil



// for test only

print(deviceToken.base64EncodedString()) // containsData


//doSomethingWithToken(token)



}


thanks

Replies

If you need a string with hex bytes, you need to convert the data yourself.


Dirk

yeah Delegate method call and the Data from Token contains 32bytes (in debug View)

but i can't convert the Data,the Data is nil.


I have the same problem with special Character in my plist in String.

my workaround is save the String (complete Plist) to local Device and

read plist again,that works great...


it's the same procedure ..the result Data contains Data,create directly NSDictionary

with this Data will fail...while unwrapping an Optional...


but save the Data as file and Load again that works...

strange...


its only for special Characters in my Plist....


but this workaround will not work for my token

this works....



extension Data {

func hexString() -> String {

return self.reduce("") { string, byte in

string + String(format: "%02X", byte)

}

}

}