iOS 使用NJKWebViewProgress做webview进度条

来源:互联网 发布:淘宝卖家退款多久到账 编辑:程序博客网 时间:2024/06/11 03:27
[objc] view plain copy
  1. //  
  2. //  UIWebviewTestVC.m  
  3. //  UIWebViewDemo  
  4. //  
  5. //  Created by Weblogic on 15/11/22.  
  6. //  Copyright © 2015年 Weblogic. All rights reserved.  
  7. //  
  8.   
  9. #import "UIWebviewTestVC.h"  
  10. #import "NJKWebViewProgressView.h"  
  11. #import "NJKWebViewProgress.h"  
  12.   
  13. @interface UIWebviewTestVC ()<UIWebViewDelegate,NJKWebViewProgressDelegate>  
  14. {  
  15.     UIWebView *_webView;  
  16.     NJKWebViewProgressView *_webViewProgressView;  
  17.     NJKWebViewProgress *_webViewProgress;  
  18. }  
  19.   
  20. @end  
  21.   
  22. @implementation UIWebviewTestVC  
  23.   
  24. - (void)viewDidLoad  
  25. {  
  26.     [super viewDidLoad];  
  27.       
  28.     _webView = [[UIWebView alloc] initWithFrame:self.view.frame];  
  29.     [self.view addSubview: _webView];  
  30.       
  31.     _webViewProgress = [[NJKWebViewProgress alloc] init];  
  32.     _webView.delegate = _webViewProgress;  
  33.     _webViewProgress.webViewProxyDelegate = self;  
  34.     _webViewProgress.progressDelegate = self;  
  35.       
  36.     CGRect navBounds = self.navigationController.navigationBar.bounds;  
  37.     CGRect barFrame = CGRectMake(0,  
  38.                                  navBounds.size.height - 2,  
  39.                                  navBounds.size.width,  
  40.                                  2);  
  41.     _webViewProgressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];  
  42.     _webViewProgressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;  
  43.     [_webViewProgressView setProgress:0 animated:YES];  
  44.       
  45.     NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.hao123.com"]];  
  46.     [_webView loadRequest:request];  
  47. }  
  48.   
  49. -(void)viewWillAppear:(BOOL)animated  
  50. {  
  51.     [self.navigationController.navigationBar addSubview:_webViewProgressView];  
  52. }  
  53.   
  54. -(void)viewWillDisappear:(BOOL)animated  
  55. {  
  56.     [_webViewProgressView removeFromSuperview];  
  57. }  
  58.   
  59. - (void)didReceiveMemoryWarning  
  60. {  
  61.     [super didReceiveMemoryWarning];  
  62. }  
  63.   
  64. -(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress  
  65. {  
  66.     [_webViewProgressView setProgress:progress animated:YES];  
  67.     self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];  
  68. }  
  69.   
  70. - (void) webViewDidStartLoad:(UIWebView *)webView  
  71. {  
  72.     NSLog(@"webViewDidStartLoad");  
  73. }  
  74. - (void) webViewDidFinishLoad:(UIWebView *)webView  
  75. {  
  76.     NSLog(@"webViewDidFinishLoad");  
  77. }  
  78. - (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error  
  79. {  
  80.     NSLog(@"didFailLoadWithError:%@", error);  
  81. }  
  82.   
  83. @end  



以下帖子转自:http://my.oschina.NET/u/936286/blog/511611?p={{page}}

升级Xcode 7.0发现网络访问失败。
输出错误信息

?
1
The resource could not be loaded because the App Transport Security policy requires the use of a secure connection.


Google后查证,iOS9引入了新特性App Transport Security (ATS)。详情:App Transport Security (ATS)

新特性要求App内访问的网络必须使用HTTPS协议。
但是现在公司的项目使用的是HTTP协议,使用私有加密方式保证数据安全。现在也不能马上改成HTTPS协议传输。

最终找到以下解决办法:

  1. 在Info.plist中添加NSAppTransportSecurity类型Dictionary

  2. NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES

0 0