问题
I don't understand how to present the apns token as a string in view-controller. tried a lot of things and nothings worked out. in swift 4.
回答1:
1.
You can save it to UserDefaults as Halil suggested like so:
UserDefaults.standard.set(yourAPNSToken, forKey: "APNSToken")
And retrieve it inside your ViewController:
let token = UserDefaults.standard.string(forKey: "APNSToken")
2.
Or you can create new variable inside AppDelegate and access it from your VC
In your AppDelegate:
public var token = String()
token = yourAPNSToken
And inside your VC
let newToken = (UIApplication.shared.delegate as! AppDelegate).token
回答2:
Save Device token in UserDefaults from AppDelegate didregisterforremotenotificationswithdevicetoken then use in other viewController.
回答3:
Once your app register for APNS and you get DeviceToken as a Data in your didRegisterForRemoteNotificationsWithDeviceToken you can convert that data in to the string like this.
var token: String = deviceToken.description.trimmingCharacters(in: CharacterSet(charactersIn: "<>"))
token = token.replacingOccurrences(of: " ", with: "")
print("device token ---\(token)")
And if you want to store it in User default then use this.
UserDefaults.standard.set(token, forKey: "deviceToken")
来源:https://stackoverflow.com/questions/45459888/how-to-present-apns-token-in-view-controller