Hi,
I have been working on an app with codea and now, thanks to all the tutorials and examples of the people on this forum, it is nearly complete!
However, I am stuck with implementing the iAds for my app. I used the tutorial from @reefwing from http://codeatuts.blogspot.com.au/2013/04/tutorial-29-codea-v152-objective-c-add.html but when researching how to make it compatible with landscape and iphones I came across the canDisplayBannerAds=YES; function and managed to get the iads working by adding
self.currentController.canDisplayBannerAds=YES;
in the
- (void) codea:(CodeaViewController*)controller didCreateLuaState:(struct lua_State*)L
method. But now I am trying to add the option to remove ads through an in-app purchase but cannot wrap my head around how to remove it using the showAdFromBottom() (or top, it does not matter for my app) and hideAd() functions. I tried to get it working earlier today (as well as remove the duplicate code from the tutorial since I replaced it with canDisplayBannerAds=YES) but ended up making it so the ads don’t work at all . here is the source code for iAdsAddon.m:
//
// IAdsAddon.m
// AcornMayhem
//
//
// 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;//YES;
// Initialise our iAd Banner View
CGRect frame = CGRectZero;
//frame.size = [ADBannerView sizeFromBannerContentSizeIdentifier: ADBannerContentSizeIdentifierPortrait];
[_bannerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; //replacement for above depricated
//[_bannerView setAutoresizesSubviews:YES];// remove if not working //does not work
//[_bannerView setAutoresizingMask: // does not work
// UIViewAutoresizingFlexibleWidth | // does not work
// UIViewAutoresizingFlexibleTopMargin]; //alternate for next line
//UIViewAutoresizingFlexibleHeight]; // does not work
_bannerView = [[ADBannerView alloc] initWithFrame: frame];
//_bannerView.requiredContentSizeIdentifiers = [NSSet setWithObject: ADBannerContentSizeIdentifierPortrait];
_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;
if(_showBannerFromTop==YES){
self.currentController.canDisplayBannerAds=YES;//this one line replaces the rest of the file ///////////////////
}else{
self.currentController.canDisplayBannerAds=NO;
}
}
#pragma mark - iAds Add On Functions and associated Methods
// Objective C Methods
/*- (void)showBannerViewAnimated:(BOOL)animated
{
if ([self.bannerView isBannerLoaded])
{
// 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(@"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;
}
}*/
// 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)
{
[iAdsAddOnInstance setShowBannerFromTop: YES];
//[iAdsAddOnInstance showBannerViewAnimated: YES];
return 0;
}
static int showAdFromBottom(struct lua_State *state)
{
[iAdsAddOnInstance setShowBannerFromTop: YES];
//[iAdsAddOnInstance showBannerViewAnimated: YES];
return 0;
}
static int hideAd(struct lua_State *state)
{
//[iAdsAddOnInstance hideBannerViewAnimated: YES];
//self.currentController.canDisplayBannerAds=NO;
[iAdsAddOnInstance setShowBannerFromTop: NO];
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(_showBannerFromTop==YES){
// if (![self.currentController.view.subviews containsObject: _bannerView])
// [self.currentController.view addSubview: _bannerView];
self.currentController.canDisplayBannerAds=YES;
//[self showBannerViewAnimated: YES];
}else{
self.currentController.canDisplayBannerAds=NO;
}
}
// 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.currentController.canDisplayBannerAds=NO;
//[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
{
}
//candisplaybannerads clicked
// 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
how can I fix this?
thanks,
~patrick