CLLocationManager在用户授权Swift之前启动更新

时间:2016-12-14 11:46:40

标签: ios swift cllocationmanager

使用位置管理器遇到一些问题。 似乎locationManager.requestWhenInUseAuth不会停止应用程序流,并且在用户可以解除auth警报之前调用startUpdatingLocation。 怎么避免这个?

我的应用程序加载了不可用GPS的默认值,所以我总是默认(因为即使&#34要调用此函数,也要调用此函数...?"警报仍在显示)。

我的代码:

if ask{
        locationManager.requestWhenInUseAuthorization()
        self.manageLocation()
    }

func manageLocation(){
    if CLLocationManager.locationServicesEnabled() {

        switch(CLLocationManager.authorizationStatus()) {

        case .notDetermined, .restricted, .denied:
            // load default deck
            self.loadBlink(useDefault: true)

        case .authorizedAlways, .authorizedWhenInUse:
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.distanceFilter = 10.0
            locationManager.startUpdatingLocation()

        }
    } else {
        // load default deck
        self.loadBlink(useDefault: true)
    }
}

2 个答案:

答案 0 :(得分:0)

请求异步授权。它立即返回,你必须考虑到这一点。

答案 1 :(得分:-1)

将此添加到 info.plist

<key>NSLocationAlwaysUsageDescription</key>
<string>$(PRODUCT_NAME) would like to use your location.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>$(PRODUCT_NAME) would like to use your location.</string>

设置var locationManager

var locationManager:CLLocationManager!
viewDidLoad上的

称之为:

locationManager = [[CLLocationManager alloc] init]; // objective-c
locationManager = CLLocationManager() // swift (if i'm not wrong)
在致电requestWhenInUseAuth

之前,请先致电此startUpdatingLocation