2

I can't get the "prefs" URL Scheme to work in iOS 10.
It's set up correctly since the same App works fine on iOS 9.

Is this a bug or did it get renamed / removed?

Code:

NSURL *url = [NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"];
if (![[UIApplication sharedApplication]canOpenURL:url]) {
    [[UIApplication sharedApplication]openURL:url]; 
}
SG iOS Developer
  • 158
  • 2
  • 10

6 Answers6

3

In order directly open location settings. Use the below code:

// This will open ios devices location settings in iOS 10
  if ([UIApplication instancesRespondToSelector:NSSelectorFromString(@"openURL:options:completionHandler:")]) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"App-Prefs:root=Privacy&path=LOCATION"] options:@{} completionHandler:nil];
  }
  else {
// This will open ios devices location settings in below iOS 10
    if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]]) {
      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"]];
    }
  }
Rajan Balana
  • 3,775
  • 25
  • 42
  • 1
    "App-Prefs:root" or "prefs:root" are considered as private API's. Avoid using them otherwise the app may get rejected during review. – Tejas K Jul 24 '18 at 09:05
3

Got to replace prefs with App-Prefs, here is my Swift helper methods to support both iOS 10 and lower, tested on iOS 9 & 10

class Helper {

    static func openLocationSettings() {
        if #available(iOS 10.0, *) {
            openSettings(url: "App-Prefs:root=Privacy&path=LOCATION")
        } else {
            openSettings(url: "prefs:root=LOCATION_SERVICES")
        }
    }

    static func openSettings(url: String) {
        guard let settingsUrl = URL(string: url), UIApplication.shared.canOpenURL(settingsUrl) else { return }

        if #available(iOS 10.0, *) {
            UIApplication.shared.open(settingsUrl)
        } else {
            UIApplication.shared.openURL(settingsUrl)
        }
    }

}

Example 1 Open Location Settings:

Helper.openLocationSettings()

Example 2 Open Application Settings:

Helper.openSettings(url: UIApplicationOpenSettingsURLString)
AamirR
  • 11,672
  • 4
  • 59
  • 73
2

The new method in iOS 10:

//Objective-C
- (void)openURL:(NSURL*)url options:(NSDictionary<NSString *, id> *)options
  completionHandler:(void (^ __nullable)(BOOL success))completion


// Swift
open func open(_ url: URL, options: [String : Any] = [:],
  completionHandler completion: (@escaping (Bool) -> Swift.Void)? = nil)

Opening a URL with iOS 10

// Objective-C
UIApplication *application = [UIApplication sharedApplication];
[application openURL:URL options:@{} completionHandler:nil];

// Swift
UIApplication.shared.open(url, options: [:], completionHandler: nil)

For more info, refer to this link

Mutawe
  • 6,464
  • 3
  • 47
  • 90
  • This code also not working for iOS 10 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"] options:@{} completionHandler:nil]; – SG iOS Developer Oct 12 '16 at 13:11
0

I would suggest to verify method availability first and then use appropriate method.

   if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) 
   {
      [application openURL:URL options:@{}
         completionHandler:^(BOOL success) {

            NSLog(@"Open URL Scheme %@: %d",scheme,success);
      }];
   } 
   else 
   {
      BOOL success = [application openURL:URL];
      NSLog(@"Open URL Scheme %@: %d",scheme,success);
   }

Hope this helps.

Mrunal
  • 13,982
  • 6
  • 52
  • 96
0

Add your scheme to your plist as one of your URLSchemes

Au Ris
  • 4,541
  • 2
  • 26
  • 53
0

prefs:root=LOCATION_SERVICES is working for below iOS 10...so checked for UIApplicationOpenSettingsURLString and App-Prefs:root=Privacy&path=LOCATION. Both are working for me.... Got hint by @Rajan Balan.

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]]) {
     if(SYSTEM_VERSION_GREATER_THAN(@"10")) {
          [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"App-Prefs:root=Privacy&path=LOCATION"]];

          // Below code is also working in iOS 10 to open location services
          // [[UIApplication sharedApplication] openURL:[NSURL URLWithString: UIApplicationOpenSettingsURLString]];

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

Where

#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)
Sujay
  • 2,510
  • 2
  • 27
  • 47