如何检测以前是否显示过位置服务弹出窗口

时间:2016-08-04 15:06:15

标签: ios objective-c swift xcode core-location

当我第一次运行locationManager.requestWhenInUseAuthorization()时,标准"允许AppName在您使用应用程序时访问您的位置"弹出,永远不再基于标准的IOS设计(无论用户是否选择允许不允许)。以下代码位于我的主视图控制器

中的ViewDidLoad中
// Check location status
    if locationAuthStatus == CLAuthorizationStatus.AuthorizedWhenInUse {
        self.displayMessage.hidden = false
        self.displayMessage.text = "Waiting for GPS Signal..."

    } else {

        locationManager.requestWhenInUseAuthorization()
    }

我遇到的问题是,如果用户退出程序,禁用位置服务,然后返回,则没有弹出窗口要求用户启用已显示的位置。因此,如果以下两个条件为真,我想添加另一个自定义弹出窗口以请求许可

  1. 以前显示过初始弹出窗口
  2. 未启用位置服务。
  3. 最初我在locationManager.requestWhenInUseAuthorization()之后得到了我的弹出代码。但是,这导致了一个问题。如果用户第一次使用该应用程序,系统会提示他使用默认弹出窗口,然后就会弹出我的弹出窗口...

    谢谢,

2 个答案:

答案 0 :(得分:4)

您可以查看authorizationstatus以了解弹出窗口是否已弹出。

CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
//Status possible:
// User has not yet made a choice with regards to this application
    kCLAuthorizationStatusNotDetermined = 0,

    // This application is not authorized to use location services.  Due
    // to active restrictions on location services, the user cannot change
    // this status, and may not have personally denied authorization
    kCLAuthorizationStatusRestricted,

    // User has explicitly denied authorization for this application, or
    // location services are disabled in Settings.
    kCLAuthorizationStatusDenied,

    // User has granted authorization to use their location at any time,
    // including monitoring for regions, visits, or significant location changes.
    kCLAuthorizationStatusAuthorizedAlways NS_ENUM_AVAILABLE(NA, 8_0),

    // User has granted authorization to use their location only when your app
    // is visible to them (it will be made visible to them if you continue to
    // receive location updates while in the background).  Authorization to use
    // launch APIs has not been granted.
    kCLAuthorizationStatusAuthorizedWhenInUse NS_ENUM_AVAILABLE(NA, 8_0),

    // This value is deprecated, but was equivalent to the new -Always value.
    kCLAuthorizationStatusAuthorized NS_ENUM_DEPRECATED(10_6, NA, 2_0, 8_0, "Use kCLAuthorizationStatusAuthorizedAlways") __TVOS_PROHIBITED __WATCHOS_PROHIBITED = kCLAuthorizationStatusAuthorizedAlways

如果您获得 kCLAuthorizationStatusNotDetermined ,则表示未显示弹出窗口(用户未做出选择)。然后,根据authorizationstatus,您可以将用户定向到设置屏幕或继续更新位置。

PS:您应该实现下面的代理,并在startUpdatingLocation上调用CLLocationManager时(当弹出窗口没有弹出时弹出弹出窗口时),看看它是如何工作的。

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status

答案 1 :(得分:0)

Swift 中,此属性现在是枚举。您可以查看:

if CLLocationManager.authorizationStatus() == .notDetermined {
    //popup was not shown
}