本文介绍了如何在Xcode webview中加载本地HTML文件而不是网页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

喂!
如何在此代码中加载保存到项目而不是网页的本地html文件:

Hey!How can I load a local html file saved to the project instead of a webpage in this code:

- (void)loadAboutHTML {
UIWebView *aboutHTML = [[UIWebView alloc] init];
NSURL *webURL = [NSURL URLWithString:@"http://apple.com"];
NSURLRequest *webURLRequest = [NSURLRequest requestWithURL:webURL];
[aboutHTML loadRequest:webURLRequest];
[aboutHTML setScalesPageToFit:YES];
[aboutHTML setFrame:CGRectMake(0, 0, 320, 416)];
[self addSubview:aboutHTML];}


推荐答案

选项1

使用此

- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL

阅读项目中文件的内容为 NSString 。然后使用上面的方法加载html内容

Read the contents of the file in your project into an NSString. Then use the above method to load the html content

使用

+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error

从文件中获取字符串然后使用

to obtain the string from the file and then use

[webView loadHTMLString:urHTMLString baseURL:baseURL];

选项2

NSURLRequest *urlReq = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"html"]]];
[webView loadRequest:urlReq];

更新

- (void)loadAboutHTML {
UIWebView *aboutHTML = [[UIWebView alloc] init];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"yourFileName" ofType:@"html"]]];
[aboutHTML loadRequest:urlRequest;
[self addSubview:aboutHTML];
}

这篇关于如何在Xcode webview中加载本地HTML文件而不是网页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 14:44