加载本地 Html 文件不引用 UIWebView 中的 js 文件
我正在使用 UIWebView
项目,我想从项目资源加载 HTML 文件.当我从 URL 运行时它工作正常,但是当我在本地查看 HTML 文件时,没有加载 JS 文件.加载本地HTML本地文件不是指UIWebView
中的js文件.
I am working with UIWebView
project and I want to load an HTML file from a project resource. It is working fine when I run from the URL, but when I view the HTML file locally, JS files are not loaded. Loading the local HTML local file doesn't refer to js files in UIWebView
.
这是我加载HTML文件项目本地资源并且不引用js文件的代码:
Here's my code to load the HTML file project local resource and does't refer the js file:
NSString *path = [[NSBundle mainBundle] pathForResource:@"textfile" ofType:@"txt"];
NSError *error = nil;
NSString *string = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
NSString *path1 = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path1];
NSLog(@"%@ >>> %@",baseURL,path);
[webview loadHTMLString:string baseURL:baseURL];
这段代码在 UIWebView
中没有找到 JS 文件,即使它成功地从项目资源中加载了图像文件.
This code doesn't find JS files in UIWebView
, even though it loads image files from the project resource successfully.
我发现问题是 js 文件不会自动复制到项目包中,所以您可以尝试按照以下说明进行操作:
i am find the problem is js files are not automatically copy to project bundles so, you can try follow instructions like below:
转到您的项目目标 -> 单击选项卡以构建阶段 -> 展开部分复制捆绑资源 -> 现在找到所有文件你项目的bin bundle资源检查你的js文件没有被复制,所以,点击+按钮并在你的项目中手动添加js文件!
go to your project target -> click tab to Bulid Phases -> expand the section copy Bundle Resources -> now find all files bin bundle resources of your project check your js files not copied,so, click + button and add js files manually on your project!
现在完美运行项目!
如下图所示:
在屏幕截图下方的项目中添加 js 文件后:
after add js file in your project below screen shot:
欢迎!