UIWebView是iOS sdk中一个最常用的控件。是内置的浏览器控件,我们可以用它来浏览网页、打开文档等等,UIWebView能够加载html/htm、pdf、docx、txt等格式的文件
UIWebView:->UIView同时遵守了滚动的协议
loadRequest:加载请求
NSURLRequest:请求类用于设置网络请求相关信息如:网址、传的内容、请求头...
NSURL:路径每个内容都有一个在万维网中唯一的路径
挂代理:<UIWebViewDelegate>
1.在viewDidLoad里面
self.edgesForExtendedLayout=UIRectEdgeNone;
myWebView= [[UIWebViewalloc]initWithFrame:[UIScreenmainScreen].bounds];
myWebView.delegate=self;
myWebView.scalesPageToFit=YES;
myWebView.allowsInlineMediaPlayback=YES;
[self.viewaddSubview:myWebView];
self.navigationItem.leftBarButtonItem= [[UIBarButtonItemalloc]initWithTitle:@"返回"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(gotoBack)];
self.navigationItem.rightBarButtonItem= [[UIBarButtonItemalloc]initWithTitle:@"下一页"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(next)];
indicatorView= [[UIActivityIndicatorViewalloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
indicatorView.center=self.view.center;
[self.viewaddSubview:indicatorView];
NSString*path = [[NSBundlemainBundle]pathForResource:@"123"ofType:@"pdf"];
[selfloadWithURL:[NSURLURLWithString:@"http://www.chinacaipu.com/menu/dianxinshipin/118481.html"]];
- (void)loadWithURL:(NSURL*)url{
NSURLRequest*request = [NSURLRequestrequestWithURL:url];
[myWebViewloadRequest:request]; }
- (void)gotoBack{
[myWebViewgoBack]; }
- (void)next{
[myWebViewgoForward]; }
开始加载请求
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType{
[indicatorViewstartAnimating]; returnYES; }
加载完成
- (void)webViewDidFinishLoad:(UIWebView*)webView{
[indicatorViewstopAnimating]; }
加载失败
- (void)webView:(UIWebView*)webView didFailLoadWithError:(nullableNSError*)error{
if([errorisEqual:[NSNullnull]] !=YES) {
UIAlertAction*reloadAction = [UIAlertActionactionWithTitle:@"重新加载"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction*_Nonnullaction) {
[selfloadWithURL:[NSURLURLWithString:@"https://www.baidu.com"]]; }];
UIAlertAction*cancelAction = [UIAlertActionactionWithTitle:@"取消"style:UIAlertActionStyleCancelhandler:^(UIAlertAction*_Nonnullaction) { }];
NSString*errorMessage =@"";
if(error.code== -1009) {
errorMessage =@"网络连接错误"; }
UIAlertController*alert = [UIAlertControlleralertControllerWithTitle:@"加载错误"message:errorMessagepreferredStyle:UIAlertControllerStyleAlert];
[alertaddAction:reloadAction];
[alertaddAction:cancelAction];
[selfpresentViewController:alertanimated:YEScompletion:nil];
NSLog(@"%@",error.userInfo); }}
前往下一页:goForward
self.navigationItem.rightBarButtonItem= [[UIBarButtonItemalloc]initWithTitle:@"下一页"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(next)];
返回上一页:goBack
self.navigationItem.leftBarButtonItem= [[UIBarButtonItemalloc]initWithTitle:@"返回"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(gotoBack)];
scalesPageToFit让加载的内容自适应设备
webView可以通过代理方法检测加载内容的状态
// 是否允许播放内链视频 在手机上默认是NO
myWebView.allowsInlineMediaPlayback=YES;
代理方法
网页开始加载的时候调用
-(void)webViewDidStartLoad:(UIWebView *)webView
网页加载完成的时候调用
-(void)webViewDidFinishLoad:(UIWebView *)webView
网页加载出错的时候调用
-(void)webView:(UIWebView )webView didFailLoadWithError:(NSError )error
网页中的每一个请求都会被触发这个方法,返回NO代表不执行这个请求(常用于JS与iOS之间通讯)
-(BOOL)webView:(UIWebView )webView shouldStartLoadWithRequest:(NSURLRequest )request navigationType:(UIWebViewNavigationType)navigationType
网友评论