首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >远程化类型的iOS8检查权限

远程化类型的iOS8检查权限
EN

Stack Overflow用户
提问于 2014-08-29 14:08:21
回答 5查看 6.9K关注 0票数 4

在iOS8之前,我可以检查用户是否获得通知(警报)权限,比如:

代码语言:javascript
运行
复制
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types & UIRemoteNotificationTypeAlert)
{
    //user granted
}

它没有在iOS8上工作,它说:

代码语言:javascript
运行
复制
iOS (3.0 and later) Deprecated:Register for user notification settings using the  registerUserNotificationSettings: method instead.

控制台说:

代码语言:javascript
运行
复制
enabledRemoteNotificationTypes is not supported in iOS 8.0 and later.

那么如何在iOS 8上检查呢?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2014-09-03 11:05:02

好吧,我这样解决了我的问题:

代码语言:javascript
运行
复制
BOOL isgranted = false;

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
    {
        isgranted =  [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
    }
#else
    UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    if (types & UIRemoteNotificationTypeAlert)
    {
        isgranted = true;
    }
#endif
票数 9
EN

Stack Overflow用户

发布于 2014-10-02 15:56:11

为了更有活力,我扩展了伍哈斯的回答。

代码语言:javascript
运行
复制
 - (BOOL)pushNotificationsEnabled
{

    BOOL isgranted = false;

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
        if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
        {
            isgranted =  [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
        }else{

        }
    }else{
        UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if (types & UIRemoteNotificationTypeAlert)
        {

           isgranted = true;
        }else{

        }
    }
    return isgranted;

}
票数 4
EN

Stack Overflow用户

发布于 2015-03-05 10:53:56

我在使用isRegisteredForNotifications时运气不佳。最后,我转而使用了currentUserNotificationSettings

代码语言:javascript
运行
复制
+ (BOOL)notificationServicesEnabled {
    BOOL isEnabled = NO;

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]){
        UIUserNotificationSettings *notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];

        if (!notificationSettings || (notificationSettings.types == UIUserNotificationTypeNone)) {
            isEnabled = NO;
        } else {
            isEnabled = YES;
        }
    } else {
        UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if (types & UIRemoteNotificationTypeAlert) {
            isEnabled = YES;
        } else{
            isEnabled = NO;
        }
    }

    return isEnabled;
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25570015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档