cocos2dx3.0自带的plugin包含推广,收益等各个方面的第三方插件,但是对iAd没有支持,大概是因为专属于IOS,没有单独成库的必要,不过为了统一使用广告的插件化管理,封装一个专属IOS的IAD插件还是有必要的,搞了一天,在这里做个记录,有兴趣的朋友可以参考一下,不完善的地方请指出:
1:如何创建XCODE库就不说了,创建在其他广告库的同级目录,文件结构也是一样,一共就三个文件:
.pch
AdsApple.h
AdsApple.m
2:要加入到cocos2dx的PluginManager统一管理,那么我们这个新的类需要继承InterfaceAds,并且链接iAd.framework.
3:快1点了,直接上代码,完整注释:
头文件:
// // AdsApple // AdsApple // // Created by kevin on 14-5-2. // Copyright (c) 2014年 kevin. All rights reserved. // #import <Foundation/Foundation.h> #import "iAd/iAd.h" #import "InterfaceAds.h" // 广告类型 typedef enum { kTypeBanner = 1, // 广告栏 kTypeFullScreen, // 全屏 } AppleType; @interface AdsApple : NSObject <InterfaceAds, ADBannerViewDelegate> { } @property BOOL debug; @property bool bannerVisible; @property int bannerPos; @property (assign, nonatomic) ADBannerView* bannerView; // 设置开发者信息 - (void) configDeveloperInfo: (NSMutableDictionary*) devInfo; // 显示广告 - (void) showAds: (NSMutableDictionary*) info position:(int) pos; // 隐藏广告 - (void) hideAds: (NSMutableDictionary*) info; // 位置获取 - (void) queryPoints; // - (void) spendPoints: (int) points; // 开关调试模式 - (void) setDebugMode: (BOOL) isDebugMode; // 获取SDK版本 - (NSString*) getSDKVersion; // 获取插件版本 - (NSString*) getPluginVersion; @end
实现:
// // AdsApple // AdsApple // // Created by kevin on 14-5-2. // Copyright (c) 2014年 kevin. All rights reserved. // #import "AdsApple.h" #import "AdsWrapper.h" #define OUTPUT_LOG(...) if (self.debug) NSLog(__VA_ARGS__); #define OUT_POS CGPointMake(-1024, -1024) @implementation AdsApple @synthesize debug = __debug; // 初始化 - (id)init { self = [super init]; if (self) { } return self; } // 释放 - (void)dealloc { if( self.bannerView != nil ) { [self.bannerView removeFromSuperview]; [self.bannerView release]; self.bannerView = nil; } [super dealloc]; } #pragma mark InterfaceAds impl // 设置开发者信息 - (void) configDeveloperInfo: (NSMutableDictionary*) devInfo { } // 显示广告 - (void) showAds: (NSMutableDictionary*) info position:(int) pos { NSString* strType = [info objectForKey:@"AppleType"]; int type = [strType intValue]; switch (type) { case kTypeBanner: { [self showBanner:pos]; break; } case kTypeFullScreen: OUTPUT_LOG(@"Now not support full screen view in AppleType"); break; default: OUTPUT_LOG(@"The value of 'AppleType' is wrong (should be 1 or 2)"); break; } } - (void) hideAds: (NSMutableDictionary*) info { NSString* strType = [info objectForKey:@"AppleType"]; int type = [strType intValue]; switch (type) { case kTypeBanner: { if (nil != self.bannerView) { [self.bannerView removeFromSuperview]; [self.bannerView release]; self.bannerView = nil; } break; } case kTypeFullScreen: OUTPUT_LOG(@"Now not support full screen view in AppleType"); break; default: OUTPUT_LOG(@"The value of 'AppleType' is wrong (should be 1 or 2)"); break; } } - (void) queryPoints { OUTPUT_LOG(@"AdsApple not support query points!"); } - (void) spendPoints: (int) points { OUTPUT_LOG(@"AdsApple not support spend points!"); } - (void) setDebugMode: (BOOL) isDebugMode { self.debug = isDebugMode; } - (NSString*) getSDKVersion { return @"6.4.2"; } - (NSString*) getPluginVersion { return @"0.2.0"; } // 显示广告栏 - (void) showBanner: (int) pos { // 如果存在先删除,重新创建 if (nil != self.bannerView) { [self.bannerView removeFromSuperview]; [self.bannerView release]; self.bannerView = nil; } // 创建 self.bannerView = [[ADBannerView alloc] initWithFrame:CGRectZero]; self.bannerView.frame = CGRectOffset( self.bannerView.frame, 0, -50 ); self.bannerView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait]; self.bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait; self.bannerView.delegate=self; [AdsWrapper addAdView:self.bannerView atPos:pos]; self.bannerView.center = OUT_POS; self.bannerPos = pos; [UIView commitAnimations]; self.bannerVisible = false; } // 在加载广告前通告 - (void)bannerViewWillLoadAd:(ADBannerView *)banner NS_AVAILABLE_IOS(5_0) { NSLog( @"bannerViewWillLoadAd" ); } // 每次有新广告加载后通告 - (void)bannerViewDidLoadAd:(ADBannerView *)banner { NSLog( @"bannerViewDidLoadAd" ); if( self.bannerVisible == false ) { [self.bannerView removeFromSuperview]; [AdsWrapper addAdView:self.bannerView atPos:self.bannerPos]; [UIView commitAnimations]; self.bannerVisible = true; // 向监听器发送广告显示的通告 [AdsWrapper onAdsResult:self withRet:kAdsShown withMsg:@"ok"]; } // 向监听器发送接到数据的通告 [AdsWrapper onAdsResult:self withRet:kAdsReceived withMsg:@"ok"]; } // 发生错误 - (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error { NSLog( @"didFailToReceiveAdWithError" ); if( self.bannerVisible ) { self.bannerView.center = OUT_POS; self.bannerVisible = false; // 向监听器发送广告隐藏(错过)的通告 [AdsWrapper onAdsResult:self withRet:kAdsDismissed withMsg:@"ok"]; } // 向监听器发送广告接受数据错误的通告 [AdsWrapper onAdsResult:self withRet:kNetworkError withMsg:error.domain]; } // 当用户点击广告栏通告,返回值BOOL指定广告是否打开 - (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave { NSLog( @"bannerViewActionShouldBegin" ); return TRUE; } // 全画面的广告表示完了后,调用该接口 // 该接口被调用之后,当前程序一般会作为后台程序运行 // 该接口中需要回复之前被中断的处理(如果有的话) - (void)bannerViewActionDidFinish:(ADBannerView *)banner { NSLog( @"bannerViewActionDidFinish" ); // 向监听器发送广告点击成功关闭的通告 [AdsWrapper onPlayerGetPoints:self withPoints:1]; } @end
4:使用的时候和其他的插件一样,加载,显示,隐藏,卸载..
#if CC_TARGET_PLATFORM == CC_PLATFORM_IOS m_pNowAdsPtl = dynamic_cast<ProtocolAds*>(PluginManager::getInstance()->loadPlugin( "AdsApple" ) ); if( m_pNowAdsPtl ) { m_mapAdsInfo["AppleType"] = "1"; m_bUsingIAD = true; } #endif
5:iAd有广告点击切换前后的事件通知,为了保证监听接口不变,GetPoint成了点击广告后的监听回调,用于给小费...
6:iAd在部分国家没有支持,可以根据时区或者其他的检测方法进行广告平台之间的切换,我用的是失败次数检测,这里就不写出来了,各有各的办法.
结束~