简体   繁体   中英

Swift/Obtaining a Device Token in iOS

I'm brand new to iOS development and I'm trying to figure out how to obtain a device token for iOS. I've been following the documentation as written for Swift in listing 4-1 here: https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/HandlingRemoteNotifications.html

On this line: self.configureUserInteractions() (or any line referencing "self"), I'm receiving an error which says: Value of type 'AppDelegate' has no member 'configureUserInteractions'. I'm thinking maybe I'm missing an import statement in my AppDelegate.swift file, but I cant find any information on this method.

self.configureUserInteractions() is a method that Apple uses an example. Completely unnecessary to implement. func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) is all you need to get the device token.

The method configureUserInteractions should be a custom method of your AppDelegate , so you have to implement it first, but if you just need to get a remote notification token (for Push Notifications) simply implement UIApplicationDelegate this protocol method in your AppDelegate class:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    print(deviceToken.reduce("", {$0 + String(format: "%02X", $1)})) // prints deviceToken
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM