Loading...
IPhone Developer

Sunday, December 19, 2010

iAd in Cocos2d

Apple app store allows the developers to be compensated for their apps in multiple ways. The most common method is to put a price tag on your app. The other method which is introduced recently is through displaying advertisements in the app.

Start:-
Framework :-
The first step in getting started with iAds is to reference the iAds framework. Right click on the Frameworks folder and select "Add Existing Frameworks". Then select iAd.Framework and add it to the application. Take a look at the screenshot below to get a clear picture:






















Implementation:-

in Cocos2d 0.99.5(latest version) defaultly view controller inherits with the rootview controller.
just follow the below code.

Example:-
Rootviewcontroller.h








Rootviewcontroller.m



















Helloworldscene.h










Helloworldscene.m












AppDelegate.h









AppDelegate.m





















now the ads is displaying in the bottom of the screen.

Final output like this:-
Sample output:-(Screenshot)


You can place ad anywhere but Apple recommends to choose either the top part of the view or the bottom part of the view. This placement will make sure that the ads are not interfering with the actual application. Also, note that these are just test ads which makes sure that the ads are running smoothly in your application. When the application is live in the app store then Apple will send relevant ads to your app.

The iAd network must be activated before submitted the app for approval. If the iAd network is not activated then ads will not be displayed. You can activate the iAd network for your particular app by logging into iTunes connect and managing the desired application. You do not need to activate the iAds network when only running test ads.

sometimes you will see the following error:


didFailToReceiveAdWithError
The operation couldn't be completed. banner view is visible but does not have content.

Do not worry to much about this error since this simply means that your application is having trouble communicating with the Apple iAd server. Most of the time the problem is on Apple's end and the only thing you can do is wait and try again at later time.

"  GIL " is posting the full code for this iad with modified version .  thanks to GIL  posted the code in my name.  



 

9 comments:

Anonymous said...

hi nice tutorial.

keep blogging

Joel Wanner said...

Please help me!
I did everything like you, but when I build it, I get the following warning and the app crashes:
.../TestAppDelegate.m:34: warning: 'EAGLView' may not respond to '+viewWithFrame:pixelFormat:depthFormat:preserveBackbuffer:'
Why is that? Thanks in advance!

Joel Wanner said...

Now I got it to run, but I only see a black screen.

Alexander said...

thx for this! helped me a lot!

Maya said...

HI Nice tutorial ,

Application was running nice but after clicking to iAD and back to running application at that time
i got one thread and the app crashes :


2011-06-23 11:52:55.637 iADtest[4974:207] bannerViewActionShouldBegin called
2011-06-23 11:53:00.804 iADtest[4974:207] bannerViewActionDidfinish called
2011-06-23 11:53:00.805 iADtest[4974:207] -[UIApplication setStatusBarOrientaion:]: unrecognized selector sent to instance 0x5d01860
2011-06-23 11:53:00.807 iADtest[4974:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIApplication setStatusBarOrientaion:]: unrecognized selector sent to instance 0x5d01860'
*** Call stack at first throw:
(
0 CoreFoundation 0x014775a9 __exceptionPreprocess + 185
1 libobjc.A.dylib 0x015cb313 objc_exception_throw + 44
2 CoreFoundation 0x014790bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
3 CoreFoundation 0x013e8966 ___forwarding___ + 966
4 CoreFoundation 0x013e8522 _CF_forwarding_prep_0 + 50
5 iADtest 0x000bc20c -[HelloWorldLayer bannerViewActionDidFinish:] + 172
6 CoreFoundation 0x013e7c7d __invoking___ + 29
7 CoreFoundation 0x013e7b51 -[NSInvocation invoke] + 145
8 CoreFoundation 0x013e8a04 ___forwarding___ + 1124
9 CoreFoundation 0x013e8522 _CF_forwarding_prep_0 + 50
10 iAd 0x0132b9f9 -[ADDistributedMessagingCenter messagePort:receivedMessage:withData:] + 251
11 iAd 0x0132c012 ADMessagePortCallBack + 75
12 CoreFoundation 0x0142ef4c __CFMessagePortPerform + 396
13 CoreFoundation 0x01458944 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 52
14 CoreFoundation 0x013b8cf7 __CFRunLoopDoSource1 + 215
15 CoreFoundation 0x013b5f83 __CFRunLoopRun + 979
16 CoreFoundation 0x013b5840 CFRunLoopRunSpecific + 208
17 CoreFoundation 0x013b5761 CFRunLoopRunInMode + 97
18 GraphicsServices 0x01f911c4 GSEventRunModal + 217
19 GraphicsServices 0x01f91289 GSEventRun + 115
20 UIKit 0x00698c93 UIApplicationMain + 1160
21 iADtest 0x000babcf main + 127
22 iADtest 0x00002145 start + 53
23 ??? 0x00000001 0x0 + 1
)
terminate called after throwing an instance of 'NSException'

Help me !!!!!

Srinivas said...

@maya,

check you coding
specifically this line

-[UIApplication setStatusBarOrientaion:]:

dogwalker said...

Hey, I'm just now looking at implementing iads, and I have to say, you have the best, most complete, and friendliest information on this subject.

EXCELLENT job!

Abhimanyu Singh said...

Hi Srinivas, I had spent an entire day trying to make this iAd thing work exactly the way I wanted. I finally ended up using your code. It integrated without a single glitch. Just have to say one word. Awesome!! I wish hadn't spent my entire day (since 10:00 AM to 4:00 AM) trying out the other top results on Google. Thanks a lot man, now I can go sleep peacefully. Btw, though I don't understand Tamil, I liked the songs being played while programming. :)

krish k said...

Using this code I am getting only black screen