@property (nonatomic, copy) NSString * url;
@interface WebViewController ()<UIWebViewDelegate,UIGestureRecognizerDelegate>
@property (nonatomic, weak) UIWebView * webView;
@property (nonatomic, weak) UIButton * backItem;
@property (nonatomic, weak) UIButton * closeItem;
@property (nonatomic, weak) UIActivityIndicatorView * activityView;
@end
@implementation WebViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self initNaviBar];
[self initWebView];
}
- (void)initWebView{
UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
webView.delegate = self;
[self.view addSubview:webView];
self.webView = webView;
//activityView
UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityView.center = self.view.center;
[activityView startAnimating];
self.activityView = activityView;
[self.view addSubview:activityView];
//清除UIWebView的缓存
[[NSURLCache sharedURLCache] removeAllCachedResponses];
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.url] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:10.0]];
}
- (void)initNaviBar{
UIView * backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 44)];
backView.backgroundColor = [UIColor purpleColor];
UIButton * backItem = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 56, 44)];
[backItem setImage:[UIImage imageNamed:@"back_arrow"] forState:UIControlStateNormal];
[backItem setImageEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)];
[backItem setTitle:@"返回" forState:UIControlStateNormal];
[backItem setTitleEdgeInsets:UIEdgeInsetsMake(0, -15, 0, 0)];
[backItem setTitleColor:[UIColor colorWithRed:0.000 green:0.502 blue:1.000 alpha:1.000] forState:UIControlStateNormal];
[backItem addTarget:self action:@selector(clickedBackItem:) forControlEvents:UIControlEventTouchUpInside];
self.backItem = backItem;
[backView addSubview:backItem];
UIButton * closeItem = [[UIButton alloc]initWithFrame:CGRectMake(44+12, 0, 44, 44)];
[closeItem setTitle:@"关闭" forState:UIControlStateNormal];
[closeItem setTitleColor:[UIColor colorWithRed:0.000 green:0.502 blue:1.000 alpha:1.000] forState:UIControlStateNormal];
[closeItem addTarget:self action:@selector(clickedCloseItem:) forControlEvents:UIControlEventTouchUpInside];
closeItem.hidden = YES;
self.closeItem = closeItem;
[backView addSubview:closeItem];
UIBarButtonItem * leftItemBar = [[UIBarButtonItem alloc]initWithCustomView:backView];
self.navigationItem.leftBarButtonItem = leftItemBar;
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
if (self.webView.canGoBack) {
[self.webView goBack];
self.closeItem.hidden = NO;
return NO;
}
return YES;
}
#pragma mark - clickedBackItem
- (void)clickedBackItem:(UIBarButtonItem *)btn{
if (self.webView.canGoBack) {
[self.webView goBack];
self.closeItem.hidden = NO;
}else{
[self clickedCloseItem:nil];
}
}
#pragma mark - clickedCloseItem
- (void)clickedCloseItem:(UIButton *)btn{
[self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - UIWebViewDelegate
- (void)webViewDidStartLoad:(UIWebView *)webView{
self.activityView.hidden = NO;
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSLog(@"url: %@", request.URL.absoluteURL.description);
if (self.webView.canGoBack) {
self.closeItem.hidden = NO;
}
return YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
self.activityView.hidden = YES;
self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
self.activityView.hidden = YES;
}