// // TPWebViewContoller.m // ThePaperDemo // // Created by scar1900 on 14/10/28. // Copyright (c) 2014年 scar1900. All rights reserved. // #import "TPWebViewContoller.h" @interface TPWebViewContoller () @end @implementation TPWebViewContoller @synthesize url = _url; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [MobClick event:@"3"]; [self.view addSubview:self.activityView]; [self leftIconButton:@"Button/backBarBtn.png" highlited:nil selector:@selector(backButtonHandler:)]; [self.naviBarView addSubview:self.safariButton]; [self.navigationController.navigationBar addSubview:self.naviBarView]; } - (void)statusBarOrientationChange:(NSNotification *)notification{ self.naviBarView.frame = CGRectMake(CGRectGetWidth(self.view.bounds)-180, 0, 180, 45); if (isNotIOS8) { UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { self.naviBarView.frame = CGRectMake(1024-180, 0, 180, 45); }else { self.naviBarView.frame = CGRectMake(768-180, 0, 180, 45); } } if (isNotIOS8) { UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { self.webView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)); } } self.activityView.frame = CGRectMake(CGRectGetWidth(self.webView.bounds)/2-10,CGRectGetHeight(self.webView.bounds)/2-10 , 20, 20); self.activityView.center = self.webView.center; } - (UIView*)naviBarView { if (!_naviBarView) { _naviBarView = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetWidth(self.view.bounds)-180, 0, 180, 45)]; if (isNotIOS8) { UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { _naviBarView.frame = CGRectMake(1024-180, 0, 180, 45); }else { _naviBarView.frame = CGRectMake(768-180, 0, 180, 45); } } _naviBarView.backgroundColor = [UIColor clearColor]; } return _naviBarView; } - (UIButton*)safariButton { if (!_safariButton) { _safariButton = [UIButton buttonWithType:UIButtonTypeCustom]; _safariButton.backgroundColor = [UIColor clearColor]; _safariButton.frame = CGRectMake(0, 0, 150, CGRectGetHeight(self.naviBarView.bounds)); [_safariButton addTarget:self action:@selector(openLinkWithSafari) forControlEvents:UIControlEventTouchUpInside]; UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 208/2, CGRectGetHeight(_safariButton.bounds))]; lab.font = appFont(18, NO); lab.textColor = [UIColor whiteColor]; lab.textAlignment = NSTextAlignmentLeft; lab.text = @"用safari打开"; lab.backgroundColor = [UIColor clearColor]; [_safariButton addSubview:lab]; UIImageView *image = [[UIImageView alloc]initWithImage:Image(@"Button/safariBtn.png")]; image.frame = CGRectMake(CGRectGetMaxX(lab.frame)+11, (CGRectGetHeight(_safariButton.bounds)-30)/2, 30, 30); image.userInteractionEnabled = NO; [_safariButton addSubview:image]; } return _safariButton; } - (void)backButtonHandler:(id)sender { [self.naviBarView removeFromSuperview]; self.naviBarView = nil; if (!self.isPresent) { [self.navigationController popViewControllerAnimated:YES]; }else { [self dismissViewControllerAnimated:YES completion:nil]; } } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; self.navigationController.navigationBarHidden = NO; self.navigationController.navigationBar.translucent = NO; if (isIOS7) { self.navigationController.navigationBar.barTintColor = [UIColor colorWithHexString:@"0x666666"]; }else { self.navigationController.navigationBar.tintColor = [UIColor colorWithHexString:@"0x666666"]; } if ([[TPUserDefault instance].isNightMode intValue] > 0) { self.activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhite; }else { self.activityView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray; } } - (UIWebView*)webView { if (!_webView) { _webView = [[UIWebView alloc]initWithFrame:self.view.bounds]; _webView.backgroundColor = [UIColor whiteColor]; _webView.scalesPageToFit = YES; _webView.delegate = self; _webView.autoresizingMask = AutoresizingFull; } return _webView; } - (UIActivityIndicatorView*)activityView { if (!_activityView) { _activityView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; _activityView.frame = CGRectMake(CGRectGetWidth(self.webView.bounds)/2-10,CGRectGetHeight(self.webView.bounds)/2-10 , 20, 20); _activityView.hidesWhenStopped = YES; _activityView.center = self.webView.center; [_activityView stopAnimating]; } return _activityView; } - (void)setUrl:(NSString *)urlStr { _url = urlStr; if (isNotIOS8) { UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { self.webView.frame = CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)); } } [self.view addSubview:self.webView]; [self.view bringSubviewToFront:self.activityView]; self.activityView.frame = CGRectMake(CGRectGetWidth(self.webView.bounds)/2-10,CGRectGetHeight(self.webView.bounds)/2-10 , 20, 20); NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]; // [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; [self.webView loadRequest:request]; } - (void)webViewDidStartLoad:(UIWebView *)webView { [self.activityView startAnimating]; // self.webView.userInteractionEnabled = NO; } - (void)webViewDidFinishLoad:(UIWebView *)webView { [self.activityView stopAnimating]; [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"]; [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];//自己添加的,原文没有提到。 [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];//自己添加的,原文没有提到。 [[NSUserDefaults standardUserDefaults] synchronize]; } - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { [self.activityView stopAnimating]; NSURL *url = [NSURL URLWithString:[error.userInfo objectForKey:@"NSErrorFailingURLStringKey"]]; NSString *urlString = url.absoluteString; if ([urlString isMatchedByRegex:@"itms-appss"]) { NSURL *newURL = [NSURL URLWithString:urlString]; [[UIApplication sharedApplication] openURL:newURL]; } /** * bug:6069(广告:点击可下载app的广告,应直接跳入appstore下载链接) */ } -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { if (webView == _webView) { // Return no, we don't care about executing an actual request. return YES; } return YES; } - (void)dealloc { NSLog(@"dealloc:%@",self.description); [self.webView stopLoading]; [[NSURLCache sharedURLCache] removeAllCachedResponses]; [self.webView loadHTMLString:@" " baseURL:nil]; [self.webView removeFromSuperview]; [self.activityView removeFromSuperview]; self.webView.delegate = nil; self.webView = nil; self.activityView = nil; [self.naviBarView removeFromSuperview]; self.naviBarView = nil; } #pragma mark - button event - (void)openLinkWithSafari { NSURL *newURL = [NSURL URLWithString:self.url]; [[UIApplication sharedApplication] openURL:newURL]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. NSLog(@"warning!!!!!!!!!"); [[NSURLCache sharedURLCache] removeAllCachedResponses]; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end