<?xml version="1.0" encoding="UTF-8"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><title>简书首页</title><link>https://www.jianshu.com</link><atom:link href="http://rsshub0.oxyry.com:21200/jianshu/home" rel="self" type="application/rss+xml"></atom:link><description>简书是一个优质的创作社区，在这里，你可以任性地创作，一篇短文、一张照片、一首诗、一幅画……我们相信，每个人都是生活中的艺术家，有着无穷的创造力。 - Powered by RSSHub</description><generator>RSSHub</generator><webMaster>contact@rsshub.app (RSSHub)</webMaster><language>en</language><lastBuildDate>Mon, 16 Nov 2026 05:20:53 GMT</lastBuildDate><ttl>5</ttl><item><title>WKWebView 加载 Uni-App 导出的本地 H5</title><description>&lt;p&gt;Uni-App 打包发布的H5包直接放到APP中加载可以用于提升加载速度，一般有两种方式：&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;直接把包放到 iOS 项目中加载 (多用于开发阶段测试)&lt;/li&gt;
&lt;li&gt;从后台服务器预下载到文件沙盒内加载 (可以减少APP包大小，同时也能实现热更新功能)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;加载步骤&lt;/h2&gt;
&lt;h4&gt;一、创建一个uni-app项目，并打包成H5&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;在官方工具HBuilder中创建一个测试项目&lt;/p&gt;
&lt;br&gt;
&lt;div class=&quot;image-package&quot;&gt;
&lt;div class=&quot;image-container&quot;&gt;
&lt;div class=&quot;image-container-fill&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;image-view&quot; data-width=&quot;2428&quot; data-height=&quot;1392&quot;&gt;&lt;img data-original-src=&quot;//upload-images.jianshu.io/upload_images/2955252-32c08ab4590c649c.png&quot; data-original-width=&quot;2428&quot; data-original-height=&quot;1392&quot; data-original-format=&quot;image/png&quot; data-original-filesize=&quot;468063&quot; src=&quot;https://upload-images.jianshu.io/upload_images/2955252-32c08ab4590c649c.png&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;image-caption&quot;&gt;创建uni-app&lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;修改 manifest.json 文件中的 h5配置的运行的基础路径为：&lt;code&gt;./&lt;/code&gt;&lt;br&gt;
&lt;div class=&quot;image-package&quot;&gt;
&lt;div class=&quot;image-container&quot;&gt;
&lt;div class=&quot;image-container-fill&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;image-view&quot; data-width=&quot;2646&quot; data-height=&quot;2152&quot;&gt;&lt;img data-original-src=&quot;//upload-images.jianshu.io/upload_images/2955252-28f6c045294e44d2.png&quot; data-original-width=&quot;2646&quot; data-original-height=&quot;2152&quot; data-original-format=&quot;image/png&quot; data-original-filesize=&quot;968497&quot; src=&quot;https://upload-images.jianshu.io/upload_images/2955252-28f6c045294e44d2.png&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;image-caption&quot;&gt;修改配置&lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;发行为H5&lt;/p&gt;
&lt;br&gt;
&lt;div class=&quot;image-package&quot;&gt;
&lt;div class=&quot;image-container&quot;&gt;
&lt;div class=&quot;image-container-fill&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;image-view&quot; data-width=&quot;1292&quot; data-height=&quot;782&quot;&gt;&lt;img data-original-src=&quot;//upload-images.jianshu.io/upload_images/2955252-d21b0f324a951e89.png&quot; data-original-width=&quot;1292&quot; data-original-height=&quot;782&quot; data-original-format=&quot;image/png&quot; data-original-filesize=&quot;170917&quot; src=&quot;https://upload-images.jianshu.io/upload_images/2955252-d21b0f324a951e89.png&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;image-caption&quot;&gt;H5发行&lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;二、iOS项目内直接加载包内 Uni-App&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;将整个Uni-App包以引用（蓝色文件夹）的方式加入项目&lt;/p&gt;
&lt;br&gt;
&lt;div class=&quot;image-package&quot;&gt;
&lt;div class=&quot;image-container&quot;&gt;
&lt;div class=&quot;image-container-fill&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;image-view&quot; data-width=&quot;1686&quot; data-height=&quot;1112&quot;&gt;&lt;img data-original-src=&quot;//upload-images.jianshu.io/upload_images/2955252-d9ee348c6b13ad70.png&quot; data-original-width=&quot;1686&quot; data-original-height=&quot;1112&quot; data-original-format=&quot;image/png&quot; data-original-filesize=&quot;359266&quot; src=&quot;https://upload-images.jianshu.io/upload_images/2955252-d9ee348c6b13ad70.png&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;image-caption&quot;&gt;引入Uni-App&lt;/div&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;加载 uni-app&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;Objective-C&quot;&gt;- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    NSString *pathString = [[NSBundle mainBundle] pathForResource:@&quot;index&quot; ofType:@&quot;html&quot; inDirectory:@&quot;h5&quot;];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:pathString]];
    [self.webView loadRequest:request];
}
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;加载uni-app需要携带参数&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;objc&quot;&gt;- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    NSString *pathString = [[NSBundle mainBundle] pathForResource:@&quot;index&quot; ofType:@&quot;html&quot; inDirectory:@&quot;h5&quot;];
    NSString *pathString2 = [[NSString stringWithFormat:@&quot;?id=%@&quot;,@&quot;testId&quot;] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    
    NSURL *baseUrl = [NSURL fileURLWithPath:pathString];
    NSURL *loadUrl = [NSURL URLWithString:pathString2 relativeToURL:baseUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:loadUrl];
    
    [self.webView loadRequest:request];
}
&lt;/code&gt;&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;加载uni-app指定页面例如 view 示例页面，和上面一样&lt;/li&gt;
&lt;/ul&gt;
&lt;pre&gt;&lt;code class=&quot;objc&quot;&gt;- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    NSString *pathString = [[NSBundle mainBundle] pathForResource:@&quot;index&quot; ofType:@&quot;html&quot; inDirectory:@&quot;h5&quot;];
    NSString *pathString2 = @&quot;#/pages/component/view/view&quot;;

    NSURL *baseUrl = [NSURL fileURLWithPath:pathString];
    NSURL *loadUrl = [NSURL URLWithString:pathString2 relativeToURL:baseUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:loadUrl];

    [self.webView loadRequest:request];
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h4&gt;三、加载沙盒内Uni-App&lt;/h4&gt;
&lt;p&gt;基本逻辑和加载本地一致，但是必须使用 &lt;code&gt;relativeToUrl&lt;/code&gt; 方法加载，首先确认自己缓存的目录，然后给路径拼接&lt;code&gt;file://&lt;/code&gt;协议头:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;objc&quot;&gt;- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = [[paths objectAtIndex:0] stringByAppendingString:@&quot;h5&quot;];
    
    NSString *path = [basePath stringByAppendingString:@&quot;index.html&quot;];
    
    NSURL *baseUrl = [NSURL fileURLWithPath:basePath];
    
    // 需要手动拼接 file://
    NSString *loadPath = [[NSString stringWithFormat:@&quot;file://%@&quot;,path] stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    // 最终加载路径
    NSURL *loadUrl = [NSURL URLWithString:loadPath relativeToURL:baseUrl];
    
    [self.webView loadFileURL:loadUrl allowingReadAccessToURL:[NSURL fileURLWithPath: [paths objectAtIndex:0]]];
}
&lt;/code&gt;&lt;/pre&gt;
&lt;div class=&quot;image-package&quot;&gt;
&lt;div class=&quot;image-container&quot;&gt;
&lt;div class=&quot;image-container-fill&quot;&gt;&lt;/div&gt;
&lt;div class=&quot;image-view&quot; data-width=&quot;1170&quot; data-height=&quot;2532&quot;&gt;&lt;img data-original-src=&quot;//upload-images.jianshu.io/upload_images/2955252-80d1d800246bb86f.png&quot; data-original-width=&quot;1170&quot; data-original-height=&quot;2532&quot; data-original-format=&quot;image/png&quot; data-original-filesize=&quot;170937&quot; src=&quot;https://upload-images.jianshu.io/upload_images/2955252-80d1d800246bb86f.png&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div class=&quot;image-caption&quot;&gt;iOS 加载Uni-APP 效果&lt;/div&gt;
&lt;/div&gt;
&lt;h4&gt;Tips: 打包出来的H5先用电脑试下能否成功加载 index.html ~&lt;/h4&gt;
&lt;p&gt;如果是&lt;code&gt;Origin null is not allowed by Access-Control-Allow-Origin.&lt;/code&gt;问题导致无法加载可以按如下设置，使WKWebView允许跨域：&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;objc&quot;&gt;[self.webView.configuration.preferences setValue:@YES forKey:@&quot;allowFileAccessFromFileURLs&quot;];
&lt;/code&gt;&lt;/pre&gt;
</description><link>https://www.jianshu.com/p/42a64c524f81</link><guid isPermaLink="false">https://www.jianshu.com/p/42a64c524f81</guid><pubDate>Mon, 08 Aug 2022 08:55:15 GMT</pubDate><dc:creator>韦弦Zhy</dc:creator></item></channel></rss>