WebView的使用

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一. 创建: 二. 加载方式: 常用的网页加载方式 加载html格式的内容(html文件中加载和html格式字...
    fjytqiu阅读 4,909评论 0 11
  • 在上一篇中我们说了WebView的基本使用安卓开发之WebView的使用(1),里面提到了WebViewClien...
    Reathin阅读 2,757评论 0 14
  • WebView简介 String getUrl():获取当前页面的URL。 reload():重新reload当前...
    QM阅读 3,208评论 0 52
  • WebView 目前android市场上的一些应用采用的开发方式大致分为三种: Hybrid App中实现的主要技...
    Reathin阅读 2,095评论 1 9
  • 小时候憧憬自己长大,做自己喜欢做的事,长大了,总想着生活是自己的,想活出自己的样子,所以每天幻想着如何过好这一天。...
    艾梦小妹阅读 266评论 0 0