user2084676
user2084676

Reputation:

asking for notification permission swift (iOs 7)

How I can ask for permissions in iOs 8 and iOs7 both. I've tried this:

UIApplication.sharedApplication().registerForRemoteNotificationTypes(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert)

But it doesn't work.

That is my code, working only in iOS 8:

  if(UIApplication.instancesRespondToSelector(Selector("registerUserNotificationSettings:"))) {
            UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Alert | .Sound, categories: nil))
        }

        else {

        }

Upvotes: 1

Views: 2587

Answers (2)

Pung Worathiti Manosroi
Pung Worathiti Manosroi

Reputation: 1490

Some syntax has been changed lately. This is what works for me.

if(UIApplication.instancesRespondToSelector(#selector(UIApplication.registerUserNotificationSettings(_:)))) {
        UIApplication.sharedApplication().registerUserNotificationSettings(UIUserNotificationSettings(forTypes:[.Badge , .Sound , .Alert], categories: nil))
        UIApplication.sharedApplication().registerForRemoteNotifications()
}

Upvotes: 3

user3676554
user3676554

Reputation: 69

Swift:

//-- Set Notification
if application.respondsToSelector("isRegisteredForRemoteNotifications")
{
    // iOS 8 Notifications
    application.registerUserNotificationSettings(UIUserNotificationSettings(forTypes: .Badge | .Sound | .Alert, categories: nil))
    application.registerForRemoteNotifications()
}else{
    // iOS < 8 Notifications
    application.registerForRemoteNotificationTypes(.Badge | .Sound | .Alert)
}

Objective-C:

//-- Set Notification
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)]) 
{
       // iOS 8 Notifications
       [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];

       [application registerForRemoteNotifications];
}
else
{
      // iOS < 8 Notifications
      [application registerForRemoteNotificationTypes:
                 (UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound)];
}

Upvotes: 2

Related Questions