ios webView与js交互与类似回调的实现(swift)

来源:互联网 发布:ocr文字识别算法原理 编辑:程序博客网 时间:2024/05/16 08:19
直接代码,首先加载一个webView:
        webView = UIWebView.init(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height))        webView.delegate = self        webView.loadRequest(NSURLRequest(URL: NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("File", ofType: "html")!)))//本地文件测试        webView.scrollView.delegate = self        webView.scrollView.tag = 1        view.addSubview(webView)
实现方法:
 func webViewDidStartLoad(webView: UIWebView) {//        print("开始加载")//        webView.stringByEvaluatingJavaScriptFromString("        document.write(Date());")//与js的交互通过这个方法可以在网页中相当于加入一段js代码//    webView.stringByEvaluatingJavaScriptFromString("document.getElementById('demo').innerHTML = '段落已修改'")    }    //    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {        if navigationType == .LinkClicked {            print("LinkClicked")            //            let uid = webView.stringByEvaluatingJavaScriptFromString("method();")//            print("uid = \(uid!)")            //            if request.URL?.absoluteString == "http://www.baidu.com/"  {//                let secView = WebViewController()//                secView.webView.loadRequest(request)//                self.navigationController?.pushViewController(secView, animated: true)//            }//            if request.URL?.absoluteString == "http://www.google.com/"  {//                let secView = WebViewController()//                secView.webView.loadRequest(request)//                self.navigationController?.pushViewController(secView, animated: true)//            }            return false        }        if navigationType == .BackForward {                    }        if navigationType == .Other {                    }        if navigationType == .FormSubmitted {            print("FormSubmitted")                   }        return true    }        func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {        if (error != nil) {            kError = error        }            }        func webViewDidFinishLoad(webView: UIWebView) {//        print("jiazaijieshu")//        let high = webView.stringByEvaluatingJavaScriptFromString("document.body.scrollHeight;")//此处运行js代码获取页面高度//        print(high!)        let jsContext = webView.valueForKeyPath("documentView.webView.mainFrame.javaScriptContext")//获取当前js运行环境        let webBack: @convention(block) String -> () = { input in            print(input)//            return mutableString as String                    }//设置回调方法  (姑且这么称呼)        //unsafeBitCast为类型转换将type @convention(block) Swift.String -> ()转换为AnyObject,且不会对这个操作进行检查,使用unsafeBitCast要<strong>确保两边类型正确</strong>,应该使用更安全的方法,目前没找到        jsContext!.setObject(unsafeBitCast(webBack, AnyObject.self), forKeyedSubscript: "simplifyString")//在上下文中加入回调    }        func scrollViewDidScroll(scrollView: UIScrollView) {//        if scrollView == webView.scrollView {//            let y = webView.stringByEvaluatingJavaScriptFromString("window.pageYOffset")//            print(y)//        }    }
上面回调的使用是在web的代码中,

 <p><a href="http://www.baidu.com/"id="BaiDu"onclick="theFunc(this)" >去百度</a>  是一个指向万维网上的页面的链接。</p>

<script>

function theFunc(obj) {

        webBack(obj.id)

    }

</script>


0 0