Rool Paap
Rool Paap

Reputation: 1976

How to check notification permissions on the main thread in swift?

I've got a function in my code to check if we can have permission to display a UILocalNotification.

open func hasPermission() -> Bool {
    if let permissions = UIApplication.shared.currentUserNotificationSettings {
        if permissions.types.contains(.alert) {
            return true
        }
    }
    return false
}

This code has triggered a warning in Xcode 9:

UI API called from background thread: UIApplication.currentUserNotificationSettings must be used from main thread only

How do I fix this? I know there is the DispatchQueue.main.async method, just not sure how to implemented that.

Upvotes: 0

Views: 610

Answers (1)

Bilal
Bilal

Reputation: 19156

You can do it like this.

DispatchQueue.main.async {
    if hasPermission() {
        //
    }
}

Upvotes: 1

Related Questions