How to load a local html from native App in iphone
I have an html file 'sample.html' in a folder 'www' under project. How to load this 开发者_运维问答file from the native App..
I searched many sites ,the given samples are for the files from 'Resource'. Please help me in this...
Thanks, Bharath
The www
folder is not important if you're loading from the App bundle because the build process flat all your bundle files in the same directory.
In your UIViewController try to use something like this...
If you're loading from your App Bundle
UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"page.html" ofType:@""]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];
[webView release];
If you're loading form a www
folder in your Document directory (in example)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *yourFilePath = [NSString stringWithFormat:@"%@/www/page.html", documentDirectory];
NSURL *url = [NSURL fileURLWithPath:yourFilePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
[self.view addSubview:webView];
[webView release];
Hope this helps. Ciao!
You can refer to Phonegap source code:
PhoneGapDelegate.m
Especially this line and how it gets file from inside www folder:
NSURL *appURL = [NSURL fileURLWithPath:[PhoneGapDelegate pathForResource:@"index.html"]];
If your build is copying this file into the app bundle, then it gets copied into the mainBundle, no www directory.
精彩评论