PhoneGapで作成したアプリにiAdを追加する

PhoneGap 2.3.0でiAdを表示する方法が気になったので調べてみました。
phonegap_iad_20130115_008

まずプロジェクトの設定のBuild PhasesタブにあるLink Binary With Librariesの+ボタンをクリック。
phonegap_iad_20130115_001

iAd.frameworkを選択し、Addボタンをクリック。
phonegap_iad_20130115_002

MainViewController.mを選択。
phonegap_iad_20130115_003

#import <iAd/iAd.h>

上記を追加し、

@interface MainViewController : CDVViewController

の部分を下記に修正。

@interface MainViewController : CDVViewController <ADBannerViewDelegate> {
ADBannerView *adView;
}

次にMainViewController.hを修正。
webViewDidFinishLoad関数に下記を追加。

adView = [[ADBannerViewalloc] initWithFrame:CGRectZero];
 if([UIApplicationsharedApplication].statusBarOrientation ==
 UIInterfaceOrientationPortrait ||
 [UIApplicationsharedApplication].statusBarOrientation ==
 UIInterfaceOrientationPortraitUpsideDown) {
 adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
 }
 else {
 adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
 }

 adView.delegate = self;

 adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
 CGRect adFrame = adView.frame;
 adFrame.origin.y = self.view.frame.size.height-adView.frame.size.height;
 adView.frame = adFrame;
 [self.viewaddSubview:adView];

また、下記関数を追加。


- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
 adView.hidden = NO;
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
 adView.hidden = YES;
}

didFailToReceiveAdWithError関数を追加しないと、広告の取得が失敗した場合、下記エラーになります。

2013-01-15 19:17:04.399 iAdTest[12728:16103] [AppDeveloper]: ADBannerView: Unhandled error (no delegate or delegate does not implement didFailToReceiveAdWithError:): Error Domain=ADErrorDomain Code=3 "The operation couldn’t be completed. Ad inventory unavailable" UserInfo=0x8e81000 {ADInternalErrorCode=3, ADInternalErrorDomain=ADErrorDomain, NSLocalizedFailureReason=Ad inventory unavailable}
2013-01-15 19:17:04.399 iAdTest[12728:16103] [AppDeveloper]: ADBannerView: Unhandled error (no delegate or delegate does not implement didFailToReceiveAdWithError:): Error Domain=ADErrorDomain Code=0 "The operation couldn’t be completed. Unknown error" UserInfo=0x8e7f080 {ADInternalErrorCode=0, ADInternalErrorDomain=ADErrorDomain, NSLocalizedFailureReason=Unknown error}
2013-01-15 19:17:05.181 iAdTest[12728:16103] [AppDeveloper]: ADBannerView: Unhandled error (no delegate or delegate does not implement didFailToReceiveAdWithError:): Error Domain=ADErrorDomain Code=5 "The operation couldn’t be completed. Banner view is visible but does not have content" UserInfo=0x896da50 {ADInternalErrorCode=5, ADInternalErrorDomain=ADErrorDomain, NSLocalizedFailureReason=Banner view is visible but does not have content}

参考にさせていただいたサイト:
SiteKickr Blog


Similar Posts:

Leave a Reply