2

Im trying to get iAd implemented to a iOS 7 game I made with cocos2d, I added iad framework in xcode and have the setup below

in @implementation I have set

   ADBannerView *_bannerView;

then

-(id)init
{
    if( (self= [super init]) )
    {
        // On iOS 6 ADBannerView introduces a new initializer, use it when available.
        if ([ADBannerView instancesRespondToSelector:@selector(initWithAdType:)]) {
            ADBannerView *_adView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];

        } else {
            _adView = [[ADBannerView alloc] init];
        }
        _adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
        _adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
        [[[CCDirector sharedDirector]view]addSubview:_adView];
        [_adView setBackgroundColor:[UIColor clearColor]];
        [[[CCDirector sharedDirector]view]addSubview:_adView];
        _adView.delegate = self;
    }
    [self layoutAnimated:YES];
    return self;
}


- (void)layoutAnimated:(BOOL)animated
{
    // As of iOS 6.0, the banner will automatically resize itself based on its width.
    // To support iOS 5.0 however, we continue to set the currentContentSizeIdentifier appropriately.
    CGRect contentFrame = [CCDirector sharedDirector].view.bounds;
    if (contentFrame.size.width < contentFrame.size.height) {
        //_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
        [adView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
    } else {
        _bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
    }

    CGRect bannerFrame = _bannerView.frame;
    if (_bannerView.bannerLoaded) {
        contentFrame.size.height -= _bannerView.frame.size.height;
        bannerFrame.origin.y = contentFrame.size.height;
    } else {
        bannerFrame.origin.y = contentFrame.size.height;
    }

    [UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
        _bannerView.frame = bannerFrame;
    }];
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
    [self layoutAnimated:YES];
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
    [self layoutAnimated:YES];
}

I get an error about '_adview' and as well as 'currentContentSizeIdentifier' deprecated, can anyone help me get this working correctly? Ive checked online all day and couldnt get any of the code working.

Thanks in advance!

1 Answers1

1

Try this New Code:

- (void)createAdBannerView
{
    _adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero];
    _adBannerView.delegate = self;
   [_adBannerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];

    CGRect frame = _adBannerView.frame;
    frame.origin.y = -frame.size.height;
    frame.origin.x = 0.0f;

    _adBannerView.frame = frame;

    AppDelegate * app = (((AppDelegate*) [UIApplication sharedApplication].delegate));
    [app.navController.view addSubview:_adBannerView];
}

Here new iAd Code for Cocos2d 3.0.

enter image description here

Guru
  • 21,652
  • 10
  • 63
  • 102
  • I get 'ADBannerContentSizeIdentifierPortrait' is deprecated. as well for 'currentContentSizeIdentifier', do you know the ios 7 replacement code for those? – Anthony Orias Feb 18 '14 at 16:00
  • where I used ADBannerContentSizeIdentifierPortrait ? remove currentContentSizeIdentifier, and use setAutoresizingMask. – Guru Feb 18 '14 at 16:07
  • 1
    Thank you so much! Worked like a charm :) Thank you for taking time to help me out with this, I've been struggling with this the past few days lol – Anthony Orias Feb 18 '14 at 16:35
  • I get a error when I try to run the downloaded project in the simulator, 'Thread1: signal SIGABRT' saying cannot be nil – Anthony Orias Feb 20 '14 at 17:05
  • reset simulator and restart Xcode then run…if still crashed then restart your system then try…I guess in Xcode issue…here working fine. – Guru Feb 20 '14 at 17:10
  • Ok started working perfectly thanks Guru! :) using this method can I use Interstitial ads as well? – Anthony Orias Feb 23 '14 at 14:25
  • Actually I couldn't get this to work without changing 1 line: _adBannerView = [[ADBannerView alloc] initWithFrame:CGRectZero]; to: _adBannerView = [[ADBannerView alloc] initWithFrame:CGRectMake(0.0,0.0,navController.view.frame.size.width,0.0)]; – PKCLsoft Sep 23 '14 at 10:07
  • @PKCLsoft I guess in landscape mode right? In portrait it works with CGRectZero. – Guru Sep 23 '14 at 10:55
  • 1
    Correct, sorry I should have said that. In landscape it places a 320px banner at the top center if I don't specify the width of the screen. – PKCLsoft Sep 23 '14 at 11:16