swift 4.2用代码实现UIWebView

viewcontroller.swift:

import UIKit
import WebKit

class ViewController: UIViewController , WKNavigationDelegate {

    var webView: WKWebView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        let screen = UIScreen.main.bounds
        //按钮栏
        //按钮栏宽
        let buttonBarWidth: CGFloat = 316
        let buttonBar = UIView(frame: CGRect(x: (screen.size.width - buttonBarWidth)/2, y: 20, width: buttonBarWidth, height: 30))
        self.view.addSubview(buttonBar)
        
        //1.添加LoadHTMLString按钮
        let buttonLoadHTMLString = UIButton(type: UIButton.ButtonType.system)
        buttonLoadHTMLString.setTitle("LoadHTMLString", for: UIControl.State())
        buttonLoadHTMLString.frame = CGRect(x: 0, y: 0, width: 117, height: 30)
        buttonLoadHTMLString.addTarget(self, action: #selector(testLoadHTMLString(_:)), for: .touchUpInside)
        buttonBar.addSubview(buttonLoadHTMLString)
        
        //2.添加LoadData按钮
        let buttonLoadData = UIButton(type: UIButton.ButtonType.system)
        buttonLoadData.setTitle("LoadData", for: UIControl.State())
        buttonLoadData.frame = CGRect(x: 137, y: 0, width: 67, height: 30)
        buttonLoadData.addTarget(self, action: #selector(testLoadData(_:)), for: .touchUpInside)
        buttonBar.addSubview(buttonLoadData)
        
        //3. 添加LoadRequest按钮
        let buttonLoadRequest = UIButton(type: UIButton.ButtonType.system)
        buttonLoadRequest.setTitle("LoadRequest", for: UIControl.State())
        buttonLoadRequest.frame = CGRect(x: 224, y: 0, width: 92, height: 30)
        buttonLoadRequest.addTarget(self, action: #selector(testLoadRequest(_:)), for: .touchUpInside)
        buttonBar.addSubview(buttonLoadRequest)
        
        //4. 添加WKWebView
        self.webView = WKWebView(frame: CGRect(x: 0, y: 60, width: screen.size.width, height: screen.size.height - 80))
        self.view.addSubview(self.webView)
    }
    
    @objc func testLoadHTMLString(_ sender: AnyObject) {
        let htmlPath = Bundle.main.path(forResource: "index" , 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(html as String, baseURL: bundleUrl)
        } catch let err as NSError {
            err.description
        }
    }
    
    @objc func testLoadData(_ sender: AnyObject){
        let htmlPath = Bundle.main.path(forResource: "index", ofType: "html")
        let bundleUrl = NSURL.init(fileURLWithPath: Bundle.main.bundlePath)

        let htmlData = NSData(contentsOfFile: htmlPath!)
        
        self.webView.load(htmlData! as Data, mimeType: "text/html", characterEncodingName: "UTF-8", baseURL: bundleUrl as URL)
        
    }
    
    @objc func testLoadRequest(_ sender: AnyObject) {
        let url = NSURL(string:"https://www.baidu.com")
        let request = NSURLRequest(url: url! as URL)
        self.webView.load(request as URLRequest)
        self.webView.navigationDelegate = self
    }
    
    //实现WKNavigationDelegate委托协议
    //开始加载时调用
    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("加载失败")
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值