60

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

enter image description here

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Monika Patel
  • 2,287
  • 3
  • 20
  • 45

12 Answers12

40

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)!)

Swift 4.2:

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

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

Tieme
  • 62,602
  • 20
  • 102
  • 156
Vijay Karthik
  • 457
  • 4
  • 6
16

Swift 4.2

Go straight to YOUR app's settings like this:

if let bundleId = Bundle.main.bundleIdentifier,
    let url = URL(string: "\(UIApplication.openSettingsURLString)&path=LOCATION/\(bundleId)") 
{
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Trev14
  • 3,626
  • 2
  • 31
  • 40
  • 2
    Easier: UIApplication.shared.openURL(URL.init(string: UIApplicationOpenSettingsURLString)!) – Tà Truhoada Dec 26 '17 at 04:36
  • 2
    @TàTruhoada it's useless if Location services are disabled, what you wrote here it's for app location permission but not for enable/disable locations services itself.. you can't change location permissions for your app if location services itself are disabled – user25 Feb 13 '18 at 09:27
  • 4
    As of May 25, 2018 our app got rejected because of using prefs:root under Guideline 2.5.1 - Performance - Software Requirements – Ted May 26 '18 at 07:05
  • question asks about going to the location services page not the app settings page – F.O.O May 15 '21 at 04:08
  • 1
    I just saw that tinder takes you directly to the screen location services. How do they do that? Been searching through all the internet. – Iván Yoed Feb 22 '23 at 09:12
  • Although it's not the precise answer (Monika asked about system services) but it's exactly the answer that I was looking for! – kelin Aug 15 '23 at 11:49
9

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.

Jigar Tarsariya
  • 3,189
  • 3
  • 14
  • 38
  • 1
    After implementing it will it pass App Store review guidelines? – Ramis Jul 21 '17 at 06:48
  • 4
    in iOS 10 I need use the URL App-Prefs:root=Privacy&path=LOCATION. – MMSousa Jul 28 '17 at 20:14
  • @MMSousa in IOS 11 `URL(string: "App-prefs:root=LOCATION_SERVICES")` still works without problems... – user25 Feb 13 '18 at 09:14
  • 15
    As of May 25, 2018 our app got rejected because of using prefs:root under Guideline 2.5.1 - Performance - Software Requirements – Ted May 26 '18 at 07:05
6

Do you want to be safe? use UIApplicationOpenSettingsURLString, which will open the app settings, without deep-link.

Using App-prefs your app will be rejected, as many sub comments said. https://github.com/mauron85/cordova-plugin-background-geolocation/issues/394

Estevão Lucas
  • 4,440
  • 34
  • 37
4

Swift 5+
Easy Way Direct Your said application page will open

if let BUNDLE_IDENTIFIER = Bundle.main.bundleIdentifier,
    let url = URL(string: "\(UIApplication.openSettingsURLString)&path=LOCATION/\(BUNDLE_IDENTIFIER)") {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Shakeel Ahmed
  • 5,361
  • 1
  • 43
  • 34
  • please share the error message why its reject my app is still live with this code – Shakeel Ahmed Jun 18 '20 at 17:46
  • Guideline 2.5.1 Performance - Software Requirements Your app uses the "prefs:root=" non-public URL scheme, which is a private entity. The use of non-public APIs is not permitted on the App Store because it can lead to a poor user experience should these APIs change. Specifically, your app uses the following non-public URL scheme app-prefs:root=privacy&path=location Continuing to use or conceal non-public APIs in future submissions of this app may result in the termination of your Apple Developer account, as well as removal of all associated apps from the App Store – Andrey Jun 19 '20 at 09:38
  • https://stackoverflow.com/questions/50040558/ios-app-store-rejection-your-app-uses-the-prefsroot-non-public-url-scheme – Shakeel Ahmed Jun 19 '20 at 13:39
3

First:

Add URL

Go to Project settings --> Info --> URL Types --> Add New URL Schemes

See image below:

Second:

Use below code to open Location settings:

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

referred from: https://stackoverflow.com/a/35987082/5575752

Community
  • 1
  • 1
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51
3

Step 1: Click on project name >> target>> info >> url Types

enter image description here

Step 2:

-(IBAction)openSettingViewToEnableLocationService:(id)sender
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];
}
Monika Patel
  • 2,287
  • 3
  • 20
  • 45
  • 5
    Apple prohibits the use of this API now, you might not want to use it anymore since it may lead to rejection by app review: https://stackoverflow.com/questions/49059668/appstore-rejection-performance-software-requirements-prefsroot-graphicsser – MartijndeM May 25 '18 at 10:27
  • 1
    App Rejected by Apple. – Sham Dhiman May 30 '22 at 12:53
