我正在使用
swift为iOS 9.3编写应用程序,需要一张地图来显示用户位置.
我初始化CLLocationManager及其委托,我配置info.plist
隐私 – 位置使用说明字符串和调用requestWhenInUseAuthorization但它不显示任何授权请求.
我初始化CLLocationManager及其委托,我配置info.plist
隐私 – 位置使用说明字符串和调用requestWhenInUseAuthorization但它不显示任何授权请求.
这是我的代码:
import UIKit import MapKit class DetailController: UIViewController,CLLocationManagerDelegate { var locManager: CLLocationManager! @IBOutlet var myMap: MKMapView! override func viewDidLoad() { super.viewDidLoad() mostraPosizione() } func mostraPosizione(){ locManager = CLLocationManager() locManager.delegate = self locManager.desiredAccuracy = kCLLocationAccuracyBest let authorizationStatus = CLLocationManager.authorizationStatus() if (authorizationStatus == CLAuthorizationStatus.NotDetermined) { locManager.requestWhenInUseAuthorization() } else { locManager.startUpdatingLocation() } myMap.showsUserLocation = true } func locManager(manager: CLLocationManager,didChangeAuthorizationStatus status: CLAuthorizationStatus) { if (status == CLAuthorizationStatus.NotDetermined) { locManager.requestWhenInUseAuthorization() } else { locManager.startUpdatingLocation() } } }
Xcode输出:
Trying to start MapKit location updates without prompting for location authorization.
Must call -[CLLocationManager requestWhenInUseAuthorization]
or -[CLLocationManager requestAlwaysAuthorization] first.
我正在iPhone 5(iOS 9.3)模拟器上测试它.
这个东西有什么问题?
有人有建议吗?
我想是因为
let authorizationStatus = CLLocationManager.authorizationStatus()
是零,所以你的requestWhenInUseAuthorization()永远不会被调用.
let authorizationStatus = CLLocationManager.authorizationStatus() if (authorizationStatus == CLAuthorizationStatus.NotDetermined) || (authorizationStatus == nil) { locManager.requestWhenInUseAuthorization() } else { locManager.startUpdatingLocation() }
应该工作.
编辑:
如果用户拒绝请求,您还应该处理此案例
if (authorizationStatus == CLAuthorizationStatus.Denied ) { // Display a message,do what you want }
实际上,一旦用户拒绝授权,您就无法再次显示弹出窗口,用户必须进入应用程序设置并自行设置使用其位置的授权.但是,您现在可以直接从应用程序链接到设置,因此对用户来说更容易.
// Open the settings of your app if let url = NSURL(string:UIApplicationOpenSettingsURLString) { UIApplication.sharedApplication().openURL(url) }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。