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

use*_*902 0 xcode objective-c core-location ios swift

当我第一次运行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()
    }
Run Code Online (Sandbox Code Playgroud)

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

  1. 初始弹出窗口先前已显示
  2. 位置服务未启用。

最初,我在locationManager.requestWhenInUseAuthorization()之后获得了弹出代码。但是,这引起了问题。如果用户是第一次使用该应用,则系统会提示他默认的弹出窗口,然后我的弹出窗口便会出现。

谢谢,

Duy*_*Hoa 5

您可以检查授权状态以了解弹出窗口是否已弹出。

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
Run Code Online (Sandbox Code Playgroud)

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

PS:你应该执行下面的委托,看看当你调用它是如何工作startUpdatingLocation的你CLLocationManager(当弹出窗口没有弹出,当弹出弹出)。

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
Run Code Online (Sandbox Code Playgroud)