我试图在我的iOS应用程序中使用位置服务但由于某种原因requestWhenInUseAuthorization无法正常工作.当用户第一次使用该应用程序时,提示按正常方式询问权限,但是当您第二次打开应用程序时,由于某种原因,didChangeAuthorizationStatus方法未被调用,因此我无法在地图上显示用户当前位置.
我的代码如下:
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
}
}
Run Code Online (Sandbox Code Playgroud)