user5357723
user5357723

Reputation:

not allowing alerts to be shown despite declaring them objective c ios 8

I'm trying to declare notifications in the style of alerts in Xcode 7.1 beta but I can't get it to work. I have tried multiple ways of doing it but I just cannot get them to be registered. The error log I'm getting is this:

Attempting to schedule a local notification {fire date = Wednesday, 23 September 2015 8:08:03 pm New Zealand Standard Time, time zone = (null), repeat interval = 0, repeat count = UILocalNotificationInfiniteRepeatCount, next fire date = (null), user info = (null)} with an alert but haven't received permission from the user to display alerts

This is the code I'm using:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    return YES;
    //

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

And these are the other ones I have tried to no more avail:

 [[UIApplication sharedApplication] registerUserNotificationSettings:    [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert) categories:nil]]; 

[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];

What should I do to solve this? Thanks.

Upvotes: 0

Views: 46

Answers (2)

Mukesh
Mukesh

Reputation: 3690

Add this piece of code above the return statement.After return the below lines don't get execute

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    


if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {

    [application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];     
}

return YES

 }

Upvotes: 1

Rony Rozen
Rony Rozen

Reputation: 4047

As the error message says, you're trying to schedule a local notification before you received permissions from the user to do so. The problem is that you put the code asking for permissions after the return call, so it never gets called. Your application:didFinishLaunchingWithOptions: should look like this:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) 
        {
            [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeSound|UIUserNotificationTypeBadge
                                                                                                                  categories:nil]];
        }

        return YES;
    }

Upvotes: 2

Related Questions