Calling `requestAuthorization(options:completionHandler:)` in Swift 6 leads to `EXC_BAD_INSTRUCTION` crash.

I am in the process of evaluating Swift 6 and I noticed that when using the completion handler version of the requestAuthorization the application crashes with EXC_BAD_INSTRUCTION exception.

Using this code:

let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound, .badge]) { success, error in
     print(success)
}

Crashes when the project is build with Swift 6 and works normalising when build with Swift 5.

The only solution that I have found so far is to switch to using the async version of that API:

Task {
            let center = UNUserNotificationCenter.current()
            do {
                if try await center.requestAuthorization(options: [.alert, .sound, .badge]) == true {
                    print("success")
                } else {
                    print("fail")
                }
                
            } catch {
                print("Error")
            }
        }

Is the a known issue?

I have submitted feedback with ID "FB15294185".

Answered by Amiorkov in 806929022

I will add my initial solution as a comment so it can be marked as solution to the question.

The only solution that I have found so far is to switch to using the async version of the requestAuthorization API:

Task {
    let center = UNUserNotificationCenter.current()
    do {
        if try await center.requestAuthorization(options: [.alert, .sound, .badge]) == true {
            print("success")
        } else {
            print("fail")
        }
        
    } catch {
        print("Error")
    }
}

We’re talking about this issue over on this thread.

I have submitted feedback with ID FB15294185.

Thanks for that. I’m off to go read that now (-:

Share and Enjoy

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

Accepted Answer

I will add my initial solution as a comment so it can be marked as solution to the question.

The only solution that I have found so far is to switch to using the async version of the requestAuthorization API:

Task {
    let center = UNUserNotificationCenter.current()
    do {
        if try await center.requestAuthorization(options: [.alert, .sound, .badge]) == true {
            print("success")
        } else {
            print("fail")
        }
        
    } catch {
        print("Error")
    }
}
Calling `requestAuthorization(options:completionHandler:)` in Swift 6 leads to `EXC_BAD_INSTRUCTION` crash.
 
 
Q