swift使用webview載入本地html,html裡引入本地的css,js
首先把靜態頁面寫好,建議不要在xcode裡寫,著實難用,可以用sublime/vscode之類的編輯器來寫html,css,js,寫好之後,開啟xcode,把靜態頁面資料夾直接拖動到專案裡,彈出視窗上選擇 create group
,如下圖
我這的目錄結構是這樣的
html引入css,js寫法如下
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0,user-scalable=no" /> <link rel="stylesheet" href="app.css"> <script src="underscore.min.js"></script> </head> <body> </body>
下面是swift裡通過程式碼載入html
// 將頁面內容轉成string let TOPICDETAILHTML = try! String(contentsOfFile: Bundle.main.path(forResource: "topic_detail", ofType: "html")!, encoding: String.Encoding.utf8) // 通過webview載入 webView.loadHTMLString(TOPICDETAILHTML, baseURL: Bundle.main.resourceURL)
然後執行專案,頁面就載入進來了,然後就可以通過swift與webview之間互動來開發了,詳見部落格 ofollow,noindex" target="_blank">https://tomoya92.github.io/2018/07/05/swift-webview-javascript/
原文連結: