如何在应用中呈现apns令牌?在SWIFT 4

问题描述:

如何的appdelegate使用函数从视图控制器?在迅速4.如何在应用中呈现apns令牌?在SWIFT 4

let viewController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ViewController") as! ViewController 

viewController.setapnsText(token: deviceTokenString) 
public func setapnsText(token: String) -> Void { 
      apnsToken.text=token 
} 

我试过了,它不起作用。

+1

可以澄清你想达到的结果吗? –

+1

不要转发问题。我将你的[上一个问题](https://*.com/questions/45459888/how-to-present-apns-token-in-view-controller)标记为重复。在链接的问题有很多的方式来获得设备令牌作为字符串,这个作品甚至可能在斯威夫特4.除此之外,目前还不清楚你要完成的任务。 – vadian

+0

您可以将访问令牌存储在用户默认值中。 – Jaydeep

只能收到的设备标识上的每个发射如果当用户/授予您的应用程序权限推送通知。此外,这通常可以在AppDelegate中轻松捕获。 (推荐更多的,因为你可以,如果你启用了您的应用程序利用本地加密)

所以这里我举的例子,我保存到两个UserDefaults(较少推荐),核心数据。

您请求授权,像这样,对于应用其目的是要在iOS上10 +工作并请求推送通知在发射..:

class AppDelegate: UIResponder, UIApplicationDelegate { 


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

     notificationHandler(application) 
     return true 

    } 


    func notificationHandler(_ application: UIApplication) { 
     if #available(iOS 10.0, *) { 
      let center = UNUserNotificationCenter.current() 
      center.getNotificationSettings(){ (settings) in 
       switch settings.authorizationStatus { 
       case .authorized: 
        print("Authorized Push Notifications by User") 
        self.registerPushNotifications() 
       case .denied: 
        print("show user a view explaining why it's better to enable") 
        self.registerPushNotifications() 
       case .notDetermined: 
        self.requestPushNotifications(center: center, { (granted) in 
         if granted { 
          self.registerPushNotifications() 
          return 
         } 
         print("User did not grant Remote Notifications Authorizations") 
        }) 
       } 
      } 
     } else { 
      print("App does not meet minimum OS Requirements") 
      return 
     } 
    } 

    fileprivate func requestPushNotifications(center: UNUserNotificationCenter,_ result: @escaping(Bool)->Void) { 
     center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in 
      if error != nil { 
       print("Could not request Authorization for Notifications - Yet is undetermined") 
      } else { 
       result(granted) 
      } 
     } 
    } 

    func registerPushNotifications() { 
      if #available(iOS 10.0, *) { 

       UIApplication.shared.registerForRemoteNotifications() 

      } else { 
       print("App does not meet base OS requirements") 
      } 
    } 

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
      let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)}) 
      UserDefaults.standard.set(deviceTokenString, forKey: "devicetoken") 
      coreDataManager.saveDeviceToken(deviceTokenString) 
     } 

} 

最后,如果你只想在这个请求在您的应用程序的其他时刻,你可以扩展NSObject,因为它是既AppDelegateUIViewController的超类(也就是根类的所有控制器型类的反正。

class AppDelegate: UIResponder, UIApplicationDelegate { 


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

     notificationHandler(application) 
     return true 

    } 

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { 
      let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)}) 
      UserDefaults.standard.set(deviceTokenString, forKey: "devicetoken") 
      coreDataManager.saveDeviceToken(deviceTokenString) 
     } 

} 

extension NSObject { 

    func notificationHandler(_ application: UIApplication) { 
     if #available(iOS 10.0, *) { 
      let center = UNUserNotificationCenter.current() 
      center.getNotificationSettings(){ (settings) in 
       switch settings.authorizationStatus { 
       case .authorized: 
        print("Authorized Push Notifications by User") 
        self.registerPushNotifications() 
       case .denied: 
        print("show user a view explaining why it's better to enable") 
        self.registerPushNotifications() 
       case .notDetermined: 
        self.requestPushNotifications(center: center, { (granted) in 
         if granted { 
          self.registerPushNotifications() 
          return 
         } 
         print("User did not grant Remote Notifications Authorizations") 
        }) 
       } 
      } 
     } else { 
      print("App does not meet minimum OS Requirements") 
      return 
     } 
    } 

    fileprivate func requestPushNotifications(center: UNUserNotificationCenter,_ result: @escaping(Bool)->Void) { 
     center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in 
      if error != nil { 
       print("Could not request Authorization for Notifications - Yet is undetermined") 
      } else { 
       result(granted) 
      } 
     } 
    } 

    func registerPushNotifications() { 
     if #available(iOS 10.0, *) { 

      UIApplication.shared.registerForRemoteNotifications() 

     } else { 
      print("App does not meet base OS requirements") 
     } 
    } 

} 

然后,您可以拨打这里面一个按钮的链接的方法如下:

class SomeController : UIViewController { 

    override viewDidLoad() { 
     super.viewDidLoad() 
    } 

    func requestNotificationsAuthorization() { 
     notificationHandler(UIApplication.current) 
    } 

}