//
// WebViewVC.h
// 封装url
//
// Created by CJW on 16/7/14.
// Copyright © 2016年 ch. All rights reserved.
//
第三方用到的是 NJKWebViewProgress 和 PureLayout
#import <UIKit/UIKit.h>
#import "NJKWebViewProgress.h"
#import "PureLayout.h"
@interface WebViewVC : UIViewController
@property (nonatomic,strong) NSString * url;//网址string
@property (nonatomic,strong) NSString * navTitle;//标题
@property (nonatomic,strong) NJKWebViewProgress * progressProxy;//进度条
@property (nonatomic,strong) UIWebView * webView;//网页展示
@property (nonatomic,strong) UIProgressView * progressBar;//进度条
@end
//
// WebViewVC.m
// 封装url
//
// Created by CJW on 16/7/14.
// Copyright © 2016年 ch. All rights reserved.
//
#import "WebViewVC.h"
@interface WebViewVC ()<UIWebViewDelegate,NJKWebViewProgressDelegate>
@end
@implementation WebViewVC
- (void)viewDidLoad {
[super viewDidLoad];
[self configUI];
[self goToWebView];
}
#pragma mark - 懒加载控件 -
//webView
-(UIWebView *)webView
{
if (!_webView)
{
_webView = [[UIWebView alloc]init];
}
return _webView;
}
//进度条
-(UIProgressView *)progressBar
{
if (!_progressBar)
{
_progressBar = [[UIProgressView alloc]init];
}
return _progressBar;
}
#pragma mark - 初始化UI -
-(void)configUI
{
UIBarButtonItem * backItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back_Nav"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack)];
UIBarButtonItem * closeWebItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"close"] style:UIBarButtonItemStylePlain target:self action:@selector(closeWebView)];
self.navigationItem.leftBarButtonItems = @[backItem,closeWebItem];
self.view.backgroundColor = [UIColor whiteColor];
//
_webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
_progressBar = [[UIProgressView alloc]init];
[self.view addSubview:self.webView];
[self.view addSubview:self.progressBar];
//布局
[_webView autoPinEdgesToSuperviewEdges];
[self.progressBar autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeBottom];
}
#pragma mark - 加载网页 -
-(void)goToWebView
{
_progressProxy = [[NJKWebViewProgress alloc]init];
_webView.delegate = self;
_webView.scalesPageToFit = YES;
_webView.allowsInlineMediaPlayback = YES;
_progressProxy.webViewProxyDelegate = self;
_progressProxy.progressDelegate = self;
NSURL * urlstr = [NSURL URLWithString:self.url];
NSLog(@"string -->%@",_url);
[_webView loadRequest:[NSURLRequest requestWithURL:urlstr]];
}
#pragma mark - 返回首页 -
-(void)closeWebView
{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 放回上一页 -
-(void)goBack
{
if (_webView.canGoBack == YES)
{
[_webView goBack];
}
else{
[self.navigationController popViewControllerAnimated:YES];
}
}
#pragma mark - NJKWebViewProgressDelegate -
- (void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
if (progress == 0.0)
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
self.progressBar.progress = 0;
[UIView animateWithDuration:0.27 animations:^{
self.progressBar.alpha = 1.0;
}];
}
if (progress == 1.0)
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[UIView animateWithDuration:0.27 delay:(NSTimeInterval)(progress - _progressBar.progress) options:UIViewAnimationOptionLayoutSubviews animations:^{
self.progressBar.alpha = 0;
} completion:^(BOOL finished) {
}];
}
[self.progressBar setProgress:progress];
}
#pragma mark - UIWebViewDelegate -
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"request--->%@",request);
return YES;
}
-(void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(@"%@",webView.request.URL.relativeString);
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
if (_navTitle != nil && _navTitle.length >0)
{
self.title = _navTitle;
}
else
{
self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(@"error ---> %@",error);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
//
// CJWWebTool.h
// 封装url
//
// Created by CJW on 16/7/14.
// Copyright © 2016年 ch. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface CJWWebTool : NSObject
+(void)goToWebViewWithVC:(UIViewController *)target withURL:(NSString *)url andTitle:(NSString *)title;
@end
//
// CJWWebTool.m
// 封装url
//
// Created by CJW on 16/7/14.
// Copyright © 2016年 ch. All rights reserved.
//
#import "CJWWebTool.h"
#import "WebViewVC.h"
@implementation CJWWebTool
+(void)goToWebViewWithVC:(UIViewController *)target withURL:(NSString *)url andTitle:(NSString *)title
{
NSLog(@"string -->%@",url);
WebViewVC * web = [[WebViewVC alloc]init];
web.url = url;
if (title != nil)
{
web.title = title;
}else
{
}
[target.navigationController pushViewController:web animated:YES];
}
@end
调用的时候只需要一句话
[CJWWebTool goToWebViewWithVC:self withURL:@"http://www.baidu.com" andTitle:@"百度一下"];
880

被折叠的 条评论
为什么被折叠?



