CLLocationManager requestWhenInUseAuthorization()不起作用 [英] CLLocationManager requestWhenInUseAuthorization() not working

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

问题描述

我试图在我的iOS应用中使用位置服务,但是由于某些原因 requestWhenInUseAuthorization 无法正常工作。当用户第一次使用该应用程序时,会出现提示正常的提示,但随后当您再次打开该应用程序时,由于某种原因未调用 didChangeAuthorizationStatus 方法,因此我无法在地图上显示用户的当前位置。

I am trying to use location services in my iOS app but for some reason requestWhenInUseAuthorization is not working. When the user first uses the app, the prompt comes up as normal asking for permissions but then when you open the app a second time, for some reason didChangeAuthorizationStatus method is not called so I cannot display the user current location on the map.

我的代码如下:

 override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
    var config:NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration()
    config.URLCache = NSURLCache(memoryCapacity: 2 * 1024 * 1024, diskCapacity: 10 * 1024 * 1024, diskPath: "MarkerData")
    markerSession = NSURLSession(configuration: config)
 }



 func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    if status == .AuthorizedWhenInUse {

        locationManager.startUpdatingLocation()
        mapView.delegate = self
        mapView.myLocationEnabled = true
        mapView.settings.myLocationButton = true
     }
 }


推荐答案

首先,您需要添加您的info.plist文件中的NSLocationWhenInUseUsageDescription NSLocationAlwaysUsageDescription (如果要在后台使用)。参见下图:

First you need to add NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription(if you want to use in background) in your info.plist file. See the following image:

接下来,在您的快捷文件中,您需要调用 locationManager.requestWhenInUseAuthorization() locationManager.requestAlwaysAuthorization()在您的 viewDidLoad()方法中。

Next, in your swift file, you need to call either locationManager.requestWhenInUseAuthorization() or locationManager.requestAlwaysAuthorization() in your viewDidLoad() method.

最后,您可以执行 mapView.camera = GMSCameraPosition(target:locations.last!.coordinate,zoom:15,方位:0, viewAngle:0)在您的locationManager委托方法中。

Finally, you can do a mapView.camera = GMSCameraPosition(target: locations.last!.coordinate, zoom: 15, bearing: 0, viewingAngle: 0) in your locationManager delegate method.

示例代码:

class ViewController: UIViewController, CLLocationManagerDelegate {

    var locationManager = CLLocationManager();

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        var camera = GMSCameraPosition.cameraWithLatitude(-33.86,
            longitude: 151.20, zoom: 6)
        var mapView = GMSMapView.mapWithFrame(CGRectZero, camera: camera)
        mapView.myLocationEnabled = true
        self.view = mapView

        locationManager.delegate = self
        locationManager.distanceFilter = kCLDistanceFilterNone
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        if #available(iOS 8.0, *) {
            print("iOS >= 8.0.0")
            locationManager.requestAlwaysAuthorization()
        }
        locationManager.startUpdatingLocation()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
        println(locations.last)

        var mapView = self.view as! GMSMapView

        mapView.camera = GMSCameraPosition(target: locations.last!.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
    }
}

您可以这篇文章,了解有关iOS 8中LocationManager更改的更多详细信息。

You can will this post, for more details about LocationManager change in iOS 8.

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

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