iOS中UIWebView的使用详解
2016-03-05 20:04:03.950 webWiew[52644:2355401] -[ViewController webView:shouldStartLoadWithRequest:navigationType:] -- <NSMutableURLRequest: 0x7f8d5ac36b50> { URL: https://www.baidu.com/ } -- 5
2016-03-05 20:04:03.952 webWiew[52644:2355401] -[ViewController webViewDidStartLoad:]
2016-03-05 20:04:40.682 webWiew[52644:2355401] -[ViewController webView:didFailLoadWithError:] -- Error Domain=NSURLErrorDomain Code=-999 "(null)" UserInfo={NSErrorFailingURLStringKey=https://www.baidu.com/, NSErrorFailingURLKey=https://www.baidu.com/}
2016-03-05 20:04:40.694 webWiew[52644:2355401] -[ViewController webView:shouldStartLoadWithRequest:navigationType:] -- <NSMutableURLRequest: 0x7f8d5af62990> { URL: https://www.baidu.com/ } -- 3
2016-03-05 20:04:40.694 webWiew[52644:2355401] -[ViewController webViewDidStartLoad:]
2016-03-05 20:05:56.301 webWiew[52644:2355401] -[ViewController webViewDidFinishLoad:]
这里是一个 小小例
1 #import "ViewController.h" 2 3 @interface ViewController ()<UIWebViewDelegate> 4 5 @property (nonatomic, weak) UIActivityIndicatorView *activeView; 6 7 8 @end 9 10 @implementation ViewController 11 12 - (void)viewDidLoad { 13 [super viewDidLoad]; 14 15 self.view.backgroundColor = [UIColor grayColor]; 16 17 UIWebView *webView = [[UIWebView alloc] initWithFrame:self.view.bounds]; 18 19 [self.view addSubview:webView]; 20 21 webView.delegate = self; 22 23 // webView.scrollView.backgroundColor = [UIColor redColor]; 24 // 25 // webView.backgroundColor = [UIColor orangeColor]; 26 27 // [webView setOpaque:NO];//opaque是不透明的意思 28 webView.dataDetectorTypes = UIDataDetectorTypeAll;//自动检测网页上的电话号码,单击可以拨打 29 [webView setScalesPageToFit:YES];//自动缩放以适应屏幕 30 31 //加载网页的方式 32 //1.创建并加载远程网页 33 NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"]; 34 [webView loadRequest:[NSURLRequest requestWithURL:url]]; 35 //2.加载本地文件资源 36 //NSURL *url = [NSURL fileURLWithPath:filePath]; 37 //NSURLRequest *request = [NSURLRequest requestWithURL:url]; 38 //[webView loadRequest:request]; 39 //3.读入一个HTML,直接写入一个HTML代码 40 //NSString *htmlPath = [[[NSBundle mainBundle]bundlePath]stringByAppendingString:@"webapp/loadar.html"]; 41 //NSString *htmlString = [NSString stringWithContentsOfURL:htmlPath encoding:NSUTF8StringEncoding error:NULL]; 42 //[webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:htmlPath]]; 43 44 UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; 45 // activityView.frame = CGRectMake(100, 100, 32, 32); 46 activityView.backgroundColor = [UIColor redColor]; 47 activityView.center = CGPointMake([UIScreen mainScreen].bounds.size.width * .5, [UIScreen mainScreen].bounds.size.height * .5); 48 49 50 self.activeView = activityView; 51 [self.view addSubview:activityView]; 52 53 54 } 55 56 #pragma mark - UIWebView delegate 57 58 // 开始加载的时候执行该方法。 59 - (void)webViewDidStartLoad:(UIWebView *)webView 60 { 61 NSLog(@"%s",__func__); 62 [self.activeView startAnimating] ; 63 } 64 65 // 加载完成的时候执行该方法。 66 - (void)webViewDidFinishLoad:(UIWebView *)webView 67 { 68 NSLog(@"%s",__func__); 69 [self.activeView stopAnimating]; 70 } 71 72 // 有错误会来到这里 73 - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error 74 { 75 NSLog(@"%s -- %@",__func__,error); 76 } 77 78 // 每次请求的url地址 都会调用这个函数。返回yes表示加载。否则不加载 79 - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{ 80 81 NSLog(@"%s -- %@ -- %ld",__func__,request,navigationType); 82 83 return YES; 84 } 85 86 @end