尽管声明了警报,但不允许显示警报 objective c ios 8

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

我试图在 Xcode 7.1 beta 中以警报的形式声明通知,但我无法让它工作。我尝试了多种方法,但就是无法让它们注册。我得到的错误日志是这样的:

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

这是我正在使用的代码:

    - (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]];
    }

这些是我尝试过但无济于事的其他方法:

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

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

我应该怎么做才能解决这个问题?谢谢。

如错误消息所述,您试图在收到用户的许可之前安排本地通知。问题是您将请求权限的代码放在 return 调用之后,因此它永远不会被调用。您的 application:didFinishLaunchingWithOptions: 应如下所示:

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

        return YES;
    }

return statement.After return 上面添加这段代码,下面的行不会执行

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


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

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

return YES

 }