【问题标题】:OpenURL in iOS10iOS10中的OpenURL
【发布时间】:2016-12-22 04:49:14
【问题描述】:

显然OpenURL 在 iOS 10 中已被弃用。是否有人有任何文档说明原因或可以解释下一步该做什么?我已经查看了 Apple 网站,发现了一些与 OpenURL 相关的内容,这就是他们说现在要使用的内容:

UIApplication.shared().open(url: URL, options: [String: AnyObject], completionHandler: ((Bool) -> Void)?)

是否有任何证据表明这是在 Swift 3.0 中使用 OpenURL 的新方法?另外options:completionHandler:参数分别使用什么值?

【问题讨论】:

标签: ios swift xcode openurl


【解决方案1】:

Swift 3+

func open(scheme: String) {
   if let url = URL(string: scheme) {
      if #available(iOS 10, *) {
         UIApplication.shared.open(url, options: [:],
           completionHandler: {
               (success) in
                  print("Open \(scheme): \(success)")
           })
     } else {
         let success = UIApplication.shared.openURL(url)
         print("Open \(scheme): \(success)")
     }
   }
 }

用法:

open(scheme: "tweetbot://timeline")

Source

【讨论】:

  • 此示例仅适用于 Swift 3.0。太棒了!
  • 谢谢,我已经适应了这个,现在正在使用它。
  • 使用这个 let success = UIApplication.shared().openURL(url) 而不是这个 let success = UIApplication.shared.openURL(url)
【解决方案2】:

快速修复:

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

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

完整答案:

http://useyourloaf.com/blog/openurl-deprecated-in-ios10/

致谢:Keith Harrison (useyourloaf.com)

【讨论】:

    【解决方案3】:

    empty 选项字典将产生与 openUrl 相同的行为。

    否则:

    +-------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
    | UIApplicationOpenURLOptionsSourceApplicationKey | NSString containing the bundle ID of the originating application                                                                             |
    +-------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
    | UIApplicationOpenURLOptionsAnnotationKey        | property-list typed object corresponding to what the originating application passed in UIDocumentInteractionController's annotation property |
    +-------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
    | UIApplicationOpenURLOptionsOpenInPlaceKey       | bool NSNumber, set to YES if the file needs to be copied before use                                                                          |
    +-------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
    

    来自 UIApplication.h

    // Options are specified in the section below for openURL options. An empty options dictionary will result in the same
    // behavior as the older openURL call, aside from the fact that this is asynchronous and calls the completion handler rather
    // than returning a result.
    // The completion handler is called on the main queue.
    - (void)openURL:(NSURL*)url options:(NSDictionary<NSString *, id> *)options completionHandler:(void (^ __nullable)(BOOL success))completion NS_AVAILABLE_IOS(10_0) NS_EXTENSION_UNAVAILABLE_IOS("");
    
    UIKIT_EXTERN UIApplicationOpenURLOptionsKey const UIApplicationOpenURLOptionsSourceApplicationKey NS_SWIFT_NAME(sourceApplication) NS_AVAILABLE_IOS(9_0);   // value is an NSString containing the bundle ID of the originating application
    UIKIT_EXTERN UIApplicationOpenURLOptionsKey const UIApplicationOpenURLOptionsAnnotationKey NS_SWIFT_NAME(annotation) NS_AVAILABLE_IOS(9_0);   // value is a property-list typed object corresponding to what the originating application passed in UIDocumentInteractionController's annotation property
    UIKIT_EXTERN UIApplicationOpenURLOptionsKey const UIApplicationOpenURLOptionsOpenInPlaceKey NS_SWIFT_NAME(openInPlace) NS_AVAILABLE_IOS(9_0);   // value is a bool NSNumber, set to YES if the file needs to be copied before use
    

    【讨论】:

      【解决方案4】:

      新增UIApplication方法openURL:options:completionHandler:,异步执行,调用主队列上指定的completion handler(该方法替代openURL:)。

      这在 Additional Framework Changes > UIKit 下:https://developer.apple.com/library/prerelease/content/releasenotes/General/WhatsNewIniOS/Articles/iOS10.html

      【讨论】:

      • 这样回答了问题,但并不完全。 openURL:options:completionHandler: 是我们应该使用的新功能,还是我在问题中建议的功能?你仍然使用UIApplication.shared()...
      • 从我读到的,它肯定表明openURL:options:com‌​pletionHandler: 函数是openUrl 的替代品。并非所有文档都已更新,some 仍指向已弃用的代码。
      【解决方案5】:

      在加载 url 之前,您需要先进行一些检查。请检查以下代码。

      if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"https://www.gmail.com"]]){
          [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.gmail.com"] options:@{} completionHandler:^(BOOL success) {
                                      //completion codes here
                                  }];
      }
      

      我希望这会有所帮助。

      【讨论】:

        【解决方案6】:

        如果您的应用仍支持iOS 9 或更低版本,请继续使用旧的openURL。如果您的部署目标是iOS 10,您应该只迁移到新的。

        【讨论】:

          【解决方案7】:

          让实际:

          [String: AnyObject] = ["xxx key": "xxx value" as AnyObject, "yyy key": "yyy value" as AnyObject]
          
          UIApplication.shared.open(URL(string: "http:google.com")!, options: actual, completionHandler: {(true) -> Swift.Void in
          print("Refresh")
          })
          

          其中 xxx 和 yyy 是您要打印的任何字符串或将它们留空。

          【讨论】:

            【解决方案8】:

            您可以使用函数打开设置:

            func showAlert(title:String, message:String){
                    let alert = UIAlertController(title: title,
                                                  message: message,
                                                  preferredStyle: UIAlertController.Style.alert)
            
                    let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
                    alert.addAction(okAction)
            
                    let settingsAction = UIAlertAction(title: "Settings", style: .default, handler: { _ in
                        // Take the user to Settings app to possibly change permission.
                        guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else { return }
                        if UIApplication.shared.canOpenURL(settingsUrl) {
                            if #available(iOS 10.0, *) {
                                UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
                                    // Finished opening URL
                                })
                            } else {
                                // Fallback on earlier versions
                                UIApplication.shared.openURL(settingsUrl)
                            }
                        }
                    })
                    alert.addAction(settingsAction)
                    present(alert, animated: true, completion: nil)
                }
            

            像下面这样调用这个函数

            showAlert(title: "Unable to access the Photos", message: "To enable access, go to Settings > Privacy > Photos and turn on Photos access for this app.")
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-01-14
              • 1970-01-01
              • 1970-01-01
              • 2017-03-19
              • 2010-12-21
              • 1970-01-01
              相关资源
              最近更新 更多