// // GiGaWebViewController.m // GIGA // // Created by lianxiang on 2018/8/21. // Copyright © 2018年 com.giga.ios. All rights reserved. // #import "GiGaWebViewController.h" #import "NJKWebViewProgressView.h" #import "NJKWebViewProgress.h" #import "UIView+Toast.h" @interface GiGaWebViewController () { NJKWebViewProgressView *_progressView; NJKWebViewProgress *_progressProxy; } @property (weak, nonatomic) IBOutlet UIWebView *webview; @end @implementation GiGaWebViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. _progressProxy = [[NJKWebViewProgress alloc] init]; _webview.delegate = _progressProxy; _progressProxy.webViewProxyDelegate = self; _progressProxy.progressDelegate = self; CGRect navBounds = self.navigationController.navigationBar.bounds; CGRect barFrame = CGRectMake(0, navBounds.size.height - 2, navBounds.size.width,2); _progressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame]; _progressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; [_progressView setProgress:0 animated:YES]; [self loadWeb]; } -(void)viewWillAppear:(BOOL)animated{ [self.navigationController.navigationBar addSubview:_progressView]; } -(void)viewWillDisappear:(BOOL)animated{ [_progressView removeFromSuperview]; } -(void)loadWeb{ switch (self.souceType) { case WebSourceTypeUrlLink: { if (!self.url) { [self.view makeToast:@"链接为空" duration:1 position:CSToastPositionCenter]; return; } NSURLRequest *req = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:self.url]]; [_webview loadRequest:req]; } break; case WebSourceTypeLoacalHtml: { if (!self.fileName) { [self.view makeToast:@"无此文件" duration:1 position:CSToastPositionCenter]; return; } NSString *filepath = [[NSBundle mainBundle] pathForResource: self.fileName ofType:@"html"]; NSString *html = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil]; [_webview loadHTMLString:html baseURL:[NSURL URLWithString:filepath]]; } break; default: break; } } #pragma mark -NJKWebViewProgressDelegate -(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress{ [_progressView setProgress:progress animated:YES]; self.title = [_webview stringByEvaluatingJavaScriptFromString:@"document.title"]; NSLog(@"%@",self.title); } - (void)webViewDidStartLoad:(UIWebView *)webView{ NSLog(@"startLoad"); [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; } -(void)webViewDidFinishLoad:(UIWebView *)webView{ NSLog(@"finishLoad"); [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO]; } -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{ NSLog(@"error:%@",error); [_progressView setProgress:0 animated:NO]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)removeCookies { NSHTTPCookieStorage *cookies = [NSHTTPCookieStorage sharedHTTPCookieStorage]; NSArray *pcookies = [cookies cookiesForURL:[NSURL URLWithString:self.url]]; for (NSHTTPCookie *cookie in pcookies) { [cookies deleteCookie:cookie]; } } @end