iOS 使用NJKWebViewProgress做webview进度条

祁坚壁
2023-12-01


下载地址:https://github.com/ninjinkun/NJKWebViewProgress


//
//  UIWebviewTestVC.m
//  UIWebViewDemo
//
//  Created by Weblogic on 15/11/22.
//  Copyright © 2015年 Weblogic. All rights reserved.
//

#import "UIWebviewTestVC.h"
#import "NJKWebViewProgressView.h"
#import "NJKWebViewProgress.h"

@interface UIWebviewTestVC ()<UIWebViewDelegate,NJKWebViewProgressDelegate>
{
    UIWebView *_webView;
    NJKWebViewProgressView *_webViewProgressView;
    NJKWebViewProgress *_webViewProgress;
}

@end

@implementation UIWebviewTestVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    _webView = [[UIWebView alloc] initWithFrame:self.view.frame];
    [self.view addSubview: _webView];
    
    _webViewProgress = [[NJKWebViewProgress alloc] init];
    _webView.delegate = _webViewProgress;
    _webViewProgress.webViewProxyDelegate = self;
    _webViewProgress.progressDelegate = self;
    
    CGRect navBounds = self.navigationController.navigationBar.bounds;
    CGRect barFrame = CGRectMake(0,
                                 navBounds.size.height - 2,
                                 navBounds.size.width,
                                 2);
    _webViewProgressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _webViewProgressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [_webViewProgressView setProgress:0 animated:YES];
    
    NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.hao123.com"]];
    [_webView loadRequest:request];
}

-(void)viewWillAppear:(BOOL)animated
{
    [self.navigationController.navigationBar addSubview:_webViewProgressView];
}

-(void)viewWillDisappear:(BOOL)animated
{
    [_webViewProgressView removeFromSuperview];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    [_webViewProgressView setProgress:progress animated:YES];
    self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

- (void) webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"webViewDidStartLoad");
}
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"webViewDidFinishLoad");
}
- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"didFailLoadWithError:%@", error);
}

@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

其他问题参考: http://blog.csdn.net/hongfengkt/article/details/48543091







 类似资料: