UIWebView can't find local images - ios

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *baseURL = [NSURL fileURLWithPath:htmlFile];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile
encoding:NSUTF8StringEncoding
error:nil];
myWebView = [[UIWebView alloc] init];
[myWebView loadHTMLString:htmlString baseURL:baseURL];
My code is like this. In index.html, I have
<img src="dog.gif"/>
<h1>index.html loaded</h1>
Both the index.html and dog.gif are stored in the PROJECT_ROOT/Resources/.
But my app works OK on the simulator, but fails to load dog.gif on my iPad. The text is showed on both. I cleaned the project, but that didn't work.

Related

Load image/link in UIWebview

Attempting to load an html file into webview that has images that link to other pages in the supporting files folder. The main html file loads but the images and link do not work. Any suggestion on how to solve this problem? I'm referencing my img file as img src="$#%.png".
- (void)viewDidLoad {
[super viewDidLoad];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"iphone"
ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile
encoding:NSUTF8StringEncoding error:nil];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[myWebView loadHTMLString:htmlString baseURL:baseURL];
[myWebView loadHTMLString:htmlString baseURL:[[NSBundle mainBundle]
bundleURL]];
[myWebView loadHTMLString:htmlString baseURL:nil];
}
Use this code to load html file in UIWebView :
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"iphone" ofType:#"html"] isDirectory:NO]]];

CSS and JS Files are not loading in uiwebview

In my app, a website included in a folder. I tried to load it in webview using the following code:
self.wView.dataDetectorTypes = UIDataDetectorTypeLink;
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:NO];
NSLog(#"%#",htmlFile);
//NSData *pathData = [NSData dataWithContentsOfFile:htmlFile];
//NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSURL *url = [NSURL URLWithString:[htmlFile lastPathComponent] relativeToURL:[NSURL fileURLWithPath:[htmlFile stringByDeletingLastPathComponent] isDirectory:YES]];
//(void)[NSURLConnection connectionWithRequest:req delegate:self];
[self.wView loadRequest:[NSURLRequest requestWithURL:url]] ;
I get the file path in nslog.
But in my webview, css and js files are not loading.
Please help me.
Simple Solution for this problem
Just select the option Create Folder references for any added folders while you are copying the Website folder into XCode

UIWebView not loading local webpage

I have a super simple webpage that I just want to load a single local file. The file is /Resources/About.html
Here is my code:
NSString *urlPath = [[NSBundle mainBundle] URLForResource:#"About" withExtension:#"html"];
NSURL *url = [NSURL fileURLWithPath:urlPath];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
What's wrong?
The method URLForResource:withExtension returns an NSURL, not an NSString. You can use the result of that directly in your request.
NSURL *URL = [[NSBundle mainBundle] URLForResource:#"About" withExtension:#"html"];
[webView loadRequest:[NSURLRequest requestWithURL:URL]];
NSString * urlPath = [[NSBundle mainBundle] pathForResource:#"About" ofType:#"html"];
NSString *content = [NSString urlPath encoding:NSUTF8StringEncoding error:nil];
[webView content baseURL:[NSURL urlPath]];
I bet URLForResource is returning nil (Can you confirm that it is not?). It will not perform a recursive search, and will fail if your "Resources" folder is an actual folder (blue in XCode) as opposed to a group (yellow in Xcode). Try using the main bundle's resourceURL and appending your path to the end of it.

NSURLCache don't support file://?

I would like to load [NSURL fileURLWithPath:#"2.html"] in UIWebView use NSURLCache, the cachedResponseForRequest: function is executed and return a NSCachedURLResponse object which is performed correctly at http:// scheme.
But The UIWebView load failed finally. Did I miss some optional?
I want to use UIWebView load html file in a compressed file, and I do not expect to uncompress the files to disk. is there any suggestion.
Do this:
NSURL *url = [[NSBundle mainBundle] URLForResource:#"2" withExtension:#"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[webView loadHTMLString:html baseURL:baseUrl];

ipad: Running a local website and storing data

I'm trying to run a web form on an ipad that has no web connection. So far i've successfully been able to load the form into safari through the use of an app that takes a html file and displays it in a browser.
However, the data store sql component of html 5 just doesn't seem to work. I copied the code from html5rocks.com
http://playground.html5rocks.com/#async_transactions
Seems to work everywhere but in my application.
So my app is very simple, it just loads a web browser... here's the relevant code
- (void)viewDidLoad {
// Example 1, loading the content from a URLNSURL
//NSURL *url = [NSURL URLWithString:#"http://dblog.com.au"];
//NSURLRequest *request = [NSURLRequest requestWithURL:url];
//[webView loadRequest:request];
NSString *webPage = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSData *webData = [NSData dataWithContentsOfFile:webPage];
NSString *imagePath = [[NSBundle mainBundle] resourcePath];
imagePath = [imagePath stringByReplacingOccurrencesOfString:#"/" withString:#"//"];
imagePath = [imagePath stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
// NSString *HTMLData = #"
// <h1>Hello this is a test</h1>
// <img src="sample.jpg" alt="" width="100" height="100" />";
// [webView loadHTMLString:HTMLData baseURL:[NSURL URLWithString: [NSString stringWithFormat:#"file:/%#//",imagePath]]];
//[webView loadHTMLString:webPage baseURL:[NSURL URLWithString: [NSString stringWithFormat:#"file:/%#//",imagePath]]];
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:webPage isDirectory:NO]]];
NSString *baseStr = [NSString stringWithFormat:#"file:/%#//",imagePath];
NSURL *baseURL = [NSURL URLWithString: baseStr];
NSLog(#"%#",baseStr);
//webView.scalesPageToFit = YES;
//webView.multipleTouchEnabled = YES;
/*for (id subview in webView.subviews){
if([[subview class] isSubclassOfClass: [UIScrollView class]])
((UIScrollView *)subview).bounces = NO;
}*/
[webView loadData:webData MIMEType:#"text/html" textEncodingName:#"UTF-8" baseURL:baseURL];
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.cyberdesignworks.com.au/clients/ideatoaction"]]];
//[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Test" ofType:#"svg"]isDirectory:NO]]];
[super viewDidLoad];
The problem was that Xcode tries to compile my js files.
DAMN YOU APPLE!
anyway look on the right in the list of items in xcode when you have a project open
look for targets, it has a picture of a bow and arrow target, or like a red and white dart board. open that up, look at compiled resources. Move any js files from there to the copy bundled resources.
Bam! it should all work as intended now.

Resources