//
// ViewController.h
// KouDaiChenhZhou
//
// Created by jishubumac on 14/11/7.
// Copyright (c) 2014年 jishubumac. All rights reserved.
//
//UIWebView下拉刷新框架原创下载地址:http://code4app.com/ios/UIWebView-%E4%B8%8B%E6%8B%89%E5%88%B7%E6%96%B0/51aee3c16803fa770f000003
#import <UIKit/UIKit.h>
#import "EGORefreshTableHeaderView.h"//导入下拉刷新框架头文件
//引用协议
@interface ViewController : UIViewController<UIWebViewDelegate, UIScrollViewDelegate, EGORefreshTableHeaderDelegate,UITabBarDelegate> {
//下拉视图
EGORefreshTableHeaderView * _refreshHeaderView;
//刷新标识,是否正在刷新过程中
BOOL _reloading;
}
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) NSURL *homeurl;//主页url
//
// ViewController.m
// KouDaiChenhZhou
//
// Created by jishubumac on 14/11/7.
// Copyright (c) 2014年 jishubumac. All rights reserved.
//
#import "ViewController.h"
#import "PublicHeader.h"
#import <ShareSDK/ShareSDK.h>
#import <AdSupport/AdSupport.h>//为了获取User Agent而导入
@interface ViewController ()
{
UIActivityIndicatorView *loadView;
NSMutableURLRequest *_request;
UIView *bgview;
UILabel *titlelabel;
}
@end
@implementation ViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
//添加背景
- (void)addbackgroundView
{
bgview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.navigationController.navigationBar.frame.size.width,self.navigationController.navigationBar.frame.size.height)];
bgview.backgroundColor = [UIColor colorWithRed:0.0 green:0.85 blue:0.0 alpha:1.0];
[self.navigationController.navigationBar addSubview:bgview];
}
//页标题
- (void)addLabel
{
titlelabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 12, 100, 20)];
titlelabel.font = [UIFont boldSystemFontOfSize:20.0f];
titlelabel.textColor = [UIColor whiteColor];
// titlelabel.text = @"口袋郴州";
[bgview addSubview:titlelabel];
}
- (void)addButton
{
//分享按钮
UIButton *sharebtn = [UIButton buttonWithType:UIButtonTypeCustom];
sharebtn.frame = CGRectMake(self.view.frame.size.width - 22-15, 11, 22, 22);
[sharebtn setImage:[UIImage imageNamed:@"分享_当前.png"] forState:UIControlStateNormal];
[sharebtn setImage:[UIImage imageNamed:@"分享_按下.png"] forState:UIControlStateHighlighted];
[sharebtn addTarget:self action:@selector(didshare) forControlEvents:UIControlEventTouchUpInside];
[bgview addSubview:sharebtn];
}
- (void)addActivityIndicatorView
{
loadView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
loadView.center = self.view.center;
loadView.color=[UIColor colorWithWhite:0.4 alpha:1];
[self.view addSubview:loadView];
[loadView startAnimating];
}
//网页加载请求数据
- (void)webViewLoadRequest:(NSURL *)url
{
_request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
[_webView loadRequest:_request];
NSLog(@"%@",_request);
}
- (void)viewDidAppear:(BOOL)animated
{
[self webViewLoadRequest:_homeurl];
}
- (void)viewDidLoad {
[super viewDidLoad];
//识别客户端
NSDictionary *dictionary = @{@"UserAgent": @"Mozilla/5.0 (iPhone; CPU iPhone OS 10_10 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Mobile/12A365 client-cz365;{mac:57DBCBDC-597B-458E-91C4-D85C10597384;{token:57DBCBDC-597B-458E-91C4-D85C10597384;"};
NSString *adId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString];
NSLog(@"%@",adId);
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
[self addbackgroundView];
[self addLabel];
[self addButton];
[self addActivityIndicatorView];
//初始化webView
_webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 - self.tabBarController.tabBar.frame.size.height)];
_webView.scalesPageToFit = YES;//按比例适配屏幕
_webView.detectsPhoneNumbers = YES;//允许获取网页上的号码并拨打
[_webView setUserInteractionEnabled: YES ]; //是否支持交互
_webView.keyboardDisplayRequiresUserAction = YES;//交互时显示键盘
_webView.delegate = self;
_webView.scrollView.delegate = self;//设置代理,这一环对添加下拉刷新至关重要
//初始化refreshView,添加到webview 的 scrollView子视图中
if (_refreshHeaderView == nil) {
_refreshHeaderView = [[EGORefreshTableHeaderView alloc] initWithFrame:CGRectMake(0, 0-self.webView.scrollView.bounds.size.height, self.webView.scrollView.frame.size.width, self.webView.scrollView.bounds.size.height)];
_refreshHeaderView.delegate = self;
[self.webView.scrollView addSubview:_refreshHeaderView];
}
[_refreshHeaderView refreshLastUpdatedDate];
_homeurl = [NSURL URLWithString:@"http://m.cz365.com/"];
[self webViewLoadRequest:_homeurl];
}
#pragma mark - webViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
//回调时获取的四种不同的url
// http://m.cz365.com/user/error_login.aspx
// cz365callback://openurl?url=/quan/
// cz365callback://gotopage?page=0&url=/forum/forum-5-1.html
// cz365callback://openurl?url=http://m.cz365.com/lottery/detail.aspx
//判断导航类型
if (navigationType == UIWebViewNavigationTypeOther) {
//回调跳转时,捕获设置标题的url,通过url解码,判别和字符串处理,得到标题
NSString *str2 = [[request.URL absoluteString] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"%@",str2);
if ([str2 hasPrefix:@"cz365callback://settitle?type=left&title="]) {
NSMutableString *mstr2 = [NSMutableString stringWithString:str2];
[mstr2 deleteCharactersInRange:NSMakeRange(0, 41)];
titlelabel.text = mstr2; }
if ([str2 hasPrefix:@"cz365callback://settitle?type=center&title="]){
NSMutableString *mstr3 = [NSMutableString stringWithString:str2];
[mstr3 deleteCharactersInRange:NSMakeRange(0, 43)];
titlelabel.text = mstr3;
}
}
//判断是否单击
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
DetailViewController *DVCtrl = [[DetailViewController alloc] init];
//获取url
NSURL *url = [request URL];
NSString *curUrl= [url absoluteString];
//解码
NSString *str = [curUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//判别4种url,并进行字符串处理,提取url
if([str hasPrefix:@"http://"]){
//在当前页显示
}
else{
NSMutableString *mstr = [NSMutableString stringWithString:str];
[mstr deleteCharactersInRange:NSMakeRange(0,16)];
if ([mstr hasPrefix:@"openurl?url="]) {
[mstr deleteCharactersInRange:NSMakeRange(0,12)];
if ([mstr hasPrefix:@"http://"]) {
// 个别特殊的,需要编码才能打开的url
if ([mstr isEqualToString:@"http://m.news.so.com/ns?q=郴州&pq=&src=srp&_ms=0&log_id=5370304"] ) {
NSString * encodingString = [mstr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
DVCtrl.currenturl = [NSURL URLWithString:encodingString];// 若用未编码(含中文)的字符串初始化url,则初始化的url为空
NSLog(@"%@",encodingString);
}
else{
DVCtrl.currenturl = [NSURL URLWithString:mstr];//属性传值
NSLog(@"%@",mstr);
}
}
else{
NSString *urlstr = [NSString stringWithFormat:@"http://m.cz365.com%@",mstr];
DVCtrl.currenturl = [NSURL URLWithString:urlstr];
}
[self.navigationController pushViewController:DVCtrl animated:YES];//push到窗口二显示
self.tabBarController.tabBar.hidden = YES;//隐藏tabBar
[self.navigationController setNavigationBarHidden:YES animated:YES];//隐藏导航条
}
else{
NSString *numstr = [mstr substringWithRange:NSMakeRange(14, 1)];
NSLog(@"%@",numstr);
NSInteger index = [numstr intValue];
NSLog(@"%d",index);
//传送tabBar.selectedIndex当前值
[[NSNotificationCenter defaultCenter] postNotificationName:@"selectedIndex" object:numstr];
[mstr deleteCharactersInRange:NSMakeRange(0,20)];
NSString *urlstr2 = [NSString stringWithFormat:@"http://m.cz365.com%@",mstr];
//给不同页面传其当前url,并在选中的页面打开该url
switch (index) {
case 0:{[[NSNotificationCenter defaultCenter] postNotificationName:@"Firstnotification" object:urlstr2];self.tabBarController.selectedIndex = index;}
break;
case 1:{
[[NSNotificationCenter defaultCenter] postNotificationName:@"Secondnotification" object:urlstr2];self.tabBarController.selectedIndex = index;
}
break;
case 2:{[[NSNotificationCenter defaultCenter] postNotificationName:@"Thirdnotification" object:urlstr2];self.tabBarController.selectedIndex = index;}
break;
case 3:{[[NSNotificationCenter defaultCenter] postNotificationName:@"Fourthnotification" object:urlstr2];self.tabBarController.selectedIndex = 3;}
break;
case 4:{[[NSNotificationCenter defaultCenter] postNotificationName:@"Fifthnotification" object:urlstr2];self.tabBarController.selectedIndex = 4;}
break;
default:
break;
}
return NO;
}
}
}
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)webView
{
NSLog(@"Start loading...");
_reloading = YES;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
//webView重载完成后,禁止刷新
_reloading = NO;
[_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.webView.scrollView];
//获取原有User Agent
NSString *secretAgent = [webView stringByEvaluatingJavaScriptFromString:@"navigator.userAgent"];//OC和js交互
NSLog(@"%@",secretAgent);
NSLog(@"finished");
[self.view addSubview:_webView];
[loadView stopAnimating];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
NSLog(@"load page error:%@", [error description]);
_reloading = NO;
[_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:self.webView.scrollView];
}
- (void)didshare
{
NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"ShareSDK" ofType:@"jpg"];
//构造分享内容
id<ISSContent> publishContent = [ShareSDK content:@"分享内容"
defaultContent:@"默认分享内容,没内容时显示"
image:[ShareSDK imageWithPath:imagePath]
title:@"ShareSDK"
url:@"http://www.sharesdk.cn"
description:@"这是一条测试信息"
mediaType:SSPublishContentMediaTypeNews];
[ShareSDK showShareActionSheet:nil
shareList:nil
content:publishContent
statusBarTips:YES
authOptions:nil
shareOptions: nil
result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
if (state == SSResponseStateSuccess)
{
NSLog(@"分享成功");
}
else if (state == SSResponseStateFail)
{
NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"发布失败!error code == %d, error code == %@"), [error errorCode], [error errorDescription]);
}
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark -
#pragma mark UIScrollViewDelegate Methods
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
[_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
[_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
}
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{
[_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
}
#pragma mark -
#pragma mark EGORefreshTableHeaderDelegate Methods
- (void)egoRefreshTableHeaderDidTriggerRefresh:(EGORefreshTableHeaderView*)view{
[_webView reload];//下拉刷新,重新加载网页
}
- (BOOL)egoRefreshTableHeaderDataSourceIsLoading:(EGORefreshTableHeaderView*)view{
return _reloading; // should return if data source model is reloading
}
- (NSDate*)egoRefreshTableHeaderDataSourceLastUpdated:(EGORefreshTableHeaderView*)view{
return [NSDate date]; // should return date data source was last changed
}
@end