iOS10中的OpenURL

KSi*_*att 45 xcode openurl ios swift

显然OpenURL已经在iOS 10中被折旧.有没有人有任何关于为什么或可以解释下一步做什么的文档?我已经查看了Apple网站,发现了一些与OpenURL相关的内容,这就是他们现在所说的:

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

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

Asy*_*nc- 62

如果您使用iOS10兼容代码更新应用程序,也可以使用条件检查:

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)")
     }
   }
 }
Run Code Online (Sandbox Code Playgroud)

用法:

open(scheme: "tweetbot://timeline")
Run Code Online (Sandbox Code Playgroud)

资源


Ed.*_*Ed. 42

快速修复:

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

// Swift
UIApplication.shared.open(url, options: [:], completionHandler: nil)
Run Code Online (Sandbox Code Playgroud)

一个完整的答案:

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

致谢:Keith Harrison(useyourloaf.com)


小智 13

一个空的字典选项将导致相同的行为的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                                                                          |
+-------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud)

来自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
Run Code Online (Sandbox Code Playgroud)


Yas*_*sir 5

新的UIApplication方法openURL:options:completionHandler:,它是异步执行的,并在主队列上调用指定的完成处理程序(此方法替换openURL :).

这是在其他框架更改 > UIKit 下:https://developer.apple.com/library/prerelease/content/releasenotes/General/WhatsNewIniOS/Articles/iOS10.html