在Objective-C中使用脱机HTML文件而不是URL



如何将此代码转换为使用离线(捆绑)文件而不是"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];}    

这里所说的脱机文件,我假定您指的是预先放入包中的文件。在这种情况下,一切都清楚地解释在这里:http://iphoneincubator.com/blog/data-management/how-to-read-a-file-from-your-application-bundle

幸运的你:你的问题已经解决了:

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"]];  
} 

如果您想在某个时候从Internet更新HTML文件,您可以在Documents中存储一个版本,如链接的注释所述。