简体   繁体   中英

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:

在此处输入图像描述

I am trying 'openSettingsURLString' but it opens App settings instead of Location Services page, this is the code:

            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.

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. Otherwise, if you do plan to release your app, Apple will reject it upon review.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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