澎湃iPad上线版本

welcomeAndAdController.m 14KB

    // // welcomeAndAdController.m // ThePaperHD // // Created by scar1900 on 15/4/1. // Copyright (c) 2015年 scar1900. All rights reserved. // #import "welcomeAndAdController.h" #import <AdsameCubeMaxSDK/AdsameCubeMaxSDK.h> @interface welcomeAndAdController ()<AdsameCubeMaxDelegate> { NSInteger timerCount; BOOL hiddenStatusBar; BOOL isAddDataReady; CGRect screenRect; UIInterfaceOrientation lastOrientation; } @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; @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 (isIOS8) { screenRect = CGRectMake(0, 0, 1024, 768); }else { screenRect = CGRectMake(0, 0, 768, 1024); } //隐藏状态栏 if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) { [self prefersStatusBarHidden]; [self setNeedsStatusBarAppearanceUpdate]; }else { [[UIApplication sharedApplication] setStatusBarHidden:hiddenStatusBar withAnimation:UIStatusBarAnimationFade]; } [self.view addSubview:self.welcomeImgView]; if ([[TPUserDefault instance].isFirstIntoApp intValue] == 2) { [self.welcomeImgView addSubview:self.bannerView]; } /** * 加载信息网络请求 */ [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)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } - (void)viewWillLayoutSubviews { [super viewWillLayoutSubviews]; [self setImageAndAdsWithOrientation]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; self.delegate = nil; [self.bannerView removeFromSuperview]; self.bannerView = nil; [self.welcomeTimer invalidate]; self.welcomeTimer = nil; } //- (BOOL)shouldAutorotate { // if (isIOS8) { // return YES; // }else { // return NO; // } //} // //- (NSUInteger)supportedInterfaceOrientations { // UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; // if (orientation == UIInterfaceOrientationPortrait) { // return UIInterfaceOrientationMaskLandscapeLeft; // }else return UIInterfaceOrientationMaskLandscape; //} // //- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { // UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; // if (orientation == UIInterfaceOrientationPortrait) { // return UIInterfaceOrientationLandscapeRight; // }else { // return UIInterfaceOrientationLandscapeLeft|UIInterfaceOrientationLandscapeRight; // } //} /** * 计时器回调 * * @param timer 计时器 */ - (void)waitForData:(NSTimer*)timer { TPLOG(@"%ld",(long)timerCount); timerCount = timerCount -1; if (timerCount == 0) { [timer invalidate]; timer = 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 *updateVersion = self.updateInfo[@"versionName"]; //// if (![updateVersion isEqualToString:[TPUserDefault instance].appVersion]) { //// 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:)]) { //// [self.delegate goOutWelcomePage:self.remoteUserInfo]; //// } //// } // } 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:1024 height:640 delegate:self]; _bannerView.userInteractionEnabled = YES; _bannerView.frame = CGRectMake(0, 0, 1024, 640); // _bannerView.backgroundColor = [UIColor yellowColor]; _bannerView.hidden = YES; } return _bannerView; } - (UIImageView*)welcomeImgView { if (!_welcomeImgView) { _welcomeImgView = [[UIImageView alloc]initWithFrame:self.view.bounds]; _welcomeImgView.userInteractionEnabled = YES; // _welcomeImgView.autoresizingMask = AutoresizingFull; } 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!=========%@",self.description); // isAddDataReady = YES; // [self checkIsitCouldBeAddAd]; // // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // [self setImageAndAdsWithOrientation]; // _bannerView.hidden = NO; // }); } - (void)setImageAndAdsWithOrientation { UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight) { UIImage *frontImage = Image(@"other/ipad_ios7.png"); self.welcomeImgView.image = frontImage; self.welcomeImgView.frame = CGRectMake(0, 0, 1024, 768); if (isAddDataReady) { self.bannerView.transform = CGAffineTransformMakeRotation(0); self.bannerView.frame = CGRectMake(0, 0, 1024, 640); } }else { UIImage *frontImage = Image(@"other/ipad_ios7_p.png"); self.welcomeImgView.image = frontImage; self.welcomeImgView.frame = CGRectMake(0, 0, 768, 1024); if (isAddDataReady) { self.bannerView.transform = CGAffineTransformMakeRotation(M_PI*1.5); self.bannerView.frame = CGRectMake(0, 0, 640, 1024); } } } //-(void)onAdsLoadingFailed { // TPLOG(@"========load AD failed!========%@",self.description); //// 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]; // } //} //【需求】适配新版传漾SDK(bug:5429) -(void)onAdsLoadingFailed:(adsErrorCode)errorCode { TPLOG(@"========load add failed!:%d========",errorCode); if (errorCode == ADSAME_ADSERROR_RESOURCES_LOADED) { TPLOG(@"=======ad data is ready!=========%@",self.description); isAddDataReady = YES; [self checkIsitCouldBeAddAd]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self setImageAndAdsWithOrientation]; _bannerView.hidden = NO; }); }else if(errorCode == ADSAME_ADSERROR_INVALID_ADS || errorCode == ADSAME_ADSERROR_REQUEST_FAILED){ TPLOG(@"========load AD failed!========%@",self.description); // 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 { TPLOG(@"======Click Ads:%@======",clickUrl); [MobClick event:@"12"]; [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"]; [TPUserDefault instance].updateInfo = self.updateInfo; 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{ } - (void)startWaitCursor:(int)actionTag { } - (void)stopWaitCursor:(int)actionTag { } - (void)dealloc { self.displayTime = nil; self.welcomeImgView = nil; self.bannerView = nil; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #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