RequestAuthorization for push outside the didFinishLaunchingWithOptions

For ios 10 i used this for registering the push notifications :

Registering for Push Notifications in Xcode 8/Swift 3.0?

Is there a way to ask for the requestAuthorization(options:[.badge, .alert, .sound]) outside the appdelegate and the func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool

The reason i ask is because i don't want to present the pop up for push notifications after the user has used the app for a bit. Any ideas?


Like @dan said it isn't necessary to request the notifications permission in the AppDelegate . You can do it wherever you want to. This is what you probably be doing for that.

let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .badge, .sound]) { (success, error) in
    if error == nil {
        if success == true {
            print("Permission granted")
            // In case you want to register for the remote notifications
            let application = UIApplication.shared
            application.registerForRemoteNotification
        }
        else {
            print("Permission denied")
        }
    else {
        print(error)
    }
}

And Remember

  • to import the UserNotifications framework where you use this code.
  • if you register for remote notifications you need to implement the didRegisterForRemoteNotificationsWithDeviceToken method in your AppDelegate
  • 链接地址: http://www.djcxy.com/p/34876.html

    上一篇: 如何将按钮添加到自定义通知并相应地响应按钮?

    下一篇: RequestAuthorization用于在didFinishLaunchingWithOptions之外进行推送