import UIKit
import WebKitclassRootViewController: UIViewController, WKNavigationDelegate {var webView: WKWebView!
overridefunc viewDidLoad() {
super.viewDidLoad()//建立視圖
let screen =UIScreen.main.bounds//按鈕欄
let buttonBarWidth:CGFloat = 316let buttonBar= UIView(frame: CGRect(x: (screen.size.width - buttonBarWidth)/2, y: 20, width:buttonBarWidth, height: 70))
self.view.addSubview(buttonBar)
let width= buttonBar.frame.size.width / 2 - 10
//添加按鈕
let buttonHTML =UIButton(type: UIButtonType.system)
buttonHTML.setTitle("通過路徑加載", for: UIControlState())
buttonHTML.frame= CGRect(x:0, y:0, width: width, height: 30)//添加事件
buttonHTML.addTarget(self, action: #selector(buttonHTML(_:)), for: .touchUpInside)
buttonBar.addSubview(buttonHTML)//添加 loadHTMLString 按鈕
let loadHTMLString =UIButton(type: UIButtonType.system)
loadHTMLString.setTitle("loadHTMLString", for: UIControlState())
loadHTMLString.frame= CGRect(x: width + 10, y:0, width: width, height: 30)//添加事件
loadHTMLString.addTarget(self, action: #selector(loadHTMLString(_:)), for: .touchUpInside)
buttonBar.addSubview(loadHTMLString)//添加loadData按鈕
let loadData =UIButton(type: UIButtonType.system)
loadData.setTitle("loadData", for: UIControlState())
loadData.frame= CGRect(x:0, y: 40, width: width, height: 30)//添加事件
loadData.addTarget(self, action: #selector(loadData(_:)), for: .touchUpInside)
buttonBar.addSubview(loadData)//添加loadRequest按鈕--- 加載網絡位址
let loadRequest =UIButton(type: UIButtonType.system)
loadRequest.setTitle("loadRequest", for: UIControlState())
loadRequest.frame= CGRect(x: width + 10, y: 40, width: width, height: 30)//添加事件
loadRequest.addTarget(self, action: #selector(loadRequest(_:)), for: .touchUpInside)
buttonBar.addSubview(loadRequest)//添加WKWebView
self.webView = WKWebView(frame: CGRect(x:0, y: 100, width: screen.size.width, height: screen.size.height - 100))
self.view.addSubview(webView)
}//加載本地html 點選事件
@objc func buttonHTML(_ sender: AnyObject){//方法一 加載路徑
let htmlPath = Bundle.main.path(forResource: "index", ofType: "html")if let htmlPath =htmlPath {
let url= URL.init(fileURLWithPath: htmlPath) //把字元串 轉成 URL 類型
let request =URLRequest(url: url)
self.webView.load(request)
self.webView.navigationDelegate=self
}//方法二 加載路徑//let htmlPath = Bundle.main.url(forResource: "index", withExtension: "html")//if let htmlPath = htmlPath{//let request = URLRequest(url: htmlPath)//self.webView.load(request)//self.webView.navigationDelegate = self//}
}//loadHTMLString
@objc func loadHTMLString(_ sender: AnyObject){//方法一 直接加載 html 字元串//let bundleUrl = NSURL.fileURL(withPath: Bundle.main.bundlePath)//self.webView.loadHTMLString("測試
測試标題", baseURL: bundleUrl)//方法二 把本地檔案轉成字元串,進行加載
let htmlPath = Bundle.main.path(forResource: "index2", ofType: "html")
let bundleUrl=NSURL.fileURL(withPath: Bundle.main.bundlePath)do{
let html= try NSString(contentsOfFile: htmlPath!, encoding: String.Encoding.utf8.rawValue)
self.webView.loadHTMLString(htmlasString, baseURL: bundleUrl)
}catch let err asNSError{
err.description
}
}//loadData 方式加載 -- > loadData 已經轉換成--> load
@objc func loadData(_ sender: AnyObject){
let htmlPath= Bundle.main.path(forResource: "index3", ofType: "html")
let bundleUrl=NSURL.fileURL(withPath: Bundle.main.bundlePath)
let htmlData= NSData(contentsOfFile: htmlPath!)//NSData 要先轉換成 Data 類型
self.webView.load(htmlData! as Data, mimeType: "text/html", characterEncodingName: "utf-8", baseURL: bundleUrl)
}//loadRequest 加載網絡路徑 ---> loadRequest 已經改為 load , NSURL --> URL,NSURLRequest --> URLRequest
@objc func loadRequest(_ sender: AnyObject){
let url= URL(string: "https://baidu.com")
let request= URLRequest(url: url!) self.webView.load(request)
self.webView.navigationDelegate=self
}
// 委托協定的監聽方法
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
print("開始加載")
}
func webView(_ webView: WKWebView, didCommit navigation: WKNavigation!) {
print("内容開始傳回")
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
print("加載完成")
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
print("加載失敗 error:" +error.localizedDescription)
}
}