【问题标题】:Check if push enabled or location enabled ios sdk检查是否启用推送或启用位置 ios sdk
【发布时间】:2017-01-05 06:37:19
【问题描述】:

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

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

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

但问题是,即使我在应用程序中出现提示时点击“不允许”,我也总是对两者都正确。同样在设置应用程序中,我检查了位置设置为从不并且通知子标题显示。这段代码有什么问题?谁能指导我。

【问题讨论】:

    标签: ios push-notification cllocationmanager xcode7.2


    【解决方案1】:

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

    推送通知:

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

    但是,推送发送时不会向用户呈现推送,因此它将返回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
    }
    

    【讨论】:

      【解决方案2】:

      仅检查[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"];
      }
      

      【讨论】:

      • 为了您的信息,文档说 /* * locationServicesEnabled * * Discussion: * 确定用户是否启用了位置服务。 * 如果否,并且您继续调用其他 CoreLocation API,用户将收到警告 * 对话框提示。您可能希望仅在用户明确请求时检查此属性并使用位置服务。 */
      • 理想情况下它应该返回 false。你不觉得吗?
      • 是的。同意你的观点。它必须返回 false。你在设备上试过吗?
      • 添加了您的代码,它可以工作,但 Push 仍然没有运气。谢谢
      • 对于通知,请尝试检查enabledRemoteNotificationTypes,如我更新的答案中所述。
      猜你喜欢
      • 1970-01-01
      • 2019-01-21
      • 2016-12-01
      • 2017-05-09
      • 1970-01-01
      • 1970-01-01
      • 2014-07-26
      • 2023-03-23
      • 2021-03-07
      相关资源
      最近更新 更多