嘿! 如何加载保存到项目中的本地html文件而不是此代码中的网页:
- (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];}
Virat Naitha.. 13
UIWebView *web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; [web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"help" ofType:@"html"]isDirectory:NO]]]; web.backgroundColor = [UIColor clearColor];
那是我用过的
UIWebView *web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)]; [web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"help" ofType:@"html"]isDirectory:NO]]]; web.backgroundColor = [UIColor clearColor];
那是我用过的
选项1
用这个
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
将项目中文件的内容读入NSString
.然后使用上面的方法加载html内容
使用
+ (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error
从文件中获取字符串然后使用
[webView loadHTMLString:urHTMLString baseURL:baseURL];
方案2
NSURLRequest *urlReq = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"html"]]]; [webView loadRequest:urlReq];
UPDATE
- (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]; }