How to present apns token in view controller? [duplicate]

流过昼夜 提交于 2019-12-11 15:18:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!