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

Check if push enabled or location enabled ios sdk

我正在使用以下代码检测这 2 个(推送通知和定位服务)

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

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

但问题是,即使我在应用程序中提示时点击不允许,我也总是对两者都成立。同样在设置应用程序中,我检查了位置设置为从不,通知副标题显示。这段代码有什么问题?谁能在这方面指导我。

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

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

}

用于通知检查这个很棒

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

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

推送通知:

从 iOS 8.0 以上版本开始,即使用户选择退出推送,它也会注册设备并提供令牌。

但是,发送推送时不会向用户显示推送,因此它将 return 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);
    }
}

enabledRemoteNotificationTypes 自 iOS8.

以来已弃用

要检查 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
}