碎碎念
最近写包,偷懒
想用h5写好,webview加载
然后使用的的事vue3+vite
但是结果打包一直白屏
真是给我整麻了
先说vite那边要做的事,请访问无星的前端之旅(三十)——Vite打包本地预览html
本地wkwebview加载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| import UIKit import WebKit
class ViewController: UIViewController { var webView: WKWebView! override func viewDidLoad() { super.viewDidLoad() webView = initWebView() loadNativeHTML() self.view.addSubview(webView) NSLayoutConstraint.activate([ webView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor), webView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor), webView.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor), webView.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor) ]) ignoreSafeContent() } override func viewDidLayoutSubviews() { super.viewDidLayoutSubviews() } func ignoreSafeContent() { self.additionalSafeAreaInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0) self.view.insetsLayoutMarginsFromSafeArea = false } func initWebView() -> WKWebView { let wkconfig = initWkConfig() let webView = WKWebView(frame: .zero,configuration: wkconfig) webView.translatesAutoresizingMaskIntoConstraints = false if #available(iOS 16.4,*) { webView.isInspectable = true } else { } return webView } func initWkConfig() -> WKWebViewConfiguration{ let configuration = WKWebViewConfiguration() let preferences = WKPreferences() preferences.setValue(true, forKey: "allowFileAccessFromFileURLs") configuration.preferences = preferences return configuration } func loadNativeHTML() { if let path = Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "dist") { let fileURL = URL(fileURLWithPath: path) webView.loadFileURL(fileURL, allowingReadAccessTo: fileURL.deletingLastPathComponent()) } } }
|
注意文件的放入方式!
要在此处是蓝色文件夹
这样在拆包的时候才会是有文件夹进去
可以通过这个方式查看加载的文件路径无星的前端之旅(十六)——移动端调试技巧/
1
| file:///Users/xingwu/Library/Developer/CoreSimulator/Devices/790786D7-9863-4798-8DA8-05D9DAF830A7/data/Containers/Bundle/Application/5D2108CE-F89B-45A6-AB9F-DA3D87F217A6/ComputedMini.app/dist/assets/index-BL26KLFi.js
|
其中file:///Users/xingwu/Library/Developer/CoreSimulator/Devices/790786D7-9863-4798-8DA8-05D9DAF830A7/data/Containers/Bundle/Application/5D2108CE-F89B-45A6-AB9F-DA3D87F217A6/ComputedMini.app
是模拟器中App的路径,右键查看包内容就能看到层级了
如果是非蓝色
文件夹
内容引入,所有文件都会在根目录下,所以访问不到白屏!