Raghavendra
Raghavendra

Reputation: 5387

iOS8 - UILocalNotification, Check for permission later from code

I am writing my application in Swift for iOS8.

I wanted to use UILocalNotification to notify user of some things. For that, I have asked permission at the app launch with this:

application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: UIUserNotificationType.Sound | UIUserNotificationType.Alert | UIUserNotificationType.Badge, categories: nil))

A confirm box shows up asking for permission. If user allows it, there's no problem.

If the user does not give permission, how do I check if local notifications are enabled from code (programmatically)?

Upvotes: 2

Views: 3420

Answers (4)

kurtanamo
kurtanamo

Reputation: 1828

..and this is how you check it in Swift:

let currentSettings = UIApplication.sharedApplication().currentUserNotificationSettings()
let required:UIUserNotificationType = UIUserNotificationType.Sound; // Add other permissions as required
let notification = UILocalNotification()

    if (currentSettings.types & required) != nil {
        println("sound will come and also vibration")
        notification.soundName = UILocalNotificationDefaultSoundName
    }else{
        println("no sound no vibration")
    }

Nice to know:
it will vibrate automatically, whether sound is on/off over your iphone button - nothing will happen if the user will set sound off in settings for your app.
So you don't have to add vibration additionally.

Upvotes: 8

Bart van Kuik
Bart van Kuik

Reputation: 4862

For the record, this is how you check the permissions in Objective-C:

UIUserNotificationSettings *current = [[UIApplication sharedApplication] currentUserNotificationSettings];
UIUserNotificationSettings *required = UIUserNotificationTypeSound | UIUserNotificationTypeAlert; // Add other permissions as required
if(current.types & required) {
    NSLog(@"Permission present: 0x%ulX", settings.types);
} else {
    NSLog(@"Permission not present: 0x%ulX", settings.types);
}

Upvotes: 4

Raghavendra
Raghavendra

Reputation: 5387

From the documentation

application.currentUserNotificationSettings()

Upvotes: 0

dasdom
dasdom

Reputation: 14073

The documentation answers you question like this:

After calling this method, the app calls the application:didRegisterUserNotificationSettings: method of its app delegate to report the results. You can use that method to determine if your request was granted or denied by the user.

Upvotes: 2

Related Questions