swift – requestWhenInUseAuthorization()无法在iOS 8中使用Info.plist中的NSLocationWhenInUseUsageDescription键

我正在使用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个选项:“从不”,“使用应用程序时”,“始终”.但没有选择任何东西.

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

在我将经理声明移出viewDidLoad()之后,我的应用程序运行了.

不确定manager.requestWhenInUseAuthorization()是如何在场景后面工作的,以及为什么viewDidLoad()中定义的管理器不起作用.希望知道这个细节的人启发我.

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...