前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UIWebView的使用

UIWebView的使用

原创
作者头像
大师级码师
修改2021-11-01 09:26:46
4410
修改2021-11-01 09:26:46
举报
文章被收录于专栏:大师级码师
代码语言:javascript
复制
@interface ViewController ()<UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UIBarButtonItem backItem;
@property (weak, nonatomic) IBOutlet UIBarButtonItem forwardItem;
@property(nonatomic,weak)UIWebView * webView;

(IBAction)back;
(IBAction)forward;

@end
@implementation ViewController

(void)viewDidLoad {
  [super viewDidLoad];


//1.创建webView
UIWebView * webView = [[UIWebView alloc]init];
webView.frame = self.view.bounds;
self.webView = webView;
[self.view addSubview:webView];
//设置代理
webView.delegate = self;


//2.加载请求
//加载的三种方式,加载三种数据

//《1》加载url请求

//        NSURL  url = [[NSBundle mainBundle]URLForResource:@"login" withExtension:@"html"];
//        NSURLRequest  request = [NSURLRequest requestWithURL:url];
//        [webView loadRequest:request];

//《2》加载html(用于显示传递过来的内容,因为loadHTMLString传递的是什么,就显示什么)(这种方式加载的网页,不能回退)

//    NSString  path = [[NSBundle mainBundle]pathForResource:@"login.html" ofType:nil];
//    NSString  string = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
//    NSLog(@"----%@", string);
//    [webView loadHTMLString:string baseURL:nil];

//《3》加载data数据(可以加载图片,word,pdf等)(这个方法加载的也不能回退)

//获取路径
NSString * path = [[NSBundle mainBundle]pathForResource:@"hell" ofType:@"pdf"];
//获取mimetype
NSURL * url = [NSURL fileURLWithPath:path];//不要使用urlWithString方法
NSURLRequest * request = [NSURLRequest requestWithURL:url];
NSURLResponse  * rest = nil;
[NSURLConnection  sendSynchronousRequest:request returningResponse:&rest error:nil];//因为需要使用这个返回值,所以使用同步方法
//加载
NSData * data = [NSData dataWithContentsOfFile:path];
[webView loadData:data MIMEType:rest.MIMEType textEncodingName:@"utf8"baseURL:nil];
NSLog(@"MIMEType-----%@",rest.MIMEType);
NSLog(@"%ld",data.length);




}

(IBAction)back{
  [self.webView goBack];//不使用dismiss方法,因为跳转的不是控制器

}

(IBAction)forward {
  [self.webView goForward];//前进
}

pragma mark - UIWebViewDelegate(四个代理方法)

(void)webViewDidStartLoad:(UIWebView *)webView{
//    NSLog(@"webViewDidStartLoad");

}

(void)webViewDidFinishLoad:(UIWebView *)webView{
  //    if ([webView canGoBack]) {//先判断这个页面有没有返回功能
  //        self.backItem.enabled = YES;
  //    }else{
  //    self.backItem.enabled = NO;
  //    }
//简便写法
  NSLog(@"########%i", [webView canGoBack]);
  self.backItem.enabled = [webView canGoBack];
  self.forwardItem.enabled = [webView canGoForward];


}

(void)webView:(UIWebView )webView didFailLoadWithError:(NSError )error{
//    NSLog(@"didFailLoadWithError");
}


(BOOL)webView:(UIWebView )webView shouldStartLoadWithRequest:(NSURLRequest )request navigationType:(UIWebViewNavigationType)navigationType{
  //URL
  //"http://www.baidu.com/&quot;
  //协议头://主机名//路径
//过滤掉还有baidu的url
//    NSString * path = request.URL.absoluteString;//注意不是URL.path这个方法,这个方法返回的是“/”
//    NSInteger location = [path rangeOfString:@"baidu"].location;
//    if (location != NSNotFound) {
//        return NO;
//    }
NSLog(@"%@", request.URL.absoluteString);
  return YES;


}
@end
</pre> 

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档