[cocos2dx 3.0 + ios]如何编写iAd的plugin

cocos2dx3.0自带的plugin包含推广,收益等各个方面的第三方插件,但是对iAd没有支持,大概是因为专属于IOS,没有单独成库的必要,不过为了统一使用广告的插件化管理,封装一个专属IOS的IAD插件还是有必要的,搞了一天,在这里做个记录,有兴趣的朋友可以参考一下,不完善的地方请指出:

1:如何创建XCODE库就不说了,创建在其他广告库的同级目录,文件结构也是一样,一共就三个文件:

  .pch

  AdsApple.h

  AdsApple.m

2:要加入到cocos2dx的PluginManager统一管理,那么我们这个新的类需要继承InterfaceAds,并且链接iAd.framework.

3:快1点了,直接上代码,完整注释:

  头文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
//  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

  实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
//  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:使用的时候和其他的插件一样,加载,显示,隐藏,卸载..

1
2
3
4
5
6
7
#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在部分国家没有支持,可以根据时区或者其他的检测方法进行广告平台之间的切换,我用的是失败次数检测,这里就不写出来了,各有各的办法.

结束~ 

[cocos2dx 3.0 + ios]如何编写iAd的plugin,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。