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調(diào)用js
func webViewDidFinishLoad(webView: UIWebView) {
self.webview.stringByEvaluatingJavaScriptFromString("document.body.scrollHeight")//正常獲取網(wǎng)頁內(nèi)容長度的方法
}
2.js調(diào)用swift
客戶端定義好方法,并且與服務端約定好協(xié)議 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)
}
}
}
//執(zhí)行原生事件
func refreshView(data:Int){
//handleData
...
self.tableView.reloadData()
}
//解析URL參數(shù)轉(zhuǎn)換成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內(nèi)容自適應(webview在tableView中的情況)
**方法1:通過kov監(jiān)聽webview的ajax加載完成的屬性,來重新獲取webview內(nèi)容高度
//TODO:理論是可實現(xiàn)的,后續(xù)添加
**方法2:通過js調(diào)用原生告知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數(shù)據(jù)
<3>將新的圖片數(shù)據(jù)寫入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();")
}