- (void)createdWebView{
[self.certificateWebView setUserInteractionEnabled:YES];
//1.创建并加载远程网页
NSURL *url = [NSURL URLWithString:@"http://www.taobao.com"];
[self.certificateWebView loadRequest:[NSURLRequest requestWithURL:url
]];
2.加载本地文件资源
NSString *filePath = @"列如webapp.plist";
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_certificateWebView loadRequest:request];
3.读入一个HTML,直接写入一个HTML代码
拖动文件到Xcode,提示两个选择,“create groups”和“create folder references”,默认情况下为第一种,即所有加入到项目的文件都会在mainBundle根路径下,即不管加入项目的文件的目录结构如何,在APP中都可以通过mainBundlePath/filename来访问到;如果采用第二种方式,则就会保留相对路径,需要通过mainBundlePath/path/filename来访问。通过这两种方式到项目的文件夹显示具有不同的颜色。 选择“create groups”,文件夹颜色为黄色。
(1)选择“create groups”,html里有一个路径的问题( link href= “”),在Xcode中, Xcode不能找到像url(”../images/photo.png”)这种路径,所以应该在Xcode中调整下html文件访问本页面图片和css样式文件的路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"touBio-add" ofType:@"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSString *basePath = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:basePath];
[self.certificateWebView loadHTMLString:htmlString baseURL:baseURL];
(2)选择“create folder references”。蓝色的文件夹。文件夹名字为urlName,里面包含一个名为index的html文件
NSURL *htmlPath = [NSURL URLWithString:[[NSBundle mainBundle] pathForResource:@"touBio-add" ofType:@"html" inDirectory:@"appPhone"]];
NSURLRequest *request = [NSURLRequest requestWithURL:htmlPath];
[self.certificateWebView loadRequest:request];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[webView stringByEvaluatingJavaScriptFromString:@"rewrite();"];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSURL *url = [NSURL URLWithString:@"www.taobao.com"];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if ((([httpResponse statusCode] / 100) == 2)) {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[_certificateWebView loadRequest:[NSURLRequest requestWithURL:url]];
} else {
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:NSLocalizedString(@"HTTP Error", @"Error message displayed when receving a connection error.") forKey:NSLocalizedDescriptionKey];
NSError *error = [NSError errorWithDomain:@"HTTP" code:[httpResponse statusCode] userInfo:userInfo];
if ([error code] == 404) {
NSLog(@"断网");
_certificateWebView.hidden = YES;
}
}
}
iOS - WebView
最新推荐文章于 2020-06-08 15:15:23 发布