@Simeon My app got rejected because of the camera access bug in 2.0, so I’m trying to figure out how to get the iAds addon to work with 2.1. However, I’m having trouble figuring out how to use the iAds add on with the new runtime. It worked great with 2.0 version, but I can’t figure out how to use it with the way the new 2.1 runtime works.
I tried changing the appdelegate.h by adding
#import "IAdsAddOn.h"
@property (strong, nonatomic) IAdsAddOn *iAdsAddOn;
Then in appdelegate.mm adding
self.iAdsAddOn = [[IAdsAddOn alloc] init];
[self.viewController registerAddon: self.iAdsAddOn];
That is about all that had to be done in 2.0 to get the add-on to work, but i’m getting linking errors in Xcode now with the 2.1 runtime.
Below is the actual iAds add-on code:
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