ios - NSLocation 不等我点击允许

标签 ios swift core-location cllocation

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

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

}

最佳答案

请尝试这个:

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  

取得成功。

关于ios - NSLocation 不等我点击允许,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30737530/

相关文章:

ios - 应用程序无法从 iCloud Drive 中的文档启动

ios - 位置访问权限警报未显示且 CLLocation 管理器委托(delegate)方法未在 xcode 6.4 中调用。请指教

swift iOS 7 替换时间和距离计数器

ios - 用于本地化的字符串比较

ios - UIView 不显示

ios - 如何判断我是否使用同一项来填充多个 UICollectionViewCell?

swift - 使用 Swift 的 defer 的正确方法是什么

macos - 快速抓取 "My Card"CNContact

ios - 赋予它们所有可选值

swift - 如何在继续之前等待 requestLocation?