IOS中AdMob广告点击后方向旋转

来源:互联网 发布:mac电脑忘记管理员密码 编辑:程序博客网 时间:2024/05/16 08:52

原文地址:http://novacreo.com/移动端开发技术交流/ios中admob广告点击后方向旋转/


近期为Smash Angels的IOS版本集成Admob的时候,发现某些广告(我碰到的是我叫MT,瞬间恨死这游戏了)点击后再切回到游戏时,广告条顺时针旋转了90度,向下面的截图这样:
IMG_0005

因为自己的IOS基础相当薄弱,所以这个问题折腾了快两天,一直没有眉目。也请教了一些网友,也没啥进展,后来硬着头皮去看了UIViewController的官方说明以及网上一些关于UIViewController的文章,最后终于把问题解决了,把解决办法记录以下,方便和我一样不熟悉IOS开发却又要做一些IOS适配工作的童鞋。

最初在集成Admob的时候,参考了官方给出来的例子。项目中用的实际代码如下:

// File NCSAds.h#import <UIKit/UIKit.h>#import "GADBannerView.h"#import "GADBannerViewDelegate.h"@interface NCSAds : UIViewController<GADBannerViewDelegate>{    GADBannerView* bannerAd;}@property(nonatomic, assign, setter=setBannerUnitId:) NSString* _bannerUnitId;+(NCSAds*) sharedInstance;-(void) showBannerAd;-(void) hideBannerAd;@end// File NCSAds.m#import "NCSAds.h"#import <AdBuddiz/AdBuddiz.h>@implementation  NCSAds;@synthesize _bannerUnitId;static NCSAds* _sharedInstance = nil;-(void) viewDidLoad {    [super viewDidLoad];    _sharedInstance = self;    CGSize screenSize = [[UIScreen mainScreen] bounds].size;    [self.view setFrame:CGRectMake(0, 0, screenSize.height, screenSize.width)];    bannerAd = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];    bannerAd.adUnitID = [self _bannerUnitId];    bannerAd.rootViewController = self;    [bannerAd setDelegate:self];    [bannerAd setHidden:YES];    [self.view addSubview:bannerAd];    GADRequest* request = [GADRequest request];    [bannerAd loadRequest:request];    [bannerAd setCenter:CGPointMake(screenSize.height/2, bannerAd.center.y)];}+(NCSAds*) sharedInstance {    if ( _sharedInstance == nil ) {        _sharedInstance = [[[NCSAds alloc] init] autorelease];    }    return _sharedInstance;}-(void) hideBannerAd {    [bannerAd loadRequest:[GADRequest request]];    [bannerAd setHidden:YES];}-(void) showBannerAd {    [bannerAd setHidden:NO];    }- (void)adViewDidReceiveAd:(GADBannerView *)view {    NSLog(@"Received admob banner ad");    [self.view setFrame:CGRectMake(0, 0, [[self view] frame].size.width, [view frame].size.height)];}-(void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error {    [view setHidden:YES];    NSLog(@"Failed to receive ad %@", [error localizedDescription]);}-(void)dealloc {    [bannerAd release];    [super dealloc];}

在AppController.mm中增加初始化、请求广告的代码。

    NCSAds *ads = [[[NCSAds alloc] init] autorelease];    [ads setBannerUnitId:@"a15361f24208fb5"];    [viewController.view addSubview:ads.view];

这样,在适当的时候调用showAd,确实可以将广告显示出来。但是,当碰到打开in-app store的广告的时候,就会发现广告条发生旋转,变成了上面图片给出来的样子。

在网上瞎搜了一通,基本上都是说要添加几个方法,来保证view跟随屏幕的旋转而旋转。但是实际加上后,也没有变化。

// Override to allow orientations other than the default portrait orientation.// This method is deprecated on ios6- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {    return return UIInterfaceOrientationIsLandscape( interfaceOrientation );;}// For ios6, use supportedInterfaceOrientations & shouldAutorotate instead- (NSUInteger) supportedInterfaceOrientations{#ifdef __IPHONE_6_0    return UIInterfaceOrientationMaskLandscape;#endif}- (BOOL) shouldAutorotate {    return YES;}

supportedInterfaceOrientations和shouldAutorotate方法也只是在根视图启动的时候调用了一次,后面点击广告的时候并没有调用。

后来,仔细研究了别人写的UIViewController的例子,发现,自己在使用是并没有将NCSAds的view controller并没有添加到任何view controller或者UIWindow中,只是将其view添加到了根视图的view上。抱着一试的态度,修改了AppController.mm中的代码,把NCSAds的view controller作为子view controller添加到根视图控制器上,再重新编译运行,竟然成功了。

    NCSAds *ads = [[[NCSAds alloc] init] autorelease];    [ads setBannerUnitId:@"a15361f24208fb5"];    [viewController.view addSubview:ads.view];    [viewController addChildViewController:ads];

究其根本原因,是因为:以addSubview的方式将使其view作为另一个View Controller的view的subView,上一级 View Controller并不能对当前View Controller的 生命周期相关的函数进行调用,以及旋转事件的传递等,因此要把该view controller添加到上一级的view controller中,已传递屏幕旋转等事件。


0 0