- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我的应用似乎在每次使用时都运行良好,但第一次除外。我要求用户授权,并且我在 plist 中有适当的键,但是在请求授权的行之后的其余 viewDidLoad 不执行。我在下面附加了断点,断点 2 没有在第一次使用该应用程序时命中。
我很确定在获得授权后它会跳转到扩展中的 func locationManager。
我可以等到最后才请求授权,直到一切都准备就绪,但不确定这是最好的还是唯一的出路。
谢谢,
class MapController: UIViewController, GMSMapViewDelegate {
var locationManager = CLLocationManager()
var currentLocation: CLLocation?
@IBOutlet var mapView: GMSMapView!
override func viewDidLoad(){
super.viewDidLoad()
locationManager = CLLocationManager()
--------------------------> breakpoint 1
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
-------------------------> breakpoint 2
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 50
locationManager.startUpdatingLocation()
locationManager.delegate = self
guard let lat = locationManager.location?.coordinate.latitude else {return}
guard let lng = locationManager.location?.coordinate.longitude else {return}
mapView.settings.compassButton = true
mapView.settings.myLocationButton = true
mapView.isMyLocationEnabled = true
let camera = GMSCameraPosition.camera(withLatitude: lat, longitude: lng, zoom: 1)
mapView.camera = camera
mapView.delegate = self
getData()
}
extension MapController: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location: CLLocation = locations.last else {return}
let camera = GMSCameraPosition.camera(withLatitude: location.coordinate.latitude, longitude: location.coordinate.longitude, zoom: 1)
mapView.animate(to: camera)
}
}
最佳答案
为什么要请求对两件不同的事情的授权?如果您请求并获得 always 授权,则在使用时无需请求授权,因为这只是 always 授权的一个子集。
此外,这些都是异步函数,因此您不能在它们之后立即执行基于位置的代码,因为如果您还没有授权,紧接在 requestAuthorization()
之后的代码将在之前执行您实际上获得了授权,因此不会调用函数,因为您还没有授权。
您必须在调用任何与位置相关的代码之前检查授权状态,例如 locationManager.startUpdatingLocation()
并且只有在状态被授权时才执行与位置相关的代码。如果未授权,则必须执行 CLLocationManagerDelegate
的 locationManager(_:didChangeAuthorization:)
函数,并在检查结果后调用该函数内的位置相关调用更改为授权状态。
关于swift - 在第一次加载 requestAuthorization 之后,viewDidLoad 的其余部分未执行,下一行跳转到委托(delegate)函数 (swift 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45710475/
为了测试,我试图重新创建系统“请求访问”弹出窗口体验。 更新: iOS 11下,删除App后,会再次出现系统弹窗。 (上一个问题) App 第一次运行时(也是唯一 次),系统弹出窗口显示,请求访问。之
对于 ios 10,我用它来注册推送通知: Registering for Push Notifications in Xcode 8/Swift 3.0? 有没有办法在 appdelegate 和
每次我尝试为照片库请求授权时,我的应用程序在模拟器中运行时总是崩溃。我在 didFinishLaunchingWithOptions 的 appDelegate 中使用以下代码: if PHPhoto
我目前正在尝试制作一个简单的计步器应用程序。但是,我似乎无法获得使用 HealthKit 的授权。 我有这段代码试图获得许可并在之后使用回调来处理结果。 func authorizeHealthKit
UNUserNotificationCenter.current().requestAuthorization() 是否可以多次调用? 我可以在每次应用启动时调用吗? 是否可以在用户授予通知权限后再次
我正在尝试在 Apple Watch 应用程序中收集健康数据,但当我调用 healthStore.requestAuthorization 时,我的应用程序总是崩溃。我已根据需要在 Info.plis
我正在制作一个将使用语音识别的应用程序,我想知道我的应用程序遇到这种情况的频率或时间 我知道这与限制语音识别的设备有关,而不是与用户有关,但具体是什么时候??是由于某些特定型号不支持语音识别还是特定于
它在 iOS 9 中不请求画廊的权限,但在 iOS 8 中它工作正常。也许我需要 info.plist 中的任何权限 key ? PHPhotoLibrary.requestAuthorizatio
我有以下功能,它显示带有两个不同选项的操作表。目前,唯一实现的选项是标题为“照片”的选项。正确显示操作表并调用正确的操作。我的问题是,在模拟器和实际设备上,我无法显示请求访问照片库的提示。 PHPho
我已经将我的项目升级到 swift 3 并使用 Xcode 8现在,当我想要访问照片时,我遇到了崩溃,控制台中没有任何信息。 我已将“隐私 - 照片库使用说明”添加到我的 info.plist 中。请
我刚刚注意到,在将 Xcode 更新到版本 10.2.1 之后,在模拟器上启动我的几个项目时,我无法看到 Library 和 Apple Music 的权限请求。 调试,发现授权请求电话无人接听 MP
我正在尝试调用: `navigator.geolocation.requestAuthorization();` 请求地理定位许可。 但是,这会导致在 iOS 模拟器中运行时出错 这在某一时刻起作用,
我已经添加了照片库插件,请按照 ionic doc 上的说明进行操作 here 我使用 ionic cordova plugin add cordova-plugin-photo-library --
我遇到一个崩溃,说我的 bundle ID 是 nil 2016-08-31 10:39:11.630 WeatherHop[52533:17767791] * Terminating app due
我的应用似乎在每次使用时都运行良好,但第一次除外。我要求用户授权,并且我在 plist 中有适当的键,但是在请求授权的行之后的其余 viewDidLoad 不执行。我在下面附加了断点,断点 2 没有在
我是一名优秀的程序员,十分优秀!