0

The IOS app that I'm building uses push notifications. Apple requires you to ask the user wether they want this or not, the first time they run the app:

if([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}

But, the user could change his/her mind during the use of the application and toggle notifications on or off in a settings controller by use of a UISwitch.

How can I catch the current notifications value and register/unregister the user from the notifications? This is what I have found on the internet but doesn't seem to work:

[[UIApplication sharedApplication] registerForRemoteNotifications];
[[UIApplication sharedApplication] unregisterForRemoteNotifications];
[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
Caleb
  • 124,013
  • 19
  • 183
  • 272
user2381011
  • 351
  • 6
  • 21

1 Answers1

2

You don't have to manually unregister the user from notifications if he changes the preferences, but you can check the status any time with this code

if ([[UIApplication sharedApplication]  respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) {
    if ([[UIApplication sharedApplication]  isRegisteredForRemoteNotifications]){
        NSLog(@"Notifications Enabled iOS 8");
    } else {
        NSLog(@"Notifications Not Enabled iOS 8");
    }
} else {
    UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (types & UIRemoteNotificationTypeAlert) {
        NSLog(@"Notifications Enabled iOS 7 or older");
    } else {
        NSLog(@"Notifications Not Enabled iOS 7 or older");
    }
}
jcesarmobile
  • 51,328
  • 11
  • 132
  • 176