尽管声明它们是目标 c ios 8,但不允许显示警报

Posted

技术标签:

【中文标题】尽管声明它们是目标 c ios 8,但不允许显示警报【英文标题】:not allowing alerts to be shown despite declaring them objective c ios 8 【发布时间】:2015-09-23 09:42:19 【问题描述】:

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

尝试安排本地通知 触发日期 = 2015 年 9 月 23 日星期三晚上 8:08:03 新西兰标准时间,时区 = (null),重复间隔 = 0,重复计数 = UILocalNotificationInfiniteRepeatCount,下一个触发日期 = (null), user info = (null) 带有警报但未获得用户显示警报的权限

这是我正在使用的代码:

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

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

【问题讨论】:

"返回是;"应该在最后(在 if 条件之后) 【参考方案1】:

正如错误消息所述,您尝试在获得用户许可之前安排本地通知。问题是您在返回调用之后放置了请求权限的代码,因此它永远不会被调用。你的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;
    

【讨论】:

【参考方案2】:

return 语句上方添加这段代码。在return 之后,以下行不会被执行

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


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

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


return YES

 

【讨论】:

以上是关于尽管声明它们是目标 c ios 8,但不允许显示警报的主要内容,如果未能解决你的问题,请参考以下文章

尽管使用 POST,但不允许使用 Flask- 方法 [重复]

尽管设置了标头,但不允许 CORS 请求

如何让 iOS 在 tableView 上显示删除按钮,但不允许从右侧完全滑动删除

尽管添加了 android:usesCleartextTraffic 和 android:networkSecurityConfig,但不允许明文 HTTP 流量

XCode,目标 C - 键盘不会显示

MPMoviePlayerViewController 仅显示视频但不播放音频 iOS 8