簡體   English   中英

如何知道locationManager.requestAlwaysAuthorization()是否已被詢問

[英]How to know if locationManager.requestAlwaysAuthorization() has already been asked

請求用戶的iOS位置權限時,如何知道是否已向用戶詢問locationManager.requestAlwaysAuthorization()

如果用戶.AuthorizedWhenInUse狀態並且始終授權請求被拒絕,則不會顯示下一個請求的always-auth提示,因此我不會收到此請求啟動的任何回調。

有任何想法嗎?

您需要檢查CLLocationManager.authorizationStatus()並且僅在值是.notDetermined時才請求授權,因為這是實際顯示授權提示的唯一情況。

您可以檢查“ 授權”狀態,並比較是否notDetermined是否被詢問,否則-已被詢問。

您可以像這樣使用authorizationStatus()來知道。

if CLLocationManager.authorizationStatus() == .notDetermined {
    print("Not Determined")
}
else if CLLocationManager.authorizationStatus() == .restricted {
     print("Restricted")
}
else if CLLocationManager.authorizationStatus() == .denied {
     print("Denied")
}
else if CLLocationManager.authorizationStatus() == .authorizedAlways {
    print("Always Authorized")
}
else if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
    print("Authorized When Require")
}

如果對話框第一次出現,它將返回.notDetermined狀態,並且如果您響應對話框,它將根據您的選擇返回狀態,就像您始終允許訪問位置一樣,它返回.authorizedAlways

經過大量討論后,我找到了解決方案。

  1. 每次啟動應用程序時,或在用戶查看“設置”應用程序后,當應用程序進入前台時,請檢查授權狀態。

  2. 如果狀態notDetermined ,則請求authorizedWhenInUse 您不能直接跳轉到authorizedAlways

  3. 如果狀態為authorizedWhenInUse ,請請求authorizedAlways

美中不足的 ,如你所知,是當用戶從去notDeterminednotDetermined ,即不改變它們的設置。 我們需要能夠在提示之前查看其狀態。

這實際上很簡單,只需將CLLocationManager.authorizationStatus()的值保存在名為previousAuthStatus的屬性中。

private var previousAuthStatus = CLLocationManager.authorizationStatus() 

當應用程序進入前台時,請以先前的狀態檢查授權狀態。

完整代碼

千萬不要錯過這里的最后一位previousAuthStatus設置

func checkStatus(_ status: CLAuthorizationStatus) {

    switch status {

    case .notDetermined:

        // request "When in use"
        locationManager.requestWhenInUseAuthorization()

    case .authorizedWhenInUse:

        // already tried requesting "Always"?
        guard previousAuthStatus != .authorizedWhenInUse else {
            <#notify the user...#>
            break
        }

        // otherwise, request "Always"
        locationManager.requestAlwaysAuthorization()

    case .authorizedAlways:

        // start updating location
        <#dismiss any warning you may have displayed#>
        locationManager.startUpdatingLocation()

    case .denied:

        <#notify the user...#>

    case .restricted:

        <#notify the user...#>

    @unknown default:

        break
    }

    // save status for next check
    previousAuthStatus = status
}

我正面臨着同樣的問題。 這僅對應用程序更新(您未將authStatus存儲在先前版本的UserDefaults中)存在問題。 如果用戶決定卸載並重新安裝該應用程序(刪除了用戶默認設置),或者用戶手動將狀態從“始終”更改為“使用中”,這也是一個問題(無法得知已將其更改為“何時使用”)處於使用中”或一直處於這種狀態)。

順便說一句:用戶手動更改授權實際上是很常見的,因為iOS現在偶爾會發出警報“ AppName一直在后台使用您的位置...您是否要繼續允許它”(或類似那不記得了)。 我的許多用戶在該警報中選擇“否”,導致“始終”位置訪問權限更改為“在使用中”,而應用程序未收到有關該事件的通知。

但是,iOS會記住之前是否已經詢問過requestAlwaysAccess 即使在卸載並重新安裝應用程序之后。

因此,由於缺乏其他選擇,我現在正在使用它,說實話,它不是最用戶友好的,但是它確實可以工作並且對用戶友好“足夠”(至少對我而言)。

代替請求始終授權,我只用一個指向應用程序“設置”頁面的按鈕來發出警報,然后用戶可以在其中手動更改設置。 如果應用之前顯示了此警報,我確實添加了一個userdefault存儲。 如果是這樣,我將不再顯示。

switch CLLocationManager.authorizationStatus() {
    case .restricted, .denied:
        // show an alert with one button opening settings (see below)                   
    case .authorizedAlways:
        // already have always permission, continue with your code
    case .notDetermined:
        // request whenInUse authorisation (you can request always authorisation here too, but iOS won't show 'always' as a choice the first time)
    case .authorizedWhenInUse:
        guard !UserDefaults.showedNoAlwaysAuthorisationAlert else {
            return
        }
        UserDefaults.showedNoAlwaysAuthorisationAlert = true
        // show the alert with "no thanks" and "settings" button
        // button action:
        if 'settingsButtonTapped', let settingsUrl = URL(string: UIApplication.openSettingsURLString), UIApplication.shared.canOpenURL(settingsUrl) {
             UIApplication.shared.open(settingsUrl, completionHandler: nil)
        }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM