var webview = UIWebView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: webviewHeight))
webview.delegate = self
webview.scrollView.bounces = false
webview.loadRequest(NSURLRequest(URL: NSURL(string: _url)!))
self.view.addSubview(webview)
1.swift调用js
func webViewDidFinishLoad(webView: UIWebView) {
self.webview.stringByEvaluatingJavaScriptFromString("document.body.scrollHeight")//正常获取网页内容长度的方法
}
2.js调用swift
客户端定义好方法,并且与服务端约定好协议 scheme+host+query
如:myapp://refresh?type=1&data=2
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
guard let url = request.URL else{
return true
}
guard url.scheme == "myapp"{
return true
}
guard let action == url.host else{
return true
}
let parStr = url.query ?? ""
handleAction(action, par: par)
return true
}
func handleAction(action:String,par:String){
if action == " refresh"{
let parDict = par.toURLParameterDict()
let type = parDict["type"] as! Int
let data = parDict["data"] as! String
if type == 1{
refreshView(data)
}
}
}
//执行原生事件
func refreshView(data:Int){
//handleData
...
self.tableView.reloadData()
}
//解析URL参数转换成Dictionary
extension String{
func toURLParameterDict()->NSDictionary{
guard !self.isEmpty{
return NSDictionary()
}
let dict = NSMutableDictionary()
let arr = self.componentsSeparatedByString("&")
for one in arr{
let pararr = one.componentsSeparatedByString("=")
let key = pararr.first!
let value = pararr.last!
dict[key] = value
}
return NSDictionary(dictionary: dict)
}
}
3.处理js中ajax加载完后cell内容自适应(webview在tableView中的情况)
**方法1:通过kov监听webview的ajax加载完成的属性,来重新获取webview内容高度
//TODO:理论是可实现的,后续添加
**方法2:通过js调用原生告知ajax加载完成
myapp://common.loadfinish
func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
guard let url = request.URL else{
return true
}
guard url.scheme == "myapp"{
return true
}
guard let action == url.host else{
return true
}
if action = "common.loadfinish"{
let newHeight = CGFloat(Float(webView.stringByEvaluatingJavaScriptFromString("document.body.scrollHeight")!)!)
...
//reloadTableView
}
4.处理h5中的webp图片不显示
google:
1.get webp image url ;
2.download image ;
3.use base64 library ,embed same data with <img src> tag.
**思路:
<1>获取html中所有的图片
<2>如果是webp图片需要替换成base64数据
<3>将新的图片数据写入html标签中
func webViewDidFinishLoad(webView: UIWebView) {
guard let oldimgurl = webView.stringByEvaluatingJavaScriptFromString("function getlist (){var imglist='';$.each($('#news-article img'), function(i){if (i == 0){imglist = $(this).attr('src')}else{imglist = imglist+','+$(this).attr('src')}});return imglist;} getlist();") else{
return
}
let imageurllist = oldimgurl.toImgArr()
var imgliststr = ""
for i in 0..<imageurllist.count{
var reaultstr = ""
if i != 0{
reaultstr+=","
}
let defaultimgstr = UIImagePNGRepresentation(defaultImg!)!.base64EncodedStringWithOptions(.Encoding64CharacterLineLength).encodeToURL()
guard let url = NSURL(string:imageurllist[i]) else{
reaultstr+=defaultimgstr
imgliststr+=reaultstr
return
}
guard let data = NSData(contentsOfURL: url) else{
reaultstr+=defaultimgstr
imgliststr+=reaultstr
return
}
if data.isWebP(){
let img = UIImage(webPData: data)
guard let pngdata = UIImagePNGRepresentation(img) else{
reaultstr+=defaultimgstr
imgliststr+=reaultstr
return
}
let base64str = pngdata.base64EncodedStringWithOptions(.Encoding64CharacterLineLength).encodeToURL()
reaultstr+=base64str
imgliststr+=reaultstr
}else{
reaultstr+=imageurllist[i]
imgliststr+=reaultstr
}
}
webView.stringByEvaluatingJavaScriptFromString("function setlist(){var imglist = '\(imgliststr)';var imglist_arr = imglist.split(',');$.each($('#news-article img'), function(i) {if(imglist_arr[i].indexOf('http')>=0){$(this).attr('src', imglist_arr[i]);}else{$(this).attr('src', 'data:image/png;base64,' + imglist_arr[i]);}});}setlist();")
}