【问题标题】:Need assistance regarding UILocalNotification permissions需要有关 UILocalNotification 权限的帮助
【发布时间】:2015-11-12 06:53:24
【问题描述】:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

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

此代码用于注册UILocalNotification,这也会弹出:

问题1:在这种状态下,当用户没有选择任何选项时,当用户选择Don't AllowOk 选项之一时,我如何得到通知?所以我可以相应地执行应用程序。

--

UIUserNotificationSettings *current = [[UIApplication sharedApplication] currentUserNotificationSettings];
UIUserNotificationType required = UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
if(current.types & required) {
    NSLog(@"Permission present: %lu", (unsigned long)current.types);
} else {
    NSLog(@"Permission not present: %lu", (unsigned long)current.types);
}

当应用程序在第一次之后启动时,我正在尝试使用此代码获取用户允许的权限(也许他转到设置并禁用所有类型的通知警报)。

问题 2:我只是在 log 中获取数字,例如 7 用于我正在检查的权限类型,如果用户不允许 UILocalNotification,则为 0。如何正确检查权限?

【问题讨论】:

  • - (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings。这将在用户执行任何批准或拒绝操作时调用。
  • 要检查支持的类型,您可以检查 if( (types & UIRemoteNotificationTypeBadge) 。也就是说,您可以使用 UIUserNotificationSettings 的 types 属性对所需的类型进行按位与操作。

标签: ios objective-c uilocalnotification


【解决方案1】:

我在我的一个项目中使用以下方法来确定用户是否已授予权限,或者他/她是否真的关闭了设置中的通知。把这个方法放到Appdelegate里面检查一下

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

然后你可以简单地检查条件

if([self notificationServicesEnabled])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-23
    • 2014-12-26
    • 2013-08-12
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多