开发者

Use of an offline HTML file instead of a URL in Objective-C

开发者 https://www.devze.com 2023-03-31 03:42 出处:网络
How can I convert this code so it uses an offline (bundled) file in开发者_如何学JAVAstead of the \"http://5times9.com\"?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消