ios 学习札记 细节(三)
本节纪录学习WebView的心得
1.包含web的controller遵从UIWebViewDelegate协议。
2.定义 UIWebView 与 URL
@property (nonatomic, strong) UIWebView *mainWebView;
@property (nonatomic, strong) NSURL *URL;
3.发送Web页面请求
- (void)loadView
{
mainWebView = [[UIWebViewalloc] initWithFrame:[UIScreenmainScreen].bounds];
mainWebView.delegate = self;
mainWebView.scalesPageToFit = YES;
[mainWebViewloadRequest:[NSURLRequestrequestWithURL:self.URL]];
self.view = mainWebView;
}
4.实现代理部分
#pragma mark -
#pragma mark UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView
{
//打开菊花
[[UIApplicationsharedApplication] setNetworkActivityIndicatorVisible:YES];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//关闭菊花
[[UIApplicationsharedApplication] setNetworkActivityIndicatorVisible:NO];
self.navigationItem.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[[UIApplicationsharedApplication] setNetworkActivityIndicatorVisible:NO];
}
5.加载本地html页面
假设工程目录下有1.html ,222.js,test1.css,iWebView是一成员变量:
UIWebView *iWebView;
//加载本地使用说明文件文件
-(void)loadDocument:(NSString *)docName
{
NSString *mainBundleDirectory=[[NSBundle mainBundle] bundlePath];
NSString *path=[mainBundleDirectory stringByAppendingPathComponent:docName];
NSURL *url=[NSURL fileURLWithPath:path];
NSURLRequest *request=[NSURLRequestrequestWithURL:url];
iWebView.scalesPageToFit=YES;
[iWebViewloadRequest:request];
}
使用的时候,直接调用:
[self loadDocument];
7.联网时候的菊花加载还可以参照以下:
http://www.cnblogs.com/zhuqil/archive/2011/07/28/2119923.html