|
//
// welcomeAndAdController.m
// ThePaperDemo
//
// Created by scar1900 on 14/12/15.
// Copyright (c) 2014年 scar1900. All rights reserved.
//
#import "welcomeAndAdController.h"
#import <AdsameCubeMaxSDK/AdsameCubeMaxSDK.h>
#import "updateContent.h"
@interface welcomeAndAdController ()<
AdsameCubeMaxDelegate,
updateContentDelegate> {
NSInteger timerCount;
BOOL hiddenStatusBar;
BOOL isAddDataReady;
}
@property(nonatomic, strong)UIImageView *welcomeImgView;
@property(nonatomic, strong)UIView *bannerView;
@property(nonatomic, strong)NSTimer *welcomeTimer;
@property(nonatomic, strong)NSString *displayTime;
@property(nonatomic, strong)NSDictionary *updateInfo;
@property(nonatomic, strong)UIButton *passAdBtn;
@end
@implementation welcomeAndAdController
@synthesize displayTime;
@synthesize delegate;
@synthesize welcomeTimer;
@synthesize remoteUserInfo;
@synthesize updateInfo;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
timerCount = 5;
hiddenStatusBar = YES;
isAddDataReady = NO;
//隐藏状态栏
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
[self prefersStatusBarHidden];
[self setNeedsStatusBarAppearanceUpdate];
}else {
[[UIApplication sharedApplication] setStatusBarHidden:hiddenStatusBar withAnimation:UIStatusBarAnimationFade];
}
self.view.backgroundColor = [UIColor colorWithHexString:BackGroundColor];
[self.view addSubview:self.welcomeImgView];
if ([[TPUserDefault instance].isFirstIntoApp intValue] == 2) {
[self.welcomeImgView addSubview:self.bannerView];
}
self.passAdBtn.hidden = YES;
[self.view addSubview:self.passAdBtn];
// CGFloat centerY = (rect_screen.size.height-CGRectGetHeight(self.bannerView.bounds))/2;
if (_bannerView) {
CGSize imageSize = CGSizeMake(43, 23);
[self.passAdBtn makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(self.bannerView.bottom);
make.bottom.equalTo(self.view.bottom);
make.right.equalTo(self.view.right);
make.width.mas_equalTo(imageSize.width+30);
}];
}
/**
* 加载信息网络请求
*/
[Remote doJsonAction:0 requestUrl:loadingURL parameter:nil delegate:self];
TPLOG(@"%ld",(long)timerCount);
self.welcomeTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(waitForData:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.welcomeTimer forMode:NSRunLoopCommonModes];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
self.delegate = nil;
[self.bannerView removeFromSuperview];
self.bannerView = nil;
[self.welcomeTimer invalidate];
self.welcomeTimer = nil;
}
/**
* 计时器回调
*
* @param timer 计时器
*/
- (void)waitForData:(NSTimer*)timer {
TPLOG(@"%ld",(long)timerCount);
timerCount = timerCount -1;
if (timerCount == 0) {
[self goToFrontPage];
}
}
- (void)goToFrontPage {
[self.welcomeTimer invalidate];
self.welcomeTimer = nil;
hiddenStatusBar = NO;
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
[self prefersStatusBarHidden];
[self setNeedsStatusBarAppearanceUpdate];
}else {
[[UIApplication sharedApplication] setStatusBarHidden:hiddenStatusBar withAnimation:UIStatusBarAnimationFade];
}
/**
* 等待成功,回调给appdelegate
*
* @param goOutWelcomePage 代理回调方法
*
*/
if (!self.remoteUserInfo) {
if (self.updateInfo) {
NSString *latestVersion = updateInfo[@"versionName"];
NSString *currentVersion = [TPUserDefault instance].appVersion;
NSString *ignoreVersion = [TPUserDefault instance].ignoreVersion;
if (isNeedUpdate(latestVersion, currentVersion, ignoreVersion)) {
updateContent *updateView = [[updateContent alloc]initWithFrame:self.view.bounds];
updateView.delegate = self;
[updateView presentShareContentInView:self.view updateInfo:self.updateInfo];
}else {
if ([self.delegate respondsToSelector:@selector(goOutWelcomePage:withAdClick:)]) {
[self.delegate goOutWelcomePage:self.remoteUserInfo withAdClick:nil];
}
}
}else {
if ([self.delegate respondsToSelector:@selector(goOutWelcomePage:withAdClick:)]) {
[self.delegate goOutWelcomePage:self.remoteUserInfo withAdClick:nil];
}
}
}else {
if ([self.delegate respondsToSelector:@selector(goOutWelcomePage:withAdClick:)]) {
[self.delegate goOutWelcomePage:self.remoteUserInfo withAdClick:nil];
}
}
}
- (BOOL)prefersStatusBarHidden
{
return hiddenStatusBar;
}
- (UIView *)bannerView {
if (!_bannerView) {
_bannerView = [AdsameCubeMaxSDK getBannerWithCID:[adSamePublishId intValue]
width:rect_screen.size.width
height:(rect_screen.size.height-0.3*rect_screen.size.width)
delegate:self];
_bannerView.frame = CGRectMake(0, 0, rect_screen.size.width, (rect_screen.size.height-0.3*rect_screen.size.width));
}
return _bannerView;
}
- (UIButton *)passAdBtn {
if (!_passAdBtn) {
_passAdBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[_passAdBtn setImage:Image(@"Button/passAdIcon.png") forState:UIControlStateNormal];
[_passAdBtn addTarget:self action:@selector(passAd:) forControlEvents:UIControlEventTouchUpInside];
_passAdBtn.backgroundColor = [UIColor clearColor];
}
return _passAdBtn;
}
- (UIImageView*)welcomeImgView {
if (!_welcomeImgView) {
_welcomeImgView = [[UIImageView alloc]initWithFrame:self.view.bounds];
CGFloat height = rect_screen.size.height;
if (height == 960/2) {
UIImage *frontImage =Image(@"other/640*960.png");
_welcomeImgView.image = frontImage;
}else if (height == 1136/2) {
UIImage *frontImage =Image(@"other/640*1136.png");
_welcomeImgView.image = frontImage;
}else if (height == 1334/2) {
UIImage *frontImage = Image(@"other/750*1334.png");
_welcomeImgView.image = frontImage;
}else if (height == 2208/3) {
UIImage *frontImage = Image(@"other/1242*2208.png");
_welcomeImgView.image = frontImage;
}
_welcomeImgView.userInteractionEnabled = YES;
}
return _welcomeImgView;
}
/**
* 检查是否可以放置广告
*/
- (void)checkIsitCouldBeAddAd {
if (isAddDataReady) {
if (self.displayTime && !isBlankString(self.displayTime)) {
[self.welcomeTimer invalidate];
self.welcomeTimer = nil;
timerCount = [self.displayTime integerValue];
self.welcomeTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(waitForData:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.welcomeTimer forMode:NSRunLoopCommonModes];
}
}
}
#pragma mark - adsome delegate
/**
* 广告成功回调代理
*/
-(void)onAdsDataReady {
TPLOG(@"=======ad data is ready!=========");
}
-(void)onAdsLoadingFailed:(adsErrorCode)errorCode {
TPLOG(@"========load add failed!:%d========",errorCode);
if (errorCode == ADSAME_ADSERROR_RESOURCES_LOADED) {
self.passAdBtn.hidden = NO;
if (isAddDataReady) {
return;
}
isAddDataReady = YES;
[self checkIsitCouldBeAddAd];
}else if(errorCode == ADSAME_ADSERROR_INVALID_ADS || errorCode == ADSAME_ADSERROR_REQUEST_FAILED){
if (self.updateInfo) {
updateContent *updateView = [[updateContent alloc]initWithFrame:self.view.bounds];
updateView.delegate = self;
[updateView presentShareContentInView:self.view updateInfo:self.updateInfo];
}
if ([self.delegate respondsToSelector:@selector(goOutWelcomePage:withAdClick:)]) {
[self.delegate goOutWelcomePage:self.remoteUserInfo withAdClick:nil];
}
}
}
-(void)onAdsSwitching {
}
-(void)onAdsImpressed {
}
-(Boolean)onAdsClicked:(NSString *)clickUrl {
[MobClick event:@"17"];
TPLOG(@"======Click Ads:%@======",clickUrl);
[self.welcomeTimer invalidate];
self.welcomeTimer = nil;
remotePushBO *pushBO = remotePushBO.new;
NSMutableArray *comList = [NSMutableArray arrayWithArray:[clickUrl componentsSeparatedByString:@"|"]];
if ([comList[0] intValue] == 6) {
if (comList.count != 2) {
NSString *lastStr = [comList lastObject];
if ([lastStr hasPrefix:@"http"]) {
if ([lastStr isMatchedByRegex:@"__IDFA__"]) {
[lastStr stringByReplacingOccurrencesOfString:@"__IDFA__" withString:[TPUserDefault instance].IDFA];
}
[comList removeLastObject];
[Remote doSimpleAction:0 requestUrl:lastStr];
}
}
}else {
NSString *lastStr = [comList lastObject];
if ([lastStr hasPrefix:@"http"]) {
if ([lastStr isMatchedByRegex:@"__IDFA__"]) {
[lastStr stringByReplacingOccurrencesOfString:@"__IDFA__" withString:[TPUserDefault instance].IDFA];
}
[comList removeLastObject];
[Remote doSimpleAction:0 requestUrl:lastStr];
}
}
if (comList.count == 3) {
[comList removeLastObject];
}
if (comList.count == 2) {
pushBO.linkType = comList[0];
pushBO.contId = comList[1];
}
if (comList.count == 4) {
pushBO.linkType = comList[0];
pushBO.contId = comList[2];
}
pushBO.isComeFromRemote = @"0";
// [TPUserDefault instance].pushBO = pushBO;
if ([self.delegate respondsToSelector:@selector(goOutWelcomePage:withAdClick:)]) {
[self.delegate goOutWelcomePage:self.remoteUserInfo withAdClick:pushBO];
return YES;
}else return NO;
}
//这个回调只有全屏广告关闭时才有,Banner没有
-(void)onFullScreenAdsClosed {
}
- (void)didDismissUpdateContent:(UIView *)contentView {
contentView = nil;
if ([self.delegate respondsToSelector:@selector(goOutWelcomePage:withAdClick:)]) {
[self.delegate goOutWelcomePage:self.remoteUserInfo withAdClick:nil];
}
}
#pragma mark - remote delegate
- (void)remoteResponsSuccess:(int)actionTag withResponsData:(id)responsData{
if (actionTag == 0) {
if (responsData) {
NSDictionary *loadingInfo = responsData[@"loadingInfo"];
self.updateInfo = responsData[@"versionInfo"];
if ([[TPUserDefault instance].isFirstIntoApp intValue] == 1) {
if ([self.delegate respondsToSelector:@selector(goOutWelcomePage:withAdClick:)]) {
[self.delegate goOutWelcomePage:self.remoteUserInfo withAdClick:nil];
}
}else {
if (loadingInfo) {
self.displayTime = loadingInfo[@"displayTime"]?loadingInfo[@"displayTime"]:@"";
[self checkIsitCouldBeAddAd];
}
}
}
}
}
- (void)remoteResponsFailed:(int)actionTag withMessage:(NSString *)message resultCode:(NSString *)code {
// ShowTextMessage(message);
// if ([self.delegate respondsToSelector:@selector(goOutWelcomePage:)]) {
// [self.delegate goOutWelcomePage:self.remoteUserInfo];
// }
}
- (void)passAd:(UIButton*)btn {
[self goToFrontPage];
/**
* bug:5153(原生版本在进入程序时,如果点击跳过,则不会弹出新版本更新消息)
*/
}
- (void)startWaitCursor:(int)actionTag {
}
- (void)stopWaitCursor:(int)actionTag {
}
- (void)dealloc {
self.displayTime = nil;
self.welcomeImgView = nil;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
|