检查是否已启用推送或已启用位置ios sdk

时间:2016-08-29 09:24:41

标签: ios push-notification cllocationmanager xcode7.2

我使用以下代码来检测这些2(推送通知和位置服务)

    [postvars setObject:([CLLocationManager locationServicesEnabled])?@"true":@"false" forKey:@"locationServicesEnabled"];

    BOOL pushEnabled = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
    [postvars setObject:(pushEnabled)?@"true":@"false" forKey:@"pushServicesEnabled"];

但问题是,即使我在应用程序中出现提示时点按“不允许”,我也会始终认同这两个问题。同样在设置应用中,我检查了“位置”设置为“从不”,并且通知子标题显示为关闭。这段代码有什么问题?任何人都可以指导我。

2 个答案:

答案 0 :(得分:3)

仅检查[CLLocationManager locationServicesEnabled]是不够的。

if([CLLocationManager locationServicesEnabled] && 
   [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
    [postvars setObject:@"true" forKey:@"locationServicesEnabled"];

}

通知检查这个真棒SO answer

BOOL pushEnabled = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
[postvars setObject:(pushEnabled)?@"true":@"false" forKey:@"pushServicesEnabled"];

UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone) 
   {
    [postvars setObject:@"false" forKey:@"pushServicesEnabled"];
}

答案 1 :(得分:2)

检查是否已启用推送通知或已启用位置服务:

推送通知:

从iOS 8.0以上,它会注册设备并提供令牌,即使用户选择退出也是如此。

但是,当发送推送时,不会向用户显示推送,因此它将返回true

- (BOOL)isPushNotificationsEnabled {
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]) {
        UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];
        return (types & UIUserNotificationTypeAlert);
    }
    else {
        UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        return (types & UIRemoteNotificationTypeAlert);
    }
}
自iOS8以来,

enabledRemoteNotificationTypes已被弃用。

要在iOS8中检查远程通知状态,您可以使用:

[[UIApplication sharedApplication] isRegisteredForRemoteNotifications];

在此处查看答案:detect “Allow Notifications” is on/off for iOS8

在这里阅读文档:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplication_Class/index.html#//apple_ref/occ/instm/UIApplication/currentUserNotificationSettings

另请阅读此处的主题:https://forums.developer.apple.com/thread/16958

位置服务:

需要同时检查locationServicesEnabledauthorizationStatus

if([CLLocationManager locationServicesEnabled] && 
   [CLLocationManager authorizationStatus] != kCLAuthorizationStatusDenied)
{
   //Enabled
}
相关问题