NSLocation 不等我点击允许

NSLocation doesn't wait for me to click allow

当我 运行 代码时,window 会弹出请求使用位置的权限,但几乎立即消失,用户没有机会单击 "Allow"。有没有办法在继续之前强制执行此操作?

import UIKit
import MapKit
import CoreLocation

class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate{

var map:MKMapView?
var manager:CLLocationManager!

convenience init(frame:CGRect){
    self.init(nibName: nil, bundle: nil)
    self.view.frame = frame

    self.map = MKMapView(frame: frame)
    self.map!.delegate = self

    self.view.addSubview(self.map!)

}

override func viewDidLoad() {
    super.viewDidLoad()

    // Core Location
    manager = CLLocationManager()
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest

}

func locationManager(manager: CLLocationManager!,
    didChangeAuthorizationStatus status: CLAuthorizationStatus){

        print("The authorization status of location services is changed to: ")

        switch CLLocationManager.authorizationStatus(){
        case .Denied:
            println("Denied")
        case .NotDetermined:
            println("Not determined")
        case .Restricted:
            println("Restricted")
        default:
            println("Authorized")
        }

}

func displayAlertWithTitle(title: String, message: String){
    let controller = UIAlertController(title: title,
        message: message,
        preferredStyle: .Alert)

    controller.addAction(UIAlertAction(title: "OK",
        style: .Default,
        handler: nil))

    presentViewController(controller, animated: true, completion: nil)

}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    if CLLocationManager.locationServicesEnabled(){
        switch CLLocationManager.authorizationStatus(){
        case .Denied:
            displayAlertWithTitle("Not Determined",
                message: "Location services are not allowed for this app")
        case .NotDetermined:
                manager.requestWhenInUseAuthorization()
                manager.startUpdatingLocation()
        case .Restricted:
            displayAlertWithTitle("Restricted",
                message: "Location services are not allowed for this app")
        default:
            println("Default")
        }

    } else {
        println("Location services are not enabled")
    }

}


func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) {

    var userLocation:CLLocation = locations[0] as! CLLocation
    var latitude:CLLocationDegrees = userLocation.coordinate.latitude
    var longitude:CLLocationDegrees = userLocation.coordinate.longitude
    var latDelta:CLLocationDegrees = 1.0
    var lonDelta:CLLocationDegrees = 1.0

    var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
    var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
    var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
    map!.setRegion(region, animated: true)
    manager.stopUpdatingLocation()

}

这是因为您在从 manager.requestWhenInUseAuthorization() 获得结果之前调用了 manager.startUpdatingLocation()。即使您调用了 requestWhenInUseAuthorization,您在获得该方法的结果之前就更新了用户的位置(实际上,我得到的 与您完全一样!)

该问题的答案很好地解释了解决方案。基本上,您需要实现 locationManager:didChangeAuthorizationStatus 委托方法,只要授权状态根据用户输入发生变化,就会调用该方法。如果用户确实授权跟踪,那么你可以调用manager.startUpdatingLocation().

此外,有关如何实现这些方法的 Swift 示例,请查看此 guide

请试试这个:

import UIKit
import GoogleMaps
import CoreLocation

class StartViewController: UIViewController,CLLocationManagerDelegate,GMSMapViewDelegate {

var locationManager: CLLocationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    setupLocationManager()

}

func setupLocationManager() {
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.delegate = self
    locationManager.requestWhenInUseAuthorization()
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    locationHandler()
}

func locationHandler() {

    if CLLocationManager.locationServicesEnabled() == true {

        if (CLLocationManager.authorizationStatus() == .denied) {
            // The user denied authorization

        } else if (CLLocationManager.authorizationStatus() == .authorizedAlways) {
            // The user accepted authorization

        } else if (CLLocationManager.authorizationStatus() == .notDetermined){
            // The user not determiend authorization

        }else if (CLLocationManager.authorizationStatus() == .authorizedWhenInUse){
            // In use

        }else{ }
    }else{
        //Access to user location permission denied!

    }


}



}//class  

成功。