【问题标题】:requestWhenInUseAuthorization() not Work in iOS 8 With NSLocationWhenInUseUsageDescription Key in Info.plistrequestWhenInUseAuthorization() 在 iOS 8 中无法使用 Info.plist 中的 NSLocationWhenInUseUsageDescription 键
【发布时间】:2015-02-25 05:32:38
【问题描述】:

我正在使用 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 个选项:“从不”、“在使用应用程序时”、“始终”。但是什么都没有选择。

【问题讨论】:

    标签: swift ios8 core-location


    【解决方案1】:

    viewWillAppear 而不是 viewDidLoad 中设置这些属性已为我解决了这个问题,谢谢!

    override func viewWillAppear(animated: Bool) {
    
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
      }
    

    【讨论】:

      【解决方案2】:

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

      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()
        }
      

      清理项目并再次运行。

      【讨论】:

        【解决方案3】:

        问题解决了。我的managerviewDidLoad() 方法中被声明为本地变量,但它应该是一个类级别的属性。

        在我将 manager 声明从 viewDidLoad() 中移出后,我的应用程序运行了。

        不确定manager.requestWhenInUseAuthorization() 究竟是如何在幕后工作的,以及为什么在viewDidLoad() 中定义的确切manager 不起作用。希望知道这个细节的人不吝赐教。

        【讨论】:

        • 如果manager在viewDidLoad()中定义为局部变量,在方法结束时会从内存中释放。因此,对 requestWhenInUseAuthorization() 和 startUpdatingLocation() 的调用将起作用,但是一旦释放管理器,它将停止更新位置并且永远不会调用委托。
        • 我终于找到了解决方案!通常,当我使用定位服务时,我有一个处理所有事情的单例,但是在尝试快速原型化一些定位服务的东西时,我还没有实现单例方法,因此正如你所说的那样,对象并没有留下来...
        猜你喜欢
        • 1970-01-01
        • 2019-04-03
        • 1970-01-01
        • 2014-09-11
        • 2014-09-20
        • 2021-10-18
        • 2018-10-27
        • 2011-03-16
        • 2014-12-14
        相关资源
        最近更新 更多