requestWhenInUseAuthorization()在iOS 8中不起作用Info.plist中的NSLocationWhenInUseUsageDescription键 [英] requestWhenInUseAuthorization() not Work in iOS 8 With NSLocationWhenInUseUsageDescription Key in Info.plist

查看:1977
本文介绍了requestWhenInUseAuthorization()在iOS 8中不起作用Info.plist中的NSLocationWhenInUseUsageDescription键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using iOS SDK 8.1 trying to call requestWhenInUseAuthorization() method to prompt user to grant access to my app. I imported CoreLocation.framework, and added NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription keys into info.plist. When I ran the app, it never prompted me for location access. Below is my code, what have I missed?

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个选项:从不,使用应用程序时,始终。但没有任何东西可以选择ected。

The console only showed "not determined" once and nothing else. So I went to iPhone Simulator => Settings => Privacy => Location => My App. It showed me 3 options: "Never", "While Using the App", "Always". But nothing was selected.

推荐答案

问题解决了。我的管理器 viewDidLoad()方法中声明为本地var,但它应该是类级属性。

Problem solved. My manager was declared as local var inside viewDidLoad() method, but it should've been a class level property.

我将经理声明移出 viewDidLoad(),我的应用程序工作。

After I moved manager declaration out of viewDidLoad(), my app worked.

不确定 manager.requestWhenInUseAuthorization()究竟是如何在幕后工作的在 viewDidLoad()中定义的 manager 不起作用。希望知道这个细节的人启发我。

Not sure how exactly manager.requestWhenInUseAuthorization() work behind the scene and why exactly manager defined within viewDidLoad() not work. Hope someone who knows this detail enlighten me.

这篇关于requestWhenInUseAuthorization()在iOS 8中不起作用Info.plist中的NSLocationWhenInUseUsageDescription键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