How to open Location services screen from setting

2019-01-31 19:17发布

I want to open location service screen programmatically to turn on service.

enter image description here

9条回答
手持菜刀,她持情操
2楼-- · 2019-01-31 19:47

Location services App-Prefs:root=Privacy&path=LOCATION worked for me. When I tested on a device and not a simulator.

I won't list the things I tried that did not work, it's a long list.

Usage example that assumes either location services are disabled or permission is denied or not determined:

if !CLLocationManager.locationServicesEnabled() {
    if let url = URL(string: "App-Prefs:root=Privacy&path=LOCATION") {
        // If general location settings are disabled then open general location settings
        UIApplication.shared.openURL(url)
    }
} else {
    if let url = URL(string: UIApplicationOpenSettingsURLString) {
        // If general location settings are enabled then open location settings for the app
        UIApplication.shared.openURL(url)
    }
}
查看更多
我命由我不由天
3楼-- · 2019-01-31 19:48

Actually there's much simpler solution to that. It'll show your app settings with loction services/camera access, etc.:

func showUserSettings() {
    guard let urlGeneral = URL(string: UIApplicationOpenSettingsURLString) else {
        return
    }
    UIApplication.shared.open(urlGeneral)
}
查看更多
来,给爷笑一个
4楼-- · 2019-01-31 19:52

Swift 4.2

Go straight to YOUR app's settings like this. Don't forget to put in your bundle identifier -

if let bundleId = Bundle.main.bundleIdentifier,
    let url = URL(string: "\(UIApplication.openSettingsURLString)&path=LOCATION/\(bundleId)") 
{
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
查看更多
【Aperson】
5楼-- · 2019-01-31 19:54

I have tried all the above answers,it's not working on iOS11..it just opens settings page and not the app settings .. Finally this works..

UIApplication.shared.open(URL(string:UIApplicationOpenSettingsURLString)!)

Refer:https://developer.apple.com/documentation/uikit/uiapplicationopensettingsurlstring?language=swift

查看更多
啃猪蹄的小仙女
6楼-- · 2019-01-31 19:58

After adding prefs as a url type, use the following code to go directly to the location settings of an application.

if let url = URL(string: "App-prefs:root=LOCATION_SERVICES") {
     UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
查看更多
ら.Afraid
7楼-- · 2019-01-31 19:59

You can open it directly like using below code,

But first set URL Schemes in Info.plist's URL Type Like:

enter image description here

Then write below line at specific event:

In Objective - C :

[[UIApplication sharedApplication] openURL:
 [NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];

In Swift :

UIApplication.sharedApplication().openURL(NSURL(string: "prefs:root=LOCATION_SERVICES")!)

Hope this will help you.

查看更多
登录 后发表回答