ios 如何在 Swift3 中打开一个 URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39546856/
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
how to open an URL in Swift3
提问by Shane O'Seasnain
openURL
has been deprecated in Swift3. Can anyone provide some examples of how the replacement openURL:options:completionHandler:
works when trying to open an url?
openURL
已在 Swift3 中弃用。任何人都可以提供一些openURL:options:completionHandler:
尝试打开 url 时替换如何工作的示例吗?
回答by Devran Cosmo Uenal
All you need is:
所有你需要的是:
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)
}
回答by Nirav D
Above answer is correct but if you want to check you canOpenUrl
or not try like this.
以上答案是正确的,但如果您想检查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)")
})
}
Note:If you do not want to handle completion you can also write like this.
注意:如果你不想处理完成你也可以这样写。
UIApplication.shared.open(url, options: [:])
No need to write completionHandler
as it contains default value nil
, check apple documentationfor more detail.
无需编写,completionHandler
因为它包含默认值nil
,请查看苹果文档以获取更多详细信息。
回答by Chetan Rajagiri
If you want to open inside the app itself instead of leaving the app you can import SafariServicesand work it out.
如果您想在应用程序内部打开而不是离开应用程序,您可以导入 SafariServices并解决它。
import UIKit
import SafariServices
let url = URL(string: "https://www.google.com")
let vc = SFSafariViewController(url: url!)
present(vc, animated: true, completion: nil)
回答by Demosthese
Swift 3version
斯威夫特 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)
}
}
回答by Scott Maretick
I'm using macOS Sierra (v10.12.1) Xcode v8.1 Swift 3.0.1 and here's what worked for me in ViewController.swift:
我正在使用 macOS Sierra (v10.12.1) Xcode v8.1 Swift 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
import WebKit
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.
}
};
回答by Salman Khan
import UIKit
import SafariServices
let url = URL(string: "https://sprotechs.com")
let vc = SFSafariViewController(url: url!)
present(vc, animated: true, completion: nil)