gpt4 book ai didi

ios - 禁用位置服务后立即显示警报 View

转载 作者:行者123 更新时间:2023-11-28 13:01:28 25 4
gpt4 key购买 nike

我正在制作一个需要启用位置服务的 iOS 应用程序。因此,一旦安装了我的应用程序,它就会要求用户允许定位服务。显然,很多用户按下“不允许”并最终无法使用我的 iOS 应用程序。

在我的应用程序中,我输入了以下代码行:

  override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)

if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Denied {
self.locationAlert = UIAlertView(title: "Location Services Permission Needed", message: "Location service needs to be turned on to use Peek! Please press setting button below and turn the service on!", delegate: self, cancelButtonTitle: "Settings")
locationAlert.show()
}
}

extension ViewController: UIAlertViewDelegate {
func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) {
alertView.dismissWithClickedButtonIndex(buttonIndex, animated: true)
UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)
}
}

问题是警报 View 仅在您重新打开应用程序时显示。我希望用户在安装应用程序时按下“不允许位置服务”时立即显示此警报 View 。

有没有办法让这个 Action 发生?

最佳答案

您可以使用 CLLocationManagerDelegate 的委托(delegate)方法:

- (void)locationManager:(CLLocationManager *)manager
didChangeAuthorizationStatus:(CLAuthorizationStatus)status

此方法会告诉您位置授权的状态是否已更改

示例:

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
switch status {
case .Denied:
// Changed status to denied
break;
default:
break;
}
}

注意:记得将CLLocationManager的delegate设置为self!还最好检查是否尚未显示该警报 View 以避免出现多重警报。

关于ios - 禁用位置服务后立即显示警报 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33714355/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com