加速Uni-App加载,提高用户体验

2024-11-25 17:24 栏目: 公司新闻 查看()

Uni-App H5包直接加载到APP中,可以用来提高加载速度,一般有两种方法:

直接把包放进去 iOS 项目中加载 (主要用于开发阶段测试)

 从后台服务器预下载到文件沙箱 (可以减少APP包的大小,还可以实现热更新功能)


加载步骤

1、创建uni-app项目,并将其打包成H5

   使用官方工具HBuilder创建测试项目


53.png


创建uni-app



 修改 manifest.json 文件中的 H5配置运行的基本路径如下:./



 修改配置



发行为H5




 H5发行




第二,iOS项目直接加载包。 Uni-App

   整个Uni-App包以引用(蓝色文件夹)的形式添加到项目中。




引入Uni-App



 加载uni-app


- (void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingthe view fromitsnib.NSString*pathString=[[NSBundlemainBundle]pathForResource:@"index"ofType:@"html"inDirectory:@"h5"];NSURLRequest*request=[NSURLRequestrequestWithURL:[NSURLfileURLWithPath:pathString]];[self.webViewloadRequest:request];}

需要携带参数来加载uni-app。


- (void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingthe view fromitsnib.NSString*pathString=[[NSBundlemainBundle]pathForResource:@"index"ofType:@"html"inDirectory:@"h5"];NSString*GPath22=[[NSStringstringWithFormat:@"?id=%@",@"testId"]stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSetURLFragmentAllowedCharacterSet]];NSURL*baseUrl=[NSURLfileURLWithPath:pathString];NSURL*loadUrl=[NSURLURLWithString:ingpatringlatillinglatillingling:baseUrl];NSURLRequest*request=[NSURLRequestrequestWithURL:loadUrl];[self.webViewloadRequest:request];}

加载uni-app指定页面 view 例子页,和上面一样,


- (void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingthe view fromitsnib.NSString*pathString=[[NSBundlemainBundle]pathForResource:@"index"ofType:@"html"inDirectory:@"h5"];NSString*22patstring=pages/component/view/view";NSURL*baseUrl=[NSURLfileURLWithPath:pathString];NSURL*loadUrl=[NSURLURLWithString:ingpatringlatillinglatillingling:baseUrl];NSURLRequest*request=[NSURLRequestrequestWithURL:loadUrl];[self.webViewloadRequest:request];}

3、将Uni加载到沙箱中-App

 基本逻辑与加载本地逻辑一致,但必须使用 relativeToUrl 如何加载,首先确认您的缓存目录,然后拼接file路径:////协议头:

- (void)viewDidLoad{[superviewDidLoad];//Doanyadditionalsetupafterloadingthe view fromitsnib.NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);NSString*basePath=[[pathsobjectAtIndex:0]stringByAppendingString:@"h5"];NSString*path=[basePathstringByAppendingString:@"index.html"];NSURL*baseUrl=[NSURLfileURLWithPath:basePath];////需要手动拼接file://NSString*loadPath=[[NSStringstringWithFormat:@"file:///%@path]stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSetURLFragmentAllowedCharacterSet]];//LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL*loadUrl=[NSURLURLWithString:loadPathrelativeToURL:baseUrl];[self.webViewloadFileURL:loadUrlallowingReadAccessToURL:[NSURLfileURLWithPath:[pathsobjectAtIndex:0]]];}



 iOS 加载Uni-APP 效果


 Tips: 先用电脑打包H5,试试能否成功加载。 index.html ~

 假设是Origin null is not allowed by Access-Control-Allow-Origin.如因问题无法加载,可按以下设置允许WKWebView跨域:

 [self.webView.configuration.preferences setValue:@YES forKey:@"allowFileAcces





扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流

郑重申明:广西能威网络科技有限公司以外的任何单位或个人,不得使用该案例作为工作成功展示!