OpenURL在iOS10

所以显然OpenURL已经在iOS 10中折旧了。有没有人有任何文档说明为什么或可以解释下一步该做什么? 我已经查看了苹果网站,发现了一些与OpenURL有关的事情,这就是他们现在所说的:

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

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

如果您使用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)") } } } 

用法:

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

资源

快速修复:

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

空的选项字典将导致与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 

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

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

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

加载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 }]; } 

我希望这有帮助。

让实际:

 [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是你想要打印的string,或者留空。