ios iOS10 中的 OpenURL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38964264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OpenURL in iOS10
提问by KSigWyatt
So apparently OpenURL
has been deprecated in iOS 10. Does anyone have any documentation on why or can explain what to do next? I looked on the Apple site already and found a few things pertaining to OpenURL and this is what they say to use now:
所以显然OpenURL
在 iOS 10 中已被弃用。 有没有人有任何关于为什么或可以解释下一步做什么的文档?我已经查看了 Apple 网站,发现了一些与 OpenURL 相关的内容,这就是他们现在所说的:
UIApplication.shared().open(url: URL, options: [String: AnyObject], completionHandler: ((Bool) -> Void)?)
Does anyone have any evidence that this is the new way to use OpenURL in Swift 3.0? In addition what values are to be used in the options:
and completionHandler:
parameters respectively?
有没有人有任何证据表明这是在 Swift 3.0 中使用 OpenURL 的新方法?另外分别在options:
和completionHandler:
参数中使用什么值?
回答by Async-
Swift 3+:
斯威夫特 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)")
}
}
}
Usage:
用法:
open(scheme: "tweetbot://timeline")
回答by Ed.
A quick fix:
快速修复:
// Objective-C
UIApplication *application = [UIApplication sharedApplication];
[application openURL:URL options:@{} completionHandler:nil];
// Swift
UIApplication.shared.open(url, options: [:], completionHandler: nil)
A complete answer:
一个完整的答案:
http://useyourloaf.com/blog/openurl-deprecated-in-ios10/
http://useyourloaf.com/blog/openurl-deprecated-in-ios10/
Credits: Keith Harrison (useyourloaf.com)
学分:基思哈里森(useyourloaf.com)
回答by scol
An emptyoptions dictionary will result in the same behaviour as openUrl.
一个空的字典选项将导致相同的行为的OpenURL。
Otherwise:
除此以外:
+-------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
| 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 |
+-------------------------------------------------+----------------------------------------------------------------------------------------------------------------------------------------------+
From UIApplication.h
来自 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
回答by Yasir
The new UIApplication method openURL:options:completionHandler:, which is executed asynchronously and calls the specified completion handler on the main queue (this method replaces openURL:).
新的 UIApplication 方法 openURL:options:completionHandler:,异步执行并调用主队列上指定的完成处理程序(此方法替换 openURL:)。
This is under Additional Framework Changes> UIKitat: https://developer.apple.com/library/prerelease/content/releasenotes/General/WhatsNewIniOS/Articles/iOS10.html
这是在其他框架更改> UIKit下:https: //developer.apple.com/library/prerelease/content/releasenotes/General/WhatsNewIniOS/Articles/iOS10.html
回答by handiansom
You need to do some checking first before loading the url. Please check the codes below.
在加载 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
}];
}
I hope this helps.
我希望这有帮助。
回答by Mushtaque Ahmed
let actual:
让实际:
[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")
})
Where xxx and yyy are any string you want to print or leave them blank .
其中 xxx 和 yyy 是您想要打印或留空的任何字符串。
回答by Mushtaque Ahmed
If your app still support iOS 9
or lower, just keep using the old openURL
. You should only move to the new one if your Deployment Target is iOS 10
.
如果您的应用程序仍然支持iOS 9
或更低,请继续使用旧的openURL
. 如果您的部署目标是 ,您应该只移动到新的iOS 10
。
回答by Prashant Sharma
you can use function to open settings:
您可以使用函数打开设置:
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)
}
Call this function like this given below
像下面这样调用这个函数
showAlert(title: "Unable to access the Photos", message: "To enable access, go to Settings > Privacy > Photos and turn on Photos access for this app.")