New Tutorial (29) - Codea v1.5.2: Objective C Add On, iAds

If you are interested in adding iAds to your exported projected then have a look at our latest tutorial at: http://codeatuts.blogspot.com.au/2013/04/tutorial-29-codea-v152-objective-c-add.html

Feel free to use in your own projects. Comments welcome.

@Reefwing, do you have a tutorial for iAPs?

Not yet…

I’ll have a crack next weekend.

Awesome

@Reefwing I added iAds using the above tutorial and everything worked great until just recently I started getting an error where if I click on an ad in game, when I return to the game it causes an error and stops working. Do you know what’s going on?

Sounds good. Still waiting for your in-app purchase tutorial :slight_smile: haha

@zapaper - yep. Just finished my Collision Detection tute. iAP is next. Everything takes longer than you think…

@JakAttak - are you pausing Codea when the ad is opened and then restarting it when the ad closes? You need to implement the ADBannerViewDelegate Protocol. This has two call back methods which tell you when an ad is opening or closing.

//  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.codeaViewController.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.codeaViewController.paused = NO;
    NSLog(@"Ad dismissed - Codea running.");
}

@Reefwing, is it supposed to pause and then restart? Because that’s not what I want to happen. It should resume from where it was.

@JakAttak - sorry I meant pause and resume. Poor choice of words on my part.

I’ve gone back and checked the two apps that I have exported from Codea with iAds and they work ok when the ad is tapped and closed. However, I’m only showing ads on the menu screen so nothing particularly complex is going on. What error are you getting?

I use a finite state machine that saves the current “scene” as a variable screen and then do screen:draw(), screen:touched(touch), etc. after exiting an ad it says something like “trying to index nil value screen”

Sounds like your screen variable is being deleted or released somehow. I’m not sure what happens under the covers when the codeaViewController is paused - @Simeon may have some thoughts.

As a work around can you save the screen variables locally when an ad appears and then check if screen is nil and recreate it when the ad is dismissed?

How do you tell that an ad has been clicked?

That’s when the method - (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave - gets called.

But that’s not a lua function is it? Do I need to add it?

hmm - thinking about this a bit more, I don’t know a way to call a lua function running in the Codea sandbox from Objective C. I’m sure there is a way, I just don’t know how to do it. The add-on functionality allows you to go the other way (call an Objective C method from Lua).

What you need is a register Lua call back method added to the CodeaViewController.

Alternately, if 2 new functions were added to Codea like viewControllerWillPause and viewControllerResumeFromPause, that would do the trick. However I suspect that isn’t likely.

@Reefwing, after doing some more testing I realized that this doesn’t happen in the simulator. So I’m wondering if maybe it’s just my device… But I’m not sure

That’s interesting, I only tested on the Simulator. Let me see if the same thing happens for me.

@JakAttak - I just checked the tute code on my iPad. It works ok.

Alright. Well, I’ll test my app on another device. What really matters is that it works for everyone else

I am trying to add @Reefwing 's ads to my codea project in Xcode, but am experiencing some linking problems. I am very new to this, so probably an error on my part. I am trying to do it without the audio addOn.

I modified the AppDelegate.h and AppDelegate.mm as described in the tutorial.
I created a new file IAdsAddOn.h. From the tutorial it was not completely clear how or where to do this. The IAdsAddOn.h file is located just below the CodeaAddon.h (does it matter the order of the files in the Addons folder?)

The build reports the following errors…

Undefined symbols for architecture i386:
OBJC_CLASS$_IAdsAddOn”, referenced from:
objc-class-ref in AppDelegate.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I have checked numerous times the files are the same as in the tutorial (especially the use of capital I or small i).

Anyone have an idea how to fix this? I am stuck!