一、Safari中打开网页
//app未上架,所以用苹果的网站
if let url = URL(string: "http://www.apple.com/itunes/charts/paid-apps") {
UIApplication.shared.open(url)
}
二、UIWebView
if let url = URL(string: "https://www.jianshu.com/u/21c35a95919e") {
let request = URLRequest(url: url)
webView.loadRequest(request)
}
三、WKWebView
WKWebView比UIWebView速度更快,内存占用更少。是UIWebView的替代者
@1、头文件
import WebKit
@2、初始化
let wkWebView = WKWebView(frame: view.frame)
view.addSubview(wkWebView)
@3、发送请求
if let url = URL(string: "https://www.jianshu.com/u/21c35a95919e") {
let request = URLRequest(url: url)
wkWebView.load(request)
}
@4、可能存在的bug
//WKWebView高度自适应
wkWebView.autoresizingMask = [.flexibleHeight]
四、使用SFSafairViewController
@1、头文件
import SafariServices
@2、详细代码
if let url = URL(string: links[indexPath.row]) {
let sfVC = SFSafariViewController(url: url)
present(sfVC, animated: true, completion: nil)
}