WebView

作者: Dove_Q | 来源:发表于2016-08-18 14:36 被阅读6次
{
        //混合开发
        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 = "<h1>这是一个标题</h1>"
//        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

      本文链接:https://www.haomeiwen.com/subject/cauvsttx.html