Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 256
Raghavendra Avatar asked Jan 19 '26 14:01

Raghavendra


1 Answers

..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.

like image 122
kurtanamo Avatar answered Jan 21 '26 07:01

kurtanamo