forked from ThinkFun/admanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdManager.m
232 lines (190 loc) · 5.14 KB
/
AdManager.m
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
//
// AdManager.m
// SolChess
//
// Created by Samuel Ritchie on 10/11/10.
// Copyright 2010 Threadlock Design. All rights reserved.
//
#import "AdManager.h"
#import "CommonMacros.h"
#import "cocos2d.h"
#import "Cocos2DController.h"
static AdManager *sharedAdManager = nil;
@implementation AdManager
@synthesize adBannerView;
#pragma mark -
#pragma mark ADBannerView
-(id) init;
{
if ((self = [super init])) {
//Initialize the class manually to make it compatible with iOS < 4.0
Class classAdBannerView = NSClassFromString(@"ADBannerView");
if (classAdBannerView != nil)
{
[self configureBanners];
ADBannerView *adView = [[classAdBannerView alloc] initWithFrame:CGRectZero];
[adView setDelegate:self];
if (IsIPad()) {
[adView setRequiredContentSizeIdentifiers: [NSSet setWithObjects:portraitBanner, landscapeBanner, nil]];
} else {
[adView setRequiredContentSizeIdentifiers: [NSSet setWithObjects:portraitBanner, nil]];
}
self.adBannerView = adView;
[adView release];
//Add the bannerView to the openGLView which is the view of our UIViewController
[self attachAdToView:[[CCDirector sharedDirector] openGLView]];
//Set bannerView to hidden so it shows only when it is loaded.
[self.adBannerView setHidden:YES];
}
else
{
//No iAd Framework, iOS < 4.0
CCLOG(@"No iAds avaiable for this version");
}
}
return self;
}
-(void) configureBanners;
{
if (&ADBannerContentSizeIdentifierPortrait != nil) {
portraitBanner = ADBannerContentSizeIdentifierPortrait;
landscapeBanner = ADBannerContentSizeIdentifierLandscape;
NSLog(@"Running on iOS 4.2 or newer.");
} else {
portraitBanner = ADBannerContentSizeIdentifier320x50;
landscapeBanner = ADBannerContentSizeIdentifier480x32;
NSLog(@"Running on Pre-iOS 4.2.");
}
}
-(UIInterfaceOrientation) interfaceOrientation;
{
Cocos2DController *cocosController = [[[UIApplication sharedApplication] delegate] cocosController];
UIInterfaceOrientation intOrientation = [cocosController interfaceOrientation];
return intOrientation;
}
#pragma mark Singleton Methods
+ (id)sharedAdManager;
{
@synchronized(self) {
if(sharedAdManager == nil)
sharedAdManager = [[super allocWithZone:NULL] init];
}
return sharedAdManager;
}
+ (id)allocWithZone:(NSZone *)zone;
{
return [[self sharedAdManager] retain];
}
- (id)copyWithZone:(NSZone *)zone;
{
return self;
}
- (id)retain;
{
return self;
}
- (unsigned)retainCount;
{
return UINT_MAX; //denotes an object that cannot be released
}
- (void)release;
{
// never release
}
- (id)autorelease;
{
return self;
}
//should never get called!
-(void) dealloc;
{
[adBannerView release];
[super dealloc];
}
#pragma mark -
#pragma mark Other Methods
-(void) attachAdToView:(UIView *)view;
{
if (!IsEmpty(adBannerView))
[view addSubview:self.adBannerView];
}
-(void) removeAdFromViews;
{
if (!IsEmpty([adBannerView superview]))
[self.adBannerView removeFromSuperview];
}
- (void)fixBannerToDeviceOrientation:(UIInterfaceOrientation)orientation
{
//Don't rotate ad if it doesn't exist
if (adBannerView != nil)
{
//Set the transformation for each orientation
switch (orientation)
{
case UIInterfaceOrientationPortrait:
{
[adBannerView setCurrentContentSizeIdentifier:portraitBanner];
CGSize size = [ADBannerView sizeFromBannerContentSizeIdentifier:portraitBanner];
[adBannerView setCenter:CGPointMake(size.width / 2, size.height / 2)];
}
break;
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
{
[adBannerView setCurrentContentSizeIdentifier:landscapeBanner];
CGSize size = [ADBannerView sizeFromBannerContentSizeIdentifier:landscapeBanner];
[adBannerView setCenter:CGPointMake(size.width / 2, size.height / 2)];
}
break;
default:
break;
}
}
}
#pragma mark -
#pragma mark ADBannerViewDelegate
- (BOOL)allowActionToRun
{
return TRUE;
}
- (void) stopActionsForAd
{
/* remember to pause music too! */
[[CCDirector sharedDirector] stopAnimation];
[[CCDirector sharedDirector] pause];
}
- (void) startActionsForAd
{
/* resume music, if paused */
[[CCDirector sharedDirector] stopAnimation];
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] startAnimation];
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
BOOL shouldExecuteAction = [self allowActionToRun];
if (!willLeave && shouldExecuteAction)
{
// insert code here to suspend any services that might conflict with the advertisement
[self stopActionsForAd];
}
return shouldExecuteAction;
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
//Show the bannerView
[self fixBannerToDeviceOrientation:[self interfaceOrientation]];
[adBannerView setHidden:NO];
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
//Hide the bannerView if it fails to load
[adBannerView setHidden:YES];
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
//Set the device orientation
[self fixBannerToDeviceOrientation:[self interfaceOrientation]];
[self startActionsForAd];
}
@end