Request authorization for the notification center crash iOS app on Swift 6

Hey all!

During the migration of a production app to swift 6, I've encountered a problem: when hitting the UNUserNotificationCenter.current().requestAuthorization the app crashes.

If I switch back to Language Version 5 the app works as expected.

The offending code is defined here

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        FirebaseConfiguration.shared.setLoggerLevel(.min)

        UNUserNotificationCenter.current().delegate = self

        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { _, _ in }

        application.registerForRemoteNotifications()

        Messaging.messaging().delegate = self

        return true
    }
}

The error is depicted here: I have no idea how to fix this.

Any help will be really appreciated

thanks in advance

This crashes all the time, right?

If so, please generate a crash report and post it here. See Posting a Crash Report for advice on how to do that.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Here the .ips crash report, with .txt extension as suggested in your post.

Thanks for the crash report.

Consider this snippet:

@MainActor var counter = 0

@main
class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions …) -> Bool {
        counter += 1
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(options: authOptions) { _, _ in
            counter += 1
       }
        return true
    }
    …
}

I’m compiling this with Xcode 16.0 in the Swift 6 language mode.

The increments of counter show that Swift thinks that both application(_:didFinishLaunchingWithOptions:) and the closure are supposed to be running on the main actor. However, the closure called by requestAuthorization(options:completionHandler:) is documented to not run there. I’d expect that the compiler would insert code to ‘bounce’ to the main actor, but instead it inserted code to trap if it’s not on the main actor.

I’m not sure why it thinks that in this context. Oh, and I checked with the latest Xcode 16.1 beta, and it has the same issue.

I’m gonna do some more research about this (FB15294185) but, for the moment, you can avoid the crash by calling the Swift async function variant of the API:

let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
Task {
    do {
        _ = try await UNUserNotificationCenter.current().requestAuthorization(options: authOptions)
        print("here")
    } catch {
        print("there")
    }
}

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I can confirm that your workaround is working and the app is not crashing anymore. I'll check for future resolution of the problem.

Thanks Quinn!

Request authorization for the notification center crash iOS app on Swift 6
 
 
Q