3

If you set locationManager.startUpdatingLocation() and you have disabled on your iphone, it automatically show you an alertView with the option to open and activated location.

  • This worked exactly one time. It was the first time I tried. Does iOS remembers in any way what option I choose? I know `startUpdatingLocation()` showed me a standard dialog to navigate to the location service settings. And it navigated into the system wide location service settings! But it did that only the first time I called it. Any idea on this? – this.myself Jun 11 '20 at 13:39
  • To reply to previous comment, Apple documentation clearly states that this popup can only be shown ONCE per app lifetime. If you want it to re-appear, you need to restart the app. There are no way to work around this :( – SkyzohKey Jul 14 '21 at 10:12
1

This solution works for me, but I didn't know App Store will reject my app or not

if let bundleId = Bundle.main.bundleIdentifier,
    let url = URL(string: "\(UIApplication.openSettingsURLString)&root=Privacy&path=LOCATION/\(bundleId)") {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
0

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)
}
  • 2
    you can't enable location and change permission for your app if location services itself are disabled in system, so first you have to enable location services (see screenshot from author question) – user25 Feb 13 '18 at 09:05
0

Eureka: Forget about URLs. Use the method requestWhenInUseAuthorization from the CLLocationManager code to open the native prompt message. This is what Tinder and other apps do. Took me a while to figure it out.

This will open a prompt to take you EXACTLY to the Location Services screen:

let locationManager = CLLocationManager()
locationManager.requestWhenInUseAuthorization()
Iván Yoed
  • 3,878
  • 31
  • 44
  • This doesn't seem to do anything if the user has previously disallowed location permissions for the app. From what I can find, this is only supposed to work until the user has made a choice about the location services permissions for the app. – David Rector May 25 '23 at 20:26
  • If he explicitly did so, yes, I think you're right. But if it's the first time he's been asked, I think this should work, even before making a choice, but I could be wrong. In general, after the user decides he does not want to give permission, the only thing left for the app to do is let him know he has to manually enable permissions again. Tinder and other famous apps do that that way too. – Iván Yoed May 26 '23 at 02:39
  • The point was that this doesn't always work as described. Once the user has made a permission choice, this no longer does anything visible in the app. Even uninstalling the app and reinstalling it won't always change the behavior since the system caches the user permission settings for the app. – David Rector Jun 01 '23 at 18:58
  • I see. You have a point. Do you think it's nonetheless useful to leave this answer here? Otherwise I maybe should delete it. – Iván Yoed Jun 01 '23 at 22:09
  • 1
    The answer is useful. It just needed the extra little detail about not always showing the question to the user. – David Rector Jun 02 '23 at 00:06
-1

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)
}
Amrit Sidhu
  • 1,870
  • 1
  • 18
  • 32
  • 4
    As of May 25, 2018 our app got rejected because of this under Guideline 2.5.1 - Performance - Software Requirements – Ted May 26 '18 at 07:04
  • 2
    @Ted even our app got rejected due to this. Do you know alternative to this? or workaround to get this working ? help would be appreciated – Srinivas Guni May 31 '18 at 06:18