requestWhenInUseAuthorization()在iOS 8中不起作用Info.plist中的NSLocationWhenInUseUsageDescription键

时间:2014-12-29 00:12:27

标签: swift ios8 core-location

我正在使用iOS SDK 8.1尝试调用requestWhenInUseAuthorization()方法来提示用户授予对我的应用的访问权限。我导入了CoreLocation.framework,并将NSLocationWhenInUseUsageDescription和NSLocationAlwaysUsageDescription键添加到info.plist中。当我运行应用程序时,它从未提示我进行位置访问。以下是我的代码,我错过了什么?

import UIKit
import CoreLocation
import MapKit

class ViewController: UIViewController, CLLocationManagerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        var manager = CLLocationManager()
        manager.delegate = self
        manager.desiredAccuracy = kCLLocationAccuracyBest

        let authorizationStatus = CLLocationManager.authorizationStatus()
        switch authorizationStatus {
        case .Authorized:
            println("authorized")
        case .AuthorizedWhenInUse:
            println("authorized when in use")
        case .Denied:
            println("denied")
        case .NotDetermined:
            println("not determined")
        case .Restricted:
            println("restricted")
        }

        manager.requestWhenInUseAuthorization()
        manager.startUpdatingLocation()

    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        let location = locations[0] as CLLocation
        println("Latitude: \(location.coordinate.latitude). Longitude: \(location.coordinate.longitude).")
    }

    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        switch status {
        case .Authorized:
            println("authorized")
        case .AuthorizedWhenInUse:
            println("authorized when in use")
        case .Denied:
            println("denied")
        case .NotDetermined:
            println("not determined")
        case .Restricted:
            println("restricted")
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

控制台只显示“未确定”一次,没有别的。所以我去了iPhone模拟器=>设置=>隐私=>位置=>我的应用程序它向我展示了3个选项:“从不”,“使用应用程序时”,“始终”。但没有选择任何东西。

3 个答案:

答案 0 :(得分:18)

问题解决了。我的manager被声明为viewDidLoad()方法中的局部变量,但它应该是类级属性。

我将manager声明移出viewDidLoad()后,我的应用就有效了。

不确定manager.requestWhenInUseAuthorization()在幕后工作的确切程度以及为什么manager中定义的viewDidLoad()无法正常工作。希望知道这个细节的人启发我。

答案 1 :(得分:1)

viewWillAppear 中设置这些属性而不是 viewDidLoad 为我修复了它,谢谢!

override func viewWillAppear(animated: Bool) {

    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
  }

答案 2 :(得分:0)

我也遇到了同样的问题。我在info.plist中添加了两个密钥。

enter image description here

iOS 8.0及更高版本支持NSLocationWhenInUseUsageDescription键。如果Info.plist文件同时包含此密钥和NSLocationUsageDescription密钥,则系统将使用此密钥并忽略NSLocationUsageDescription密钥。

重新排列代码:

let locationManager: CLLocationManager = CLLocationManager()
  let authorizationStatus = CLLocationManager.authorizationStatus()

override func viewDidLoad() {
    super.viewDidLoad()

    locationManager.delegate = self
    if(authorizationStatus == .Denied)
    {
      print("DENIED")
      locationManager.requestWhenInUseAuthorization()
    }

  }
override func viewWillAppear(animated: Bool) {

    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
  }

清理项目并再次运行。