WebView

{
        //混合开发
        let webView = UIWebView(frame: self.view.bounds)
        webView.delegate = self
        
//        let url = NSURL(string: "http://ifeng.com")
//        let request = NSURLRequest(URL: url!)
//        webView.loadRequest(request)
        
//        let htmlStr = "

这是一个标题

" // webView.loadHTMLString(htmlStr, baseURL: nil) //1. 加载本地网页 let url = NSBundle.mainBundle().URLForResource("index", withExtension: "html") // let request = NSURLRequest(URL: url!) // webView.loadRequest(request) //2. let data = NSData(contentsOfURL: url!) webView.loadData(data!, MIMEType: "text/html", textEncodingName: "utf-8", baseURL: NSURL()) self.view.addSubview(webView) } //1. 开始加载的时机 //2. 可以过滤网址 func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool { let path = request.URL?.absoluteString print(path!) if path!.containsString("baidu.com") { return false } return true } func webViewDidStartLoad(webView: UIWebView) { // } func webViewDidFinishLoad(webView: UIWebView) { //如果需要操作网页,必须等加载完成 let res = webView.stringByEvaluatingJavaScriptFromString("document.getElementsByTagName('img')") print(res) //w3school.com.cn } func webView(webView: UIWebView, didFailLoadWithError error: NSError?) { } }

你可能感兴趣的:(WebView)