-1

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.

3 Answers3

1

Save Device token in UserDefaults from AppDelegate didregisterforremotenotificationswithdevicetoken then use in other viewController.

Jugal K Balara
  • 917
  • 5
  • 15
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
Phyber
  • 1,368
  • 11
  • 25
0

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")
Pankaj K.
  • 535
  • 8
  • 18