【问题标题】:NSLocation doesn't wait for me to click allowNSLocation 不等我点击允许
【发布时间】:2015-08-24 14:16:51
【问题描述】:

当我运行代码时,弹出窗口询问是否允许使用位置,但几乎立即消失,没有给用户点击“允许”的机会。有没有办法在继续之前强制执行此操作?

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

}

【问题讨论】:

标签: ios swift core-location cllocation


【解决方案1】:

请试试这个:

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  

取得成功。

【讨论】:

    【解决方案2】:

    这是因为您在从manager.requestWhenInUseAuthorization() 获得结果之前调用了manager.startUpdatingLocation()。即使您调用requestWhenInUseAuthorization,您也会在获得该方法的结果之前更新用户的位置(实际上,我和您的same question 完全相同!)

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

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

    【讨论】:

    • 我尝试这样做并更新了我的问题以包括我实施的内容,但现在它甚至没有提示用户启用位置服务。
    • @PabloPicasso,您是否在您的Info.plist 文件中添加了NSLocationUsageDescription 字符串? (请参阅“了解何时启动位置服务”:developer.apple.com/library/ios/documentation/UserExperience/…
    • 我没有。我希望每次启动应用时都启用定位服务。
    • @PabloPicasso 问题是,在用户首次启动应用程序时,在获得许可之前不应启用定位服务。但这不是重点——该字符串的作用是向用户提供您想要获取其位置的原因的描述。
    • 我已将 NSLocationWhenInUseUsageDescription 添加到 p.list 。之前我不包含开关CLLocationManager.authorizationStatus()的时候,它提示用户允许定位服务,但现在根本不做
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多