iOS 8和Swift中的MapKit

iOS 8和Swift中的MapKit,第1张

概述我正在尝试在iOS 8上使用MapKit并且我不断收到错误: Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationMana 我正在尝试在iOS 8上使用MapKit并且我不断收到错误:
Trying to start MapKit location updates without prompting for location authorization. Must call   -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

在这里查找,我发现我必须在我的pList中实现NSLocationWhenInUsageDescription,并且还调用了locationManager.requestWhenInUseAuthorization()但没有任何反应,我仍然在控制台中得到该错误.我究竟做错了什么?

解决方法 在我的应用程序委托中,我为类外的locationManager创建了一个可选的var,然后进行设置
locManager = CLLocationManager()locManager!.requestWhenInUseAuthorization()

这会导致警报视图d出NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription,如果您适当地更改它.

然后在视图控制器文件中,我在类外部创建了另一个var来保存本地CLLocationManager.我接着说

if locManager {        locMan = locManager!        locMan!.delegate = self    }

然后您可以使用委托方法

func locationManager(_manager: CLLocationManager!,dIDChangeAuthorizationStatus status: CLAuthorizationStatus)

当授权状态改变时调用它,当用户响应d出窗口时它会调用.在此内部,您可以使用这段代码将用户位置放在地图上

if status == CLAuthorizationStatus.AuthorizeDWhenInUse {        map.showsUserLocation = true    }

仅当您在使用时获得授权时,才会将用户位置添加到地图中.

总结

以上是内存溢出为你收集整理的iOS 8和Swift中的MapKit全部内容,希望文章能够帮你解决iOS 8和Swift中的MapKit所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1103461.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-28
下一篇2022-05-28

发表评论

登录后才能评论

评论列表(0条)

    保存