openURL
在Swift3中已被弃用。任何人都可以提供一些替代openURL:options:completionHandler:
试图打开一个网址时如何工作的例子吗?如何在Swift3中打开一个URL
77
A
回答
233
所有你需要的是:
guard let url = URL(string: "http://www.google.com") else {
return //be safe
}
if #available(iOS 10.0, *) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(url)
}
19
以上的答案是正确的,但如果你想查询你canOpenUrl
还是不要尝试这样的。
let url = URL(string: "http://www.facebook.com")!
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
//If you want handle the completion block than
UIApplication.shared.open(url, options: [:], completionHandler: { (success) in
print("Open url : \(success)")
})
}
注:如果您不希望处理完成后,您还可以这样写。
UIApplication.shared.open(url, options: [:])
无需编写completionHandler
因为它包含的是默认值nil
,检查apple documentation更多细节。
6
斯威夫特3版本
import UIKit
protocol PhoneCalling {
func call(phoneNumber: String)
}
extension PhoneCalling {
func call(phoneNumber: String) {
let cleanNumber = phoneNumber.replacingOccurrences(of: " ", with: "").replacingOccurrences(of: "-", with: "")
guard let number = URL(string: "telprompt://" + cleanNumber) else { return }
UIApplication.shared.open(number, options: [:], completionHandler: nil)
}
}
+0
您可以使用'replacesOccurrences'的正则表达式。 – Sulthan
2
我使用MacOS的塞拉利昂(v10.12.1)的Xcode V8.1斯威夫特3.0.1,这里是我在ViewController.swift什么工作:
//
// ViewController.swift
// UIWebViewExample
//
// Created by Scott Maretick on 1/2/17.
// Copyright © 2017 Scott Maretick. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
//added this code
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Your webView code goes here
let url = URL(string: "https://www.google.com")
if UIApplication.shared.canOpenURL(url!) {
UIApplication.shared.open(url!, options: [:], completionHandler: nil)
//If you want handle the completion block than
UIApplication.shared.open(url!, options: [:], completionHandler: { (success) in
print("Open url : \(success)")
})
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
};
6
如果你想打开应用程序本身,而不是离开应用程序,你可以导入SafariServices并解决。
import UIKit
import SafariServices
let url = URL(string: "https://www.google.com")
let vc = SFSafariViewController(url: url!)
present(vc, animated: true, completion: nil)
相关问题
- 1. 如何从终端打开一个URL
- 2. 在新标签中打开一个URL
- 3. 在applescript中打开一个url
- 4. 在eclipse中如何让一个打开的url在
- 5. 如何在一个标签中一个接一个地打开不同的url?
- 6. 如何打开一个URL并在Python中提取信息
- 7. 如何在另一个窗口中检查打开的URL?
- 8. 如何在JavaScript中的同一个窗口中打开一个URL?
- 9. 如何在标签中打印uiview(swift3)
- 10. 如何在python中打开url
- 11. 如何在android中打开RSS feed url?
- 12. 如何在rails中打开URL?
- 13. 打开一个URL,在jQuery的PHP
- 14. 从servlet中打开一个URL
- 15. 从ibooks中打开一个url崩溃
- 16. 如何创建一个imageView来打开一个url?
- 17. 安全地通过一个URL通过URL在iframe中打开
- 18. Django url唯一url打开
- 19. 如何在使用intent时使用firefox focus打开一个url?
- 20. 微小的URL,从URL列表中随机打开一个URL?
- 21. 如何在android中使用uiautomation在chrome浏览器中打开一个url
- 22. 如何在新窗口中打开一个URL不在新窗口中
- 23. aframevr如何打开url
- 24. 如何用apple脚本在Safari中打开一个新窗口和多个URL?
- 25. 在MFC中打开多个URL
- 26. Swift3 iOS - 如何搜索URL数组中的URL [URL?]
- 27. 如何使用javascript在同一窗口中打开URL?
- 28. 在Safari中打开URL
- 29. 在codeigniter中打开url feed
- 30. 如何在Windows上打开URL列表
如果我在我的网址中使用“+”运算符,该怎么办?例如:“https://xxxxx.com./xxxxxxxxxxxxxx="+userName+"xxxxxxx="+userPass+"&xxxxxxxxx”就是这样。该字符串给了我一个错误“否'+'候选人产生预期的上下文结果类型'网址' –
你必须在'字符串'上使用+运算符而不是'URL' –
注意:不要尝试执行这个:UIApplication.shared.openURL(URL(string:“insert url here”)!)。 XCode 8上的编译器会感到困惑,无法正确构建。因此,请按原样使用此解决方案。很棒!谢谢。 – Joel