Load resources from relative path using local html in uiwebview
来源:互联网 发布:99宿舍软件 编辑:程序博客网 时间:2024/06/07 03:28
question
I have a very simple iOS app with a uiwebview loading a very simple test page (test.html):
<html><body><img src="img/myimage.png" /></body></html>
I load this test.html file into my web view:
NSURL *url = [[NSBundle mainBundle] URLForResource:@"test" withExtension:@"html"];NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];[webView loadHTMLString:html baseURL:baseUrl];
This works fine if I reference the image without the relative path and put the referenced image in the root path under Targets -> Copy Bundle Resources within XCode, however I can't get it to work with the relative path as shown in my html file. There must be a way to do this, I have lots of images, css, javascript files that I want to load into the webview and I would like not to have to have everything in the root and have to change all the references in my web app.
answer
This is how to load/use a local html with relative references.
- Drag the resource into your xcode project (I dragged a folder named www from my finder window), you will get two options "create groups for any added folders" and "create folders references for any added folders".
- Select the "create folder references.." option.
Use the below given code. It should work like a charm.
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:@"www"]];
[webview loadRequest:[NSURLRequest requestWithURL:url]];
Now all your relative links(like img/.gif, js/.js) in the html should get resolved.
- Load resources from relative path using local html in uiwebview
- iOS: UIWebview loadhtmlstring & Local css/js/image resources
- HTML相对路径(Relative Path)和绝对路径(Absolute Path)
- HTML相对路径(Relative Path)和绝对路径(Absolute Path)
- HTML相对路径(Relative Path)和绝对路径(Absolute Path)
- HTML相对路径(Relative Path)和绝对路径(Absolute Path)
- HTML相对路径(Relative Path)和绝对路径(Absolute Path)
- HTML相对路径(Relative Path)和绝对路径(Absolute Path)
- HTML相对路径(Relative Path)和绝对路径(Absolute Path)
- HTML相对路径(Relative Path)和绝对路径(Absolute Path)
- HTML相对路径(Relative Path)和绝对路径(Absolute Path)
- HTML相对路径(Relative Path)和绝对路径(Absolute Path)
- Embedding and Using Resources in C#
- Using local Logon Scripts in Windows XP
- Local File Manage in JavaScript Using FileSystemObject
- c# Runtime load library; Load Assembly from Specific Directory Path
- HTML相对路径(Relative Path)和绝…
- Path used in ActionScript-Url,Absolute Url,Relative Url
- 使用easyBCD 安装 Win8 + Ubuntu 13.10 双系统
- java-计算任意日期所在周、月、年的第一天与最后一天
- 2015.06.23
- Eclipse自动生成作者、日期注释等功能
- 20150623----Android-Settings源码分析
- Load resources from relative path using local html in uiwebview
- 17. 解决冲突
- Android.mk添加第三方jar包
- java学习之旅09--char_字符串入门_boolean
- 哪些企业会在云计算中活下来
- Linux进程间通信(IPC)
- CSS Bootstrap简介
- ios 企业发布ipa 和 plist
- GOF设计模式