【问题标题】:The “prefs” URL Scheme not working in iOS 10“prefs” URL 方案在 iOS 10 中不起作用
【发布时间】:2017-02-18 15:28:05
【问题描述】:

我无法让“prefs”URL 方案在 iOS 10 中工作。
设置正确,因为同一个 App 在 iOS 9 上运行良好。

这是一个错误还是被重命名/删除了?

代码:

NSURL *url = [NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"];
if (![[UIApplication sharedApplication]canOpenURL:url]) {
    [[UIApplication sharedApplication]openURL:url]; 
}

【问题讨论】:

  • Xcode 的版本和它有什么关系?
  • @Droppy 我无法让“prefs” URL 方案在 iOS 10 中正常工作。因为同一个应用程序在 iOS 9 上运行良好。
  • 您应该使用这些详细信息更新您的问题。
  • @Droppy 你知道答案

标签: objective-c ios10 xcode8


【解决方案1】:

为了直接打开位置设置。使用以下代码:

// 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"]];
    }
  }

【讨论】:

  • "App-Prefs:root" 或 "prefs:root" 被视为私有 API。避免使用它们,否则应用可能会在审核期间被拒绝。
【解决方案2】:

必须将 prefs 替换为 App-Prefs,这是我的 Swift 辅助方法,支持 iOS 10 及更低版本,已在 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)
        }
    }

}

示例 1 打开位置设置:

Helper.openLocationSettings()

示例 2 打开应用程序设置:

Helper.openSettings(url: UIApplicationOpenSettingsURLString)

【讨论】:

    【解决方案3】:

    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)
    

    使用 iOS 10 打开 URL

    // Objective-C
    UIApplication *application = [UIApplication sharedApplication];
    [application openURL:URL options:@{} completionHandler:nil];
    
    // Swift
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
    

    更多信息,请参考this link

    【讨论】:

    • 此代码也不适用于 iOS 10 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=LOCATION_SERVICES"] options:@{} completionHandler:nil];
    【解决方案4】:

    我建议先验证方法的可用性,然后使用适当的方法。

       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);
       }
    

    希望这会有所帮助。

    【讨论】:

      【解决方案5】:

      将您的方案添加到您的 plist 作为您的 URLSchemes 之一

      【讨论】:

        【解决方案6】:

        prefs:root=LOCATION_SERVICES 适用于 iOS 10 以下...所以检查了 UIApplicationOpenSettingsURLStringApp-Prefs:root=Privacy&amp;path=LOCATION。两者都为我工作....得到@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"]];
             }
        }
        

        在哪里

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

        【讨论】:

          猜你喜欢
          • 2016-10-30
          • 2017-05-16
          • 2012-03-13
          • 2016-10-23
          • 1970-01-01
          • 2016-04-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多