从以zip下载的捆绑包中加载UIWebview中的.js文件

Loading .js file in UIWebview from bundle downloaded as a zip

本文关键字:UIWebview 加载 中的 js 文件 包中 zip 下载 从以      更新时间:2023-09-26

我想在UIWebView中显示一个"完整的网站"(打包在捆绑包中)。

当我把bundle放在xcode中并将其添加到"compile stuff"中时,一切都被完美地加载了,js文件运行良好。

不幸的是,为了允许更新这个网站,我需要从另一个服务器下载这个作为zip。。。我可以很容易地获得zip,提取它,并将所有内容放在一个捆绑包中,但当我在UIWebView中加载索引页时,js文件不起作用!

我如何设置任何内容以使此代码可执行或类似的内容?

这是加载我创建的捆绑包的部分:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *test = [[NSBundle bundleWithPath:documentsDirectory] pathForResource:@"contentNewVersion.bundle/index" ofType:@"html"];
[_webView  loadRequest:[NSURLRequest  requestWithURL:[NSURL fileURLWithPath:test isDirectory:NO]]];

我的索引位置是:/var/mobile/Applications/A2B602C3-DEC0-40FE-9469-288F666196A7/contentNewVersion.bundle/index.html`

//试试这个

 NSString *test = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"/contentNewVersion.bundle/index.html"];
NSLog(@"%@",test);
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:test]]];

//下面的代码块是从NSDocumentDirectory 在UIWebview中加载js文件的过程

 NSString *JSFilePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"myfile.js"];
NSString *str_JSFilePath = [NSString stringWithFormat:@"<script src='%@'></script>", JSFilePath];
    [webview loadHTMLString:str_JSFilePath baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];