How can I convert this code so it uses an offline (bundled) file in开发者_如何学JAVAstead of the "http://5times9.com"
?
- (void)loadAddWeb {
UIWebView *addWeb = [[UIWebView alloc] init];
NSURL *webURL = [NSURL URLWithString:@"http://5times9.com"];
NSURLRequest *webURLRequest = [NSURLRequest requestWithURL:webURL];
[addWeb loadRequest:webURLRequest];
[addWeb setScalesPageToFit:NO];
[addWeb setFrame:CGRectMake(0, 0, 320, 416)];
[self addSubview:addWeb];}
By an offline file, I assume you mean a file you will put in the bundle aforehand. In which case everything is clearly explained here: http://iphoneincubator.com/blog/data-management/how-to-read-a-file-from-your-application-bundle
Lucky you: your problem is solved in this as:
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"offline5times9.com" ofType:@"htm"];
NSData *htmlData = [NSData dataWithContentsOfFile:filePath];
if (htmlData) {
[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"http://kheldar.kikoo"]];
}
If you want to update the HTML file from Internet at some point, you can store a version in Documents, as stated in the comments of the link.
精彩评论