Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

iAd For Complete Noobs

edited October 2014 in Examples Posts: 47

Sub these three files in for your current, to get iAd on iPhone on Xcode. I'm not sure it works on iPad. So if you're planning for a universal application then this is not the tutorial you're gonna want. (Also before you can run this you need to add the iAd.framework to your frameworks.) Also thanks to reefwing for most of this code.
CodeaViewController.h:

//  CodeaViewController.h

#import <UIKit/UIKit.h>

@protocol CodeaAddon;

typedef enum CodeaViewMode
{
    CodeaViewModeStandard,
    CodeaViewModeFullscreen,
    CodeaViewModeFullscreenNoButtons,
} CodeaViewMode;

@interface CodeaViewController : UIViewController

@property (nonatomic, assign) CodeaViewMode viewMode;
@property (nonatomic, assign) BOOL paused;

- (void) setViewMode:(CodeaViewMode)viewMode animated:(BOOL)animated;

- (void) loadProjectAtPath:(NSString*)path;

- (void) registerAddon:(id<CodeaAddon>)addon;

@end

AppDelegate.h:

//  AppDelegate.h

#import <UIKit/UIKit.h>
#import "IAdsAddOn.h"

@class CodeaViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) CodeaViewController *viewController;

@property (strong, nonatomic) IAdsAddOn *iAdsAddOn;;

@end

-Note: you'll need to paste your project name where it says PROJECTNAME
AppDelegate.mm:

//  AppDelegate.mm

#import "AppDelegate.h"
#import "CodeaViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[CodeaViewController alloc] init];

    //  Create and add our iAdsAddOn to Codea

    self.iAdsAddOn = [[IAdsAddOn alloc] init];
    [self.viewController registerAddon: self.iAdsAddOn];

    NSString* projectPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"PROJECTNAME.codea"];

    [self.viewController loadProjectAtPath:projectPath];

    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
}

- (void)applicationWillTerminate:(UIApplication *)application
{
}

@end

Don't worry when you encounter errors it's because you need to also add these (last) two files to your addons folder. Make sure these files are in Xcode and not just in your project folder.
IAdsAddOn.h:

//  IAdsAddOn.h

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

//#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)

id iAdsAddOnInstance;

@interface IAdsAddOn : NSObject<CodeaAddon, ADBannerViewDelegate>

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

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

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

@implementation IAdsAddOn

#pragma mark - Initialisation

- (id)init
{
    self = [super init];
    if (self)
    {

        iAdsAddOnInstance = self;


        _isBannerVisible = NO;
        _showBannerFromTop = YES;


        CGRect frame = CGRectZero;
        frame.size = [ADBannerView sizeFromBannerContentSizeIdentifier: ADBannerContentSizeIdentifierPortrait];

        _bannerView = [[ADBannerView alloc] initWithFrame: frame];
        _bannerView.requiredContentSizeIdentifiers = [NSSet setWithObject: ADBannerContentSizeIdentifierPortrait];
        _bannerView.delegate = self;
    }
    return self;
}

#pragma mark - CodeaAddon Delegate


- (void) codea:(CodeaViewController*)controller didCreateLuaState:(struct lua_State*)L
{
    NSLog(@"iAdAddOn Registering Functions");


    lua_register(L, "showAdFromTop", showAdFromTop);
    lua_register(L, "showAdFromBottom", showAdFromBottom);
    lua_register(L, "hideAd", hideAd);


    self.currentController = controller;
}

#pragma mark - iAds Add On Functions and associated Methods


- (void)showBannerViewAnimated:(BOOL)animated
{
    if ([self.bannerView isBannerLoaded])
    {

        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;


        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(@"showBannerViewAnimated: Unable to display banner, no Ads loaded.");
}

- (void)hideBannerViewAnimated:(BOOL)animated
{
    if (_isBannerVisible)
    {
        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;
    }
}

static int showAdFromTop(struct lua_State *state)
{
    [iAdsAddOnInstance setShowBannerFromTop: YES];
    [iAdsAddOnInstance showBannerViewAnimated: YES];

    return 0;
}

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

    return 0;
}

static int hideAd(struct lua_State *state)
{
    [iAdsAddOnInstance hideBannerViewAnimated: YES];

    return 0;
}

#pragma mark - iAd Banner View Delegate


- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    NSLog(@"Banner View loaded Ads for display.");
    NSLog(@"Active View Controller: %@", self.currentController.class);


    if (![self.currentController.view.subviews containsObject: _bannerView])
        [self.currentController.view addSubview: _bannerView];

    [self showBannerViewAnimated: YES];
}


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

    [self hideBannerViewAnimated: YES];
}


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

}


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

    return YES;
}


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

@end

I hope this helps you!
if this doesn't make sense then try this website:

http://codeatuts.blogspot.com.au/2013/04/tutorial-29-codea-v152-objective-c-add.html

Tagged:

Comments

  • Posts: 1,976

    What about making it for @Zoyt's Obj-C addon? Last I heard, Reefwing's is a bit outdated and has errors, and Zoyt's has some extra functions.

  • Posts: 47

    I'm not sure I haven't tried it yet. @SkyTheCoder It's interesting how man important discussions go unnoticed by everyone. About a month ago I was looking for a fullscreen iAd tutorial and couldn't find one, but it's been right in front of me this whole time.

Sign In or Register to comment.