|
//
// TPWebViewContoller.m
// ThePaperDemo
//
// Created by scar1900 on 14/10/28.
// Copyright (c) 2014年 scar1900. All rights reserved.
//
#import "TPWebViewContoller.h"
@interface TPWebViewContoller ()<UIWebViewDelegate>
@end
@implementation TPWebViewContoller
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
[self.webView addSubview:self.activityView];
[self.naviBar addSubview:self.safariButton];
[self.safariButton addSubview:self.safariLabel];
[self.safariButton addSubview:self.safariImage];
[self.activityView makeConstraints:^(MASConstraintMaker *make) {
make.center.equalTo(self.webView.center);
make.width.and.height.mas_equalTo(@20);
}];
float statusBarHeight = [[UIApplication sharedApplication] statusBarFrame].size.height;
[self.safariButton makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.naviBar.top).offset(statusBarHeight);
make.bottom.equalTo(self.naviBar.bottom);
make.right.equalTo(self.naviBar.right);
make.width.mas_equalTo(@120);
}];
[self.safariLabel makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.safariButton.left);
make.centerY.equalTo(self.safariButton.centerY);
make.height.mas_equalTo(@20);
}];
[self.safariImage makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(self.safariButton.centerY);
make.size.equalTo(CGSizeMake(21.75, 21.75));
make.left.equalTo(self.safariLabel.right).offset(7.5);
make.right.equalTo(self.safariButton.right).offset(-10);
}];
[MobClick event:@"7"];
}
- (UIButton*)safariButton {
if (!_safariButton) {
_safariButton = [UIButton buttonWithType:UIButtonTypeCustom];
_safariButton.backgroundColor = [UIColor clearColor];
[_safariButton addTarget:self action:@selector(openLinkWithSafari) forControlEvents:UIControlEventTouchUpInside];
}
return _safariButton;
}
- (UILabel*)safariLabel {
if (!_safariLabel) {
_safariLabel = [[UILabel alloc]init];
_safariLabel.font = appFont(TEXT_FIVE_LEVELSIZE, NO);
_safariLabel.textColor = [UIColor colorWithHexString:TextBlack];
_safariLabel.textAlignment = NSTextAlignmentRight;
_safariLabel.text = @"用Safari打开";
_safariLabel.backgroundColor = [UIColor clearColor];
}
return _safariLabel;
}
- (UIImageView*)safariImage {
if (!_safariImage) {
_safariImage = [[UIImageView alloc]initWithImage:Image(@"Button/safariBtn.png")];
_safariImage.userInteractionEnabled = NO;
}
return _safariImage;
}
- (UIWebView*)webView {
if (!_webView) {
_webView = [[UIWebView alloc] init];
_webView.backgroundColor = [UIColor clearColor];
_webView.scalesPageToFit = YES;
_webView.delegate = self;
}
return _webView;
}
- (UIActivityIndicatorView*)activityView {
if (!_activityView) {
_activityView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
_activityView.hidesWhenStopped = YES;
[_activityView stopAnimating];
}
return _activityView;
}
- (void)setUrl:(NSString *)urlStr {
_url = urlStr;
//if (!_webView) { //bug5375: 外链新闻及专题的问答详情页点击原新闻,无法进入原新闻
[self.view addSubview:self.webView];
[self.webView makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.naviBar.bottom);
make.left.equalTo(self.view.left);
make.right.equalTo(self.view.right);
make.bottom.equalTo(self.view.bottom);
}];
//}
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:6050(广告:点击可下载app的广告,应直接跳入appstore下载链接)
*/
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *requestedURL = [[request URL] absoluteString];
TPLOG(@"%@",requestedURL);
if (webView == _webView) {
// Return no, we don't care about executing an actual request.
return YES;
}
return YES;
}
#pragma mark - button event
- (void)openLinkWithSafari {
NSURL *newURL = [NSURL URLWithString:self.url];
[[UIApplication sharedApplication] openURL:newURL];
}
- (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;
}
- (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
|