Pavlos Pavlos - 9 months ago 56
iOS Question

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?

Answer Source

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
        else {
            print("Permission denied")
    else {

And Remember

  1. to import the UserNotifications framework where you use this code.
  2. if you register for remote notifications you need to implement the didRegisterForRemoteNotificationsWithDeviceToken method in your AppDelegate