简体   繁体   English

如何使用 swift 在 iOS 中打开位置服务页面?

[英]How to open Location Services page in iOS using swift?

I need to ask user to enable Location Services if disabled.如果禁用,我需要让用户启用位置服务。 I have created an Alert which asks user if he wants to enable Location.我创建了一个警报,询问用户是否要启用位置。 I user clicks Ok I want to open Location Services page on iPhone as seen below:我用户单击确定我想在 iPhone 上打开定位服务页面,如下所示:

在此处输入图像描述

I am trying 'openSettingsURLString' but it opens App settings instead of Location Services page, this is the code:我正在尝试“openSettingsURLString”,但它会打开应用程序设置而不是位置服务页面,这是代码:

            let url = URL(string: UIApplication.openSettingsURLString)

            let settingsAction = UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default) {
                (UIAlertAction) in UIApplication.shared.open(url!, options: [:], completionHandler: nil)
                self.dismiss(animated: true, completion: nil)
            }

            alertController.addAction(cancelAction)
            alertController.addAction(settingsAction)
            self.present(alertController, animated: true, completion: nil)

I believe this is not possible, you can only navigate to the settings of the app itself;我相信这是不可能的,您只能导航到应用程序本身的设置; Apple expects the user themself to navigate to location services.苹果希望用户自己导航到定位服务。 . . I would recommend giving the user instructions on how to navigate to location services as an alternative such as some screenshots and the path they should take.我建议向用户提供有关如何导航到位置服务的说明作为替代方法,例如一些屏幕截图和它们应该采用的路径。

⚠️ Caution ⚠️注意

There is a way to achieve the following.有一种方法可以实现以下目标。 However, as of iOS 12, Apple has begun rejecting apps that use App-Prefs or prefs:root as both methods are based on undocumented APIs.但是,从 iOS 12 开始,Apple 已开始拒绝使用App-Prefsprefs:root的应用程序,因为这两种方法都基于未记录的 API。

Open Settings App - This one should be OK to use.打开设置应用程序-这个应该可以使用。

UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil)

Open Location Services开放定位服务

If this app is for personal use only and you do not plan to submit it to the App Store, then you should be ok to use the following.如果此应用仅供个人使用,并且您不打算将其提交到 App Store,那么您应该可以使用以下内容。 Otherwise, if you do plan to release your app, Apple will reject it upon review.否则,如果您确实计划发布您的应用程序,Apple 将在审核后拒绝它。

UIApplication.shared.open(URL(string: "App-prefs:LOCATION_SERVICES")!)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM