Я использую iOS SDK 8.1, пытаясь вызвать метод requestWhenInUseAuthorization(), чтобы предложить пользователю предоставить доступ к моему приложению. Я импортировал CoreLocation.framework и добавил ключи NSLocationWhenInUseUsageDescription и NSLocationAlwaysUsageDescription в info.plist. Когда я запускал приложение, оно никогда не предлагало мне доступ к местоположению. Ниже мой код, что я пропустил?
import UIKit
import CoreLocation
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
var manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
let authorizationStatus = CLLocationManager.authorizationStatus()
switch authorizationStatus {
case .Authorized:
println("authorized")
case .AuthorizedWhenInUse:
println("authorized when in use")
case .Denied:
println("denied")
case .NotDetermined:
println("not determined")
case .Restricted:
println("restricted")
}
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
let location = locations[0] as CLLocation
println("Latitude: \(location.coordinate.latitude). Longitude: \(location.coordinate.longitude).")
}
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .Authorized:
println("authorized")
case .AuthorizedWhenInUse:
println("authorized when in use")
case .Denied:
println("denied")
case .NotDetermined:
println("not determined")
case .Restricted:
println("restricted")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
Консоль показала «не определено» только один раз и больше ничего. Итак, я зашел в iPhone Simulator => Настройки => Конфиденциальность => Местоположение => Мое приложение. Он показал мне 3 варианта: «Никогда», «При использовании приложения», «Всегда». Но ничего не выбрали.
