<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">把html文件拖至项目里,我起名为myhtml(可以放根目录,也可以放自己建的group里)</span>
加入如下代码
let file=NSFileManager.defaultManager().contentsAtPath(<span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">NSBundle</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">.</span><span style="color: rgb(61, 29, 129); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">mainBundle</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">().</span><span style="color: rgb(61, 29, 129); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">pathForResource</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">(</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);">"myhtml"</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">, ofType: </span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);">"html"</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;">))</span>
webView.loadData(file!, MIMEType: "text/html", textEncodingName: "UTF-8",baseURL: NSURL(string: "local")!)
本地图片
拖到项目里,位置也是随便
在delegate的webViewDidFinishLoad方法加入
webView.stringByEvaluatingJavaScriptFromString("document.getElementById('pic').src='file://\(<span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures; color: rgb(112, 61, 170);">NSBundle</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;">.</span><span style="color: rgb(61, 29, 129); font-family: Menlo; font-variant-ligatures: no-common-ligatures;">mainBundle</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;">().</span><span style="color: rgb(61, 29, 129); font-family: Menlo; font-variant-ligatures: no-common-ligatures;">pathForResource</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;">(</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);">"error"</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;">, ofType: </span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures; color: rgb(209, 47, 27);">"jpg"</span><span style="font-family: Menlo; font-variant-ligatures: no-common-ligatures;">)!</span><span style="color: rgb(61, 29, 129); font-family: Menlo;">)'")</span>