56

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

enter image description here

CopsOnRoad
  • 175,842
  • 51
  • 533
  • 380
Monika Patel
  • 2,107
  • 3
  • 18
  • 41

10 Answers10

36

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
  • 58,835
  • 19
  • 97
  • 154
Vijay Karthik
  • 417
  • 4
  • 6
14

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,016
  • 2
  • 26
  • 34
  • 2
    Easier: UIApplication.shared.openURL(URL.init(string: UIApplicationOpenSettingsURLString)!) – Tà Truhoada Dec 26 '17 at 04:36
  • @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
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,101
  • 3
  • 13
  • 36
  • 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
  • 13
    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
5

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,030
  • 31
  • 37
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,225
  • 3
  • 23
  • 50
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,107
  • 3
  • 18
  • 41
  • 4
    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
  • App Rejected by Apple. – RsD 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
3

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
  • 4,335
  • 36
  • 31
  • 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
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
-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,792
  • 1
  • 15
  • 31
  • 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