簡體   English   中英

無法確定用戶何時不允許定位服務

[英]Can't Determine When User Doesn't Allow Location Services

我要求用戶打開位置服務。 我想知道用戶何時單擊Don't Allow以便我處理一些通知。 但是,當我單擊Don't Allow時,沒有調用didFailWithErrordidChangeAuthorizationStatus方法。 我知道這不會在記錄器中打印任何內容。 我已附上代碼示例。 我在做什么錯以及如何解決這個問題。 謝謝。

import UIKit

import CoreLocation

class AwesomeViewController: UIViewController, CLLocationManagerDelegate {

    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        let authorizationStatus = CLLocationManager.authorizationStatus()

        if(authorizationStatus == .AuthorizedWhenInUse || authorizationStatus == .AuthorizedAlways) {
            // authorization is good
        } else {
            locationManager.requestWhenInUseAuthorization()
        }
    }


    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        print(status)
    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {            
        print(error.localizedDescription)
    }
}

沒有調用didFailWithErrordidChangeAuthorizationStatus方法

這些是委托方法。 您的位置管理器沒有任何委托-當然,它沒有 (AwesomeViewController實例)作為委托。 因此永遠不會調用這些方法。 您需要設置位置管理器的委托(在這種情況下,您將其設置為self )。

暫無
暫無

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

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