【发布时间】:2019-03-27 11:46:22
【问题描述】:
如果用户选择“不允许”位置访问,我的 iOS 应用程序已被 Apple 拒绝,因为该应用程序会崩溃。然后点击我的地图按钮。
如何包装这个按钮以查看用户是否已授予权限,如果没有,我如何再次请求权限?
//Map Button Action - Opens Maps - Gives choice of Google or Apple maps
@IBAction func googleMapBtn(_ sender: UIButton) {
UIDevice.current.isBatteryMonitoringEnabled = true
let state = UIDevice.current.batteryState
//If user is in Loop - Cant open maps
if state == .charging {
print("In Loop - Cant open maps")
}
//Present Map Options
else {
let alertController = UIAlertController.init(title: "Open Map", message: "", preferredStyle: .alert)
alertController.addAction(UIAlertAction.init(title: "Google Maps", style: .default, handler: { (action) in
self.googleMapsOpen()
}))
alertController.addAction(UIAlertAction.init(title: "Apple Maps", style: .default, handler: { (action) in
self.appleMapsOpen()
}))
alertController.addAction(UIAlertAction.init(title: "Back", style: .default, handler: { (action) in
self.dismiss(animated: true, completion: nil)
}))
self.present(alertController, animated: true) {
}
}
}
每当用户选择地图类型 Google/Apple 并执行 self.googleMapsOpen() 或 self.appleMapsOpen() 时,代码就会崩溃。具体是在 let scheme= 上崩溃了
func googleMapsOpen(){
print("Google Maps Pressed")
let scheme = "comgooglemaps://?center=\(LocationManager.sharedInstance.location.coordinate.latitude),\(LocationManager.sharedInstance.location.coordinate.longitude)&zoom=15"
self.open(scheme: scheme)
}
func appleMapsOpen(){
print("Apple Maps Pressed")
let scheme = "http://maps.apple.com/?ll=\(LocationManager.sharedInstance.location.coordinate.latitude),\(LocationManager.sharedInstance.location.coordinate.longitude)&zoom=15"
self.open(scheme: scheme)
}
【问题讨论】:
-
在这段代码中你在哪里使用了用户位置,你知道你的应用在哪一行崩溃?
-
@Rengers 这就是苹果所说的“如果我们在点击“不允许”时尝试使用地图功能,则您的应用程序在运行 iOS 12.0.1 并连接到 IPv6 网络的 iPhone 上崩溃提示是否允许使用当前位置。如果需要位置信息,则需要显示通知或警报指示此要求。"
-
我也已经测试过了,如果我选择不允许位置许可并尝试打开其中一个地图按钮,应用程序将崩溃
-
对,所以如果你能重现崩溃,你就可以找出你的应用在哪一行崩溃了。您无法让 iOS 再次提示许可。您可以检查
CLLocationManager上的权限并向用户显示警报,如果权限被拒绝,提示他们打开应用设置。 -
@Paulw11 我已经更新了我的问题以显示崩溃的位置
标签: ios swift core-location cllocationmanager ios-permissions