How to get iAds Working in PhoneGap

crickettachyphagiaMobile - Wireless

Dec 10, 2013 (3 years and 10 months ago)

372 views

1
Copyright © 2011 Amersham Equestrian Ltd
How to get iAds Working in PhoneGap
Pre-requisites:
Xcode 4.2 or higher
Phonegap 1.2 or higher
How to
Create a new Phonegap project in XCode called “iAdDemo”. Follow all the normal steps (see the
PhoneGap ReadMe) to get a complete project including dragging “www” into the project:
Pull the latest source from Github “phonegap-plugins
/ iPhone
/ AdPlugin”. Extract the files as
follows:
SAiOSAdPlugin.m & SAiOSAdPlugin.h to <drive>:\iAdDemo\iAdDemo\Plugins.
SAiOSAdPlugin.js and index.html to <drive>:\iAdDemo\www.
2
Copyright © 2011 Amersham Equestrian Ltd
From Finder, drag SAiOSAdPlugin.m & SAiOSAdPlugin.h to the plugins group in Xcode:
Open PhoneGap.plist in “Supporting Files” and add an entry to Plugins of “SAiOSAdPlugin” with a
value of “SAiOSAdPlugin”:
3
Copyright © 2011 Amersham Equestrian Ltd
While in PhoneGap.plist add “*” as Item 0 of ExternalHosts – this allows access to the iAd servers.
You can tighten this up later if necessary:
Edit the index.html and make sure it references the correct version of phonegap.js:
<script type="text/javascript" charset="utf-8" src="phonegap-1.2.0.js"></script>
Modify the first line of the function “iAdBannerViewDidFailToReceiveAdWithErrorEventHandler(evt)”
From:
alert(evt.error);
To:
console.log(evt.error);
Add the following line at the start of function “iAdBannerViewDidLoadAdEventHandler(evt)”
console.log('iAd Received.');
This makes debugging and monitoring easier.
4
Copyright © 2011 Amersham Equestrian Ltd
Add the iAd Framework to the project. To do this, select the top level “iAdDemo”. Then select
“iAdDemo” from “Targets” and then click the “Build Phases” tab:
Click the “+” the bottom of the “Link Binaries with Libraries” panel and find the “iAd Framework”:
Click “Add”.
5
Copyright © 2011 Amersham Equestrian Ltd
Within iTunes Connect make sure you are signed up for iAds. (I can’t show the steps as my account is
already done!) – just follow the instructions within iTunes Connect.
Now try running the app in an iOS 5 simulator:
If you click on the test iAd you should get:
6
Copyright © 2011 Amersham Equestrian Ltd
So far so good. But now try running it in the iOS4.3 simulator.
If you look in the console log, you will now see warnings:
2011-12-10 13:54:08.004 iAdDemo[4207:207] ADBannerView:
ADBannerContentSizeIdentifier480x32 is deprecated, please use
ADBannerContentSizeIdentifierLandscape instead
2011-12-10 13:54:08.005 iAdDemo[4207:207] ADBannerView:
ADBannerContentSizeIdentifier320x50 is deprecated, please use
ADBannerContentSizeIdentifierPortrait instead
2011-12-10 13:54:08.006 iAdDemo[4207:207] ADBannerView:
ADBannerContentSizeIdentifier320x50 is deprecated, please use
ADBannerContentSizeIdentifierPortrait instead
The source in the iAd plugin says to use the deprecated values:
“// we are still using these constants even though they are deprecated -
if it is changed, iOS 4 devices < 4.3 will crash.”
Personally I have therefore chosen to ignore this build warning, particularly as with the iOS5 SDK the
warning does not appear – maybe Apple changed their mind? It certainly doesn’t seem to cause a
problem.
You may also see (in any iOS version):
2011-12-10 13:55:12.782 iAdDemo[4207:207] [INFO] Error Domain=ADErrorDomain
Code=3 "The operation couldn’t be completed. Ad inventory unavailable"
UserInfo=0x5a64eb0 {ADInternalErrorCode=3, NSLocalizedFailureReason=Ad
inventory unavailable}
As far as I can tell this can happen in test and live and indicates that no suitable iAd is available right
now (The iAd can’t be “fulfilled”.) This is why we changed the “alert” to a “console.log” earlier. If
you wait long enough an iAd will usually appear:
2011-12-10 13:55:12.782 iAdDemo[4207:207] [INFO] Error Domain=ADErrorDomain
Code=3 "The operation couldn’t be completed. Ad inventory unavailable"
UserInfo=0x5a64eb0 {ADInternalErrorCode=3, NSLocalizedFailureReason=Ad
inventory unavailable}
2011-12-10 13:55:44.459 iAdDemo[4207:207] [INFO] iAd Received.
2011-12-10 13:56:47.532 iAdDemo[4207:207] [INFO] iAd Received.
You may see other errors, but again as far as I can tell this is the iAd network’s normal operation and
you can’t be 100% certain of receiving an iAd at all times. Your live app needs to handle this
situation gracefully. In my apps I either display my own “ad” usually for the full version of the app or
go to another ad provided such as Admob (A whole new learning process!!).
If no iAd ever appears – check you are properly signed up in iTunes Connect. There may be a delay
between sign-up and being able to access test iAds in your app, so keep trying.
Back to iOS4.3 (and lower) – when an iAd is displayed try clicking on it – almost certainly nothing will
happen indicating that there is a problem. Thanks go to Davide who provided the solution.
7
Copyright © 2011 Amersham Equestrian Ltd
Open the file SAiOSAdPlugin.m. Locate the function “__prepare”. After the line:
self.adView.delegate = self;
add:
//Suggested fix for pre iOS5
self.adView.backgroundColor = [UIColor whiteColor];
Obvious – not!!
Re-build and run and now clicking on the iAd should work:
8
Copyright © 2011 Amersham Equestrian Ltd
In earlier versions of PhoneGap there also seemed to be a problem with orientation, although this
did not occur when I put this demo together with v1.2. If when you execute the app the iAd is in
landscape rather portrait then modify SAiOSAdPlugin.m and change the default orientation selection
as follows in the “prepare” function:
- (void) prepare:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
NSUInteger argc = [arguments count];
if (argc > 1) {
return;
}
self.isLandscape = NO;
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
switch (orientation) {
case UIDeviceOrientationPortrait:
case UIDeviceOrientationPortraitUpsideDown:
self.isLandscape = NO;
break;
case UIDeviceOrientationLandscapeLeft:
case UIDeviceOrientationLandscapeRight:
self.isLandscape = YES;
break;
default:
self.isLandscape = NO;
break;
}
NSString* atBottomValue = [arguments objectAtIndex:0];
[self __prepare:[atBottomValue boolValue]];
}
NW
10-Dec-2011