iAds Landscape trouble..

Hi all!

I am having trouble with iAds in landscape mode.

The code works perfect for iAds in portrait mode, but when I use the same code for an app that is in landscape mode, the iAds will not show up.

Here is the code that I am using:

IadsAddOn.h


/
//  IAdsAddOn.h
//  30 Balls
//
//  Created by Théo on 6/19/14.
//  Copyright (c) 2014 MyCompany. All rights reserved.
//

#import "CodeaAddon.h"
#import <iAd/iAd.h>
#import <Foundation/Foundation.h>

// Device Detection Macros - can be used to position the banner advertisement based on device.
// Not used in this tutorial.

//#define IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
//#define IS_IPHONE (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
//#define IS_IPHONE_5 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 568.0f)
//#define IS_IPHONE_4 (IS_IPHONE && [[UIScreen mainScreen] bounds].size.height == 480.0f)

//  Create a variable which points to the instance of this class so that we can access self
//  from within the Lua c functions.
////
id iAdsAddOnInstance;

//  This class conforms to the CodeaAddon & ADBannerViewDelegate Protocols

@interface IAdsAddOn : NSObject<CodeaAddon, ADBannerViewDelegate>

@property BOOL isBannerVisible;
@property BOOL showBannerFromTop;
@property BOOL adsAllowed;
@property (strong, nonatomic) ADBannerView *bannerView;
@property (weak, nonatomic) CodeaViewController *currentController;

//  Forward declare our Lua iAd functions. These are static to confine their scope
//  to this file. By default c functions are global.

static int showAdFromTop(struct lua_State *state);
static int showAdFromBottom(struct lua_State *state);
static int hideAd(struct lua_State *state);

@end

IadsAddOn.m


//
//  IAdsAddOn.m
//  30 Balls
//
//  Created by Théo on 6/19/14.
//  Copyright (c) 2014 MyCompany. All rights reserved.
//

#import "lua.h"
#import "IAdsAddOn.h"

@implementation IAdsAddOn

#pragma mark - Initialisation

- (id)init
{
    self = [super init];
    if (self)
    {
        
        
        //  audioAddOnInstance allows us to access self from within the c functions.
        
        iAdsAddOnInstance = self;
        
        // Initialise our Instance Variables
        
        _isBannerVisible = NO;
        _showBannerFromTop = YES;
        _adsAllowed = NO;
        
        //  Initialise our iAd Banner View
        
        CGRect frame = CGRectZero;
        //frame.size = [ADBannerView sizeFromBannerContentSizeIdentifier: ADBannerContentSizeIdentifierPortrait];
        
        _bannerView = [[ADBannerView alloc] initWithFrame: frame];
        //_bannerView.requiredContentSizeIdentifiers = [NSSet setWithObject: ADBannerContentSizeIdentifierPortrait];
        [_bannerView setAutoresizingMask: UIViewAutoresizingFlexibleWidth];
        _bannerView.delegate = self;
    }
    return self;
}

#pragma mark - CodeaAddon Delegate

//  Classes which comply with the <CodeaAddon> Protocol must implement this method

- (void) codea:(CodeaViewController*)controller didCreateLuaState:(struct lua_State*)L
{
    NSLog(@"iAdAddOn Registering Functions");
    
    //  Register the iAd functions, defined below
    
    lua_register(L, "showAdFromTop", showAdFromTop);
    lua_register(L, "showAdFromBottom", showAdFromBottom);
    lua_register(L, "hideAd", hideAd);
    
    //  Hook up with the CodeaViewController - don't try to add subviews in this method.
    
    self.currentController = controller;
}

#pragma mark - iAds Add On Functions and associated Methods

//  Objective C Methods

- (void)showBannerViewAnimated:(BOOL)animated
{
    if ([self.bannerView isBannerLoaded])
    {
        if (_adsAllowed) {
            //  We only display the banner View if it has ads loaded and isn't already visible.
            //  Set the banner view starting position as off screen.
            
            CGRect frame = _bannerView.frame;
            
            if (_showBannerFromTop)
                frame.origin.y = 0.0f - _bannerView.frame.size.height;
            else
                frame.origin.y = CGRectGetMaxY(self.currentController.view.bounds);
            
            _bannerView.frame = frame;
            
            // Set banner View final position to animate to.
            
            if (_showBannerFromTop)
                frame.origin.y = 0;
            else
                frame.origin.y -= frame.size.height;
            
            if (animated)
                [UIView animateWithDuration: 0.5 animations: ^{self.bannerView.frame = frame;}];
            else
                self.bannerView.frame = frame;
            
            _isBannerVisible = YES;
        }
        else
        {
           NSLog(@"Ads should not be shown right now");
            [self hideBannerViewAnimated: NO];
            
        
            
            
        }
    }
    else
        NSLog(@"showBannerViewAnimated: Unable to display banner, no Ads loaded.");
}

- (void)hideBannerViewAnimated:(BOOL)animated
{
    if (_isBannerVisible || !_adsAllowed)
    {
        CGRect frame = self.bannerView.frame;
        
        if (_showBannerFromTop)
            frame.origin.y -= frame.size.height;
        else
            frame.origin.y = CGRectGetMaxY(self.currentController.view.bounds);
        
        if (animated)
            [UIView animateWithDuration: 0.5 animations: ^{self.bannerView.frame = frame;}];
        else
            self.bannerView.frame = frame;
        
        _isBannerVisible = NO;
    }
}

//  C Functions
//
//  Note that the returned value from all exported Lua functions is how many values that function should return in Lua.
//  For example, if you return 0 from that function, you are telling Lua that function returns 0 values.
//  If you return 2, you are telling Lua to expect 2 values on the stack when the function returns.
//
//  To actually return values, you need to push them onto the Lua stack and then return the number of values you pushed on.

static int showAdFromTop(struct lua_State *state)
{
    
    NSLog(@"Show Ad From Top");
    
    [iAdsAddOnInstance setAdsAllowed: YES];
    [iAdsAddOnInstance setShowBannerFromTop: YES];
    [iAdsAddOnInstance showBannerViewAnimated: YES];
    
    return 0;
}

static int showAdFromBottom(struct lua_State *state)
{
    [iAdsAddOnInstance setAdsAllowed: YES];
    [iAdsAddOnInstance setShowBannerFromTop: NO];
    [iAdsAddOnInstance showBannerViewAnimated: YES];
    
    return 0;
}

static int hideAd(struct lua_State *state)
{
    [iAdsAddOnInstance setAdsAllowed: NO];
    [iAdsAddOnInstance hideBannerViewAnimated: YES];
    
    return 0;
}

#pragma mark - iAd Banner View Delegate

//  Your application implements this method to be notified when a new advertisement is ready for display.

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    NSLog(@"Banner View loaded Ads for display.");
    NSLog(@"Active View Controller: %@", self.currentController.class);
    
    //  Add our banner view to the CodeaViewController view, if we haven't already.
    
    if (![self.currentController.view.subviews containsObject: _bannerView])
        [self.currentController.view addSubview: _bannerView];
    
    [self showBannerViewAnimated: YES];
}

//  This method is triggered when an advertisement could not be loaded from the iAds system
//  (perhaps due to a network connectivity issue).

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    NSLog(@"bannerview failed to receive iAd error: %@", [error localizedDescription]);
    
    [self hideBannerViewAnimated: YES];
}

//  This method is triggered when the banner confirms that an advertisement is available but before the ad is
//  downloaded to the device and is ready for presentation to the user.

- (void)bannerViewWillLoadAd:(ADBannerView *)banner
{
    
}

//  This method is triggered when the user touches the iAds banner in your application. If the willLeave argument
//  passed through to the method is YES then your application will be placed into the background while the user is
//  taken elsewhere to interact with or view the ad. If the argument is NO then the ad will be superimposed over your
//  running application.

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
    self.currentController.paused = YES;
    NSLog(@"Ad being displayed - Codea paused.");
    
    return YES;
}

//  This method is called when the ad view removes the ad content currently obscuring the application interface.
//  If the application was paused during the ad view session this method can be used to resume activity.

- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
    self.currentController.paused = NO;
    NSLog(@"Ad dismissed - Codea running.");
}

@end


Does anyone familiar with Objective C see what is not allowing showAdFromTop() to show the add in landscape mode?

Any help would be greatly appreciated!

Have you tried looking at the view hierarchy with the new tool in Xcode 6? This will help you see where the view is.

Sadly I’m running a hackintosh with Mavericks so only have XCode 5.

  1. Mavericks will run xcode 6, but with xcode 6.1 out I’m not sure you can still download xcode 6.
  2. I have a hackintosh, and the upgrade to Yosemite worked just fine. YMMV

For whatever reason, the iAds showed up in landscape now when I randomly tried the testflight demo of the app today. The only problem is the ads are showing on the bottom left of the screen, I need to figure out how to center them on the screen.

Any Objective C experts know how to do this? This is the last thing I need before I have this figured out…

Implementing iAds is driving me crazy…