forked from chrisjp/CJPAdController
/
CJPAdController.m
502 lines (431 loc) · 18.3 KB
/
CJPAdController.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//
// CJPAdController.m
// ChrisJP
//
// Created by Chris Phillips on 19/11/2011.
// Copyright (c) 2011 ChrisJP. All rights reserved.
//
#import "CJPAdController.h"
static CJPAdController *CJPSharedManager = nil;
@implementation CJPAdController
@synthesize iAdView = _iAdView;
@synthesize adMobView = _adMobView;
@synthesize contentController = _contentController;
@synthesize containerView = _containerView;
@synthesize showingiAd = _showingiAd;
@synthesize showingAdMob = _showingAdMob;
@synthesize adsRemoved = _adsRemoved;
@synthesize iOS4 = _iOS4;
@synthesize kADBannerContentSizeIdentifierLandscape;
@synthesize kADBannerContentSizeIdentifierPortrait;
#pragma mark -
#pragma mark Class Methods
+ (CJPAdController *)sharedManager
{
@synchronized(self) {
if (CJPSharedManager == nil){
CJPSharedManager = [[self alloc] init];
}
}
return CJPSharedManager;
}
- (id)initWithContentViewController:(UIViewController *)contentController
{
self = [super init];
if (self != nil) {
// Ads Removed?
_adsRemoved = [[NSUserDefaults standardUserDefaults] boolForKey:kAdsPurchasedKey];
_contentController = contentController;
// Create a container view to hold both our parent view and the banner view
// iOS 5+ can use native view containment
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 5) {
_containerView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self addChildViewController:_contentController];
[_containerView addSubview:_contentController.view];
[_contentController didMoveToParentViewController:self];
self.view = _containerView;
}
// iOS 4+ can't
else {
// iOS 4 Support
// Since iOS 4 does not support view containment, we create a new view that fills the screen
// We add our contentController's view as a subview to this, as well as an adview
// We then set this new view as the main view.
_iOS4 = YES;
_containerView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_containerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin;
[_containerView addSubview:_contentController.view];
self.view = _containerView;
}
// iOS 4.0/4.1 iAd Support
kADBannerContentSizeIdentifierPortrait = &ADBannerContentSizeIdentifierPortrait != nil ? ADBannerContentSizeIdentifierPortrait : ADBannerContentSizeIdentifier320x50;
kADBannerContentSizeIdentifierLandscape = &ADBannerContentSizeIdentifierLandscape != nil ? ADBannerContentSizeIdentifierLandscape : ADBannerContentSizeIdentifier480x32;
if (!_adsRemoved) {
[self performSelector:@selector(createBanner:) withObject:kDefaultAds afterDelay:kWaitTime];
}
}
return self;
}
- (void)createBanner:(NSString *)adType
{
BOOL inPortrait = UIInterfaceOrientationIsPortrait(self.interfaceOrientation);
BOOL isIPad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? YES : NO;
if(kAdTesting) NSLog(@"Creating %@", adType);
// Create iAd
if([adType isEqualToString:@"iAd"]){
_iAdView = [[ADBannerView alloc] initWithFrame:CGRectZero];
_iAdView.requiredContentSizeIdentifiers = [NSSet setWithObjects:kADBannerContentSizeIdentifierPortrait, kADBannerContentSizeIdentifierLandscape, nil];
if (!inPortrait)
_iAdView.currentContentSizeIdentifier = kADBannerContentSizeIdentifierLandscape;
else
_iAdView.currentContentSizeIdentifier = kADBannerContentSizeIdentifierPortrait;
// Set initial frame to be offscreen
CGRect bannerFrame = _iAdView.frame;
if([kAdPosition isEqualToString:@"bottom"])
bannerFrame.origin.y = [[UIScreen mainScreen] bounds].size.height;
else if([kAdPosition isEqualToString:@"top"])
bannerFrame.origin.y = 0 - _iAdView.frame.size.height;
_iAdView.frame = bannerFrame;
_iAdView.delegate = self;
[self.view addSubview:_iAdView];
}
// Create AdMob
else if([adType isEqualToString:@"AdMob"]){
GADAdSize adMobSize;
if (kUseAdMobSmartSize) {
if (!inPortrait)
adMobSize = kGADAdSizeSmartBannerLandscape;
else
adMobSize = kGADAdSizeSmartBannerPortrait;
_adMobView = [[GADBannerView alloc] initWithAdSize:adMobSize];
}else{
// Legacy AdMob ad sizes don't fill the full width of the device screen apart from iPhone when in portrait view
// We need to offset the x position so the ad appears centered - Calculation: (View width - Ad width) / 2
// Problem is that getting the width of the bounds doesn't take into account the current orientation
// As a workaround, if we're in landscape, we'll simply get the height instead
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = inPortrait ? CGRectGetWidth(screen) : CGRectGetHeight(screen);
adMobSize = isIPad ? kGADAdSizeLeaderboard : kGADAdSizeBanner;
CGSize cgAdMobSize = CGSizeFromGADAdSize(adMobSize);
CGFloat adMobXOffset = (screenWidth-cgAdMobSize.width)/2;
_adMobView = [[GADBannerView alloc] initWithFrame:CGRectMake(adMobXOffset, self.view.frame.size.height - cgAdMobSize.height, cgAdMobSize.width, cgAdMobSize.height)];
}
// Specify the ad's "unit identifier." This is your AdMob Publisher ID.
_adMobView.adUnitID = kAdMobID;
// Set initial frame to be off screen
CGRect bannerFrame = _adMobView.frame;
if([kAdPosition isEqualToString:@"bottom"])
bannerFrame.origin.y = [[UIScreen mainScreen] bounds].size.height;
else if([kAdPosition isEqualToString:@"top"])
bannerFrame.origin.y = 0 - _adMobView.frame.size.height;
_adMobView.frame = bannerFrame;
// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
_adMobView.rootViewController = self;
_adMobView.delegate = self;
[_containerView addSubview:_adMobView];
// Request an ad
GADRequest *adMobRequest = [GADRequest request];
// Uncomment the following line if you wish to receive test ads (simulator only)
//adMobRequest.testing = YES;
[_adMobView loadRequest:adMobRequest];
}
if(kAdTesting) NSLog(@"%@ added to view.", adType);
}
- (void)removeBanner:(NSString *)adType permanently:(BOOL)permanent
{
// Hides the banner from view
// If permanent is set to YES we'll additionally set its view to nil and remove it
// iAd
if ([adType isEqualToString:@"iAd"]) {
_showingiAd = NO;
CGRect bannerFrame = _iAdView.frame;
if([kAdPosition isEqualToString:@"bottom"]){
bannerFrame.origin.y = [[UIScreen mainScreen] bounds].size.height;
}
else if([kAdPosition isEqualToString:@"top"]){
bannerFrame.origin.y = 0 - _iAdView.frame.size.height;
}
_iAdView.frame = bannerFrame;
if (permanent) {
_iAdView.delegate = nil;
[_iAdView removeFromSuperview];
_iAdView = nil;
}
}
// AdMob
if ([adType isEqualToString:@"AdMob"]) {
_showingAdMob = NO;
CGRect bannerFrame = _adMobView.frame;
if([kAdPosition isEqualToString:@"bottom"]){
bannerFrame.origin.y = [[UIScreen mainScreen] bounds].size.height;
}
else if([kAdPosition isEqualToString:@"top"]){
bannerFrame.origin.y = 0 - _adMobView.frame.size.height;
}
_adMobView.frame = bannerFrame;
if (permanent) {
_adMobView.delegate = nil;
[_adMobView removeFromSuperview];
_adMobView = nil;
}
}
[UIView animateWithDuration:0.25 animations:^{
[self layoutAds];
}];
}
- (void)removeAllAdsForever
{
// This method is intended to be called when a user has, for example, purchased removal of advertising
// Assuming you have an in-app purchase to handle that, you can call this method on successful purchase
// Remove all ad banners from the view
if(_iAdView!=nil) [self removeBanner:@"iAd" permanently:YES];
if(_adMobView!=nil) [self removeBanner:@"AdMob" permanently:YES];
// Set adsRemoved to YES, and store in UserDefaults for future use
_adsRemoved = YES;
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:kAdsPurchasedKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
#pragma mark -
#pragma mark View Methods
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return [_contentController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
// BUG FIX:
// Since we can't use view containment in iOS 4, creating our own container means the
// navigationBar does not get resized on rotation because it is not the rootViewController
// The following code manually resizes the navBar - not applicable on iPad as the navBar remains the same size anyway
if (_iOS4) {
NSArray *subs = _contentController.view.subviews;
UINavigationBar *navbar = nil;
for (int i=0; i<subs.count; i++) {
if ([[subs objectAtIndex:i] isKindOfClass:[UINavigationBar class]]) {
navbar = [subs objectAtIndex:i];
break;
}
}
if (navbar && UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone){
CGRect frame = navbar.frame;
frame.size.height = UIInterfaceOrientationIsPortrait(toInterfaceOrientation) ? 44. : 32.;
navbar.frame = frame;
}
if (_showingiAd) _iAdView.hidden = YES;
else if (_showingAdMob) _adMobView.hidden = YES;
}
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if (_iOS4) {
[UIView animateWithDuration:0.25 animations:^{
[self layoutAds];
if (_showingiAd) _iAdView.hidden = NO;
else if (_showingAdMob) _adMobView.hidden = NO;
}];
}
}
- (void)viewDidLayoutSubviews
{
// This method is called when an ad is received or removed.
// It will move the ad on/offscreen as necessary.
// It is iOS5 only, but we can manually call it on iOS4.
BOOL isPortrait = UIInterfaceOrientationIsPortrait(self.interfaceOrientation);
BOOL isPad = UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad ? YES : NO;
CGRect contentFrame = self.view.bounds;
if (_iAdView) {
if (isPortrait) {
_iAdView.currentContentSizeIdentifier = kADBannerContentSizeIdentifierPortrait;
} else {
_iAdView.currentContentSizeIdentifier = kADBannerContentSizeIdentifierLandscape;
}
CGRect bannerFrame = _iAdView.frame;
if (_showingiAd) {
if([kAdPosition isEqualToString:@"bottom"]){
contentFrame.size.height -= _iAdView.frame.size.height;
bannerFrame.origin.y = contentFrame.size.height;
}
else if([kAdPosition isEqualToString:@"top"]){
contentFrame.size.height -= _iAdView.frame.size.height;
contentFrame.origin.y += _iAdView.frame.size.height;
bannerFrame.origin.y = 0;
}
} else {
if([kAdPosition isEqualToString:@"bottom"]){
bannerFrame.origin.y = contentFrame.size.height;
}
else if([kAdPosition isEqualToString:@"top"]){
bannerFrame.origin.y = 0 - bannerFrame.size.height;
contentFrame.origin.y = 0;
}
}
_iAdView.frame = bannerFrame;
}
if (_adMobView) {
if (kUseAdMobSmartSize) {
if (isPortrait) _adMobView.adSize = kGADAdSizeSmartBannerPortrait;
else _adMobView.adSize = kGADAdSizeSmartBannerLandscape;
}
else{
// Legacy AdMob doesn't have different orientation sizes - we just need to change the X offset so the ad remains centered
CGRect bannerFrame = _adMobView.frame;
CGRect screen = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = isPortrait ? CGRectGetWidth(screen) : CGRectGetHeight(screen);
GADAdSize adMobSize = isPad ? kGADAdSizeLeaderboard : kGADAdSizeBanner;
CGSize cgAdMobSize = CGSizeFromGADAdSize(adMobSize);
CGFloat adMobXOffset = (screenWidth-cgAdMobSize.width)/2;
bannerFrame.origin.x = adMobXOffset;
_adMobView.frame = bannerFrame;
}
CGRect bannerFrame = _adMobView.frame;
if (_showingAdMob) {
if([kAdPosition isEqualToString:@"bottom"]){
contentFrame.size.height -= _adMobView.frame.size.height;
bannerFrame.origin.y = contentFrame.size.height;
}
else if([kAdPosition isEqualToString:@"top"]){
contentFrame.size.height -= _adMobView.frame.size.height;
contentFrame.origin.y += _adMobView.frame.size.height;
bannerFrame.origin.y = 0;
}
} else {
if([kAdPosition isEqualToString:@"bottom"]){
bannerFrame.origin.y = contentFrame.size.height;
}
else if([kAdPosition isEqualToString:@"top"]){
bannerFrame.origin.y = 0 - bannerFrame.size.height;
contentFrame.origin.y = 0;
}
}
_adMobView.frame = bannerFrame;
}
_contentController.view.frame = contentFrame;
}
- (void)layoutAds
{
[self.view setNeedsLayout];
[self.view layoutIfNeeded];
if(_iOS4) [self viewDidLayoutSubviews];
}
#pragma mark -
#pragma mark iAd Delegate Methods
- (void)bannerViewWillLoadAd:(ADBannerView *)banner
{
// Shouldn't need to execute any code before an iAd
// is about to be displayed
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if(kAdTesting) NSLog(@"New iAd received.");
if(!_showingiAd){
// Ensure AdMob is hidden
if (_showingAdMob) {
// If we're preferring iAd then we should remove AdMob rather than simply hiding it
if ([kDefaultAds isEqualToString:@"iAd"]) {
[self removeBanner:@"AdMob" permanently:YES];
}
else {
[self removeBanner:@"AdMob" permanently:NO];
}
_showingAdMob = NO;
}
}
_showingiAd = YES;
[UIView animateWithDuration:0.25 animations:^{
[self layoutAds];
}];
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if(kAdTesting) NSLog(@"Failed to receive iAd. %@", error.localizedDescription);
// Ensure iAd is hidden
if (_iAdView.frame.origin.y>=0 && _iAdView.frame.origin.y < _containerView.frame.size.height){
[self removeBanner:@"iAd" permanently:NO];
}
_showingiAd = NO;
// Create AdMob (if not already created)
if(kAdTesting && kUseAdMob) NSLog(@"Trying AdMob instead...");
if(_adMobView==nil && kUseAdMob){
if(kAdTesting) NSLog(@"adMobView doesn't exist. Creating view.");
[self createBanner:@"AdMob"];
}
else if(kUseAdMob){
if(kAdTesting) NSLog(@"adMobView already exists. Requesting new ad.");
[_adMobView loadRequest:[GADRequest request]];
}
[UIView animateWithDuration:0.25 animations:^{
[self layoutAds];
}];
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
//[[NSNotificationCenter defaultCenter] postNotificationName:BannerViewActionWillBegin object:self];
return YES;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
//[[NSNotificationCenter defaultCenter] postNotificationName:BannerViewActionDidFinish object:self];
}
#pragma mark -
#pragma mark AdMob Delegate Methods
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView
{
if(kAdTesting) NSLog(@"New AdMob ad received.");
if(!_showingAdMob){
// Ensure iAd is hidden, then show AdMob
if (_showingiAd) {
// If we're preferring AdMob then we should remove iAd rather than simply hiding it
if ([kDefaultAds isEqualToString:@"AdMob"]) {
[self removeBanner:@"iAd" permanently:YES];
}
else if (_iAdView.isBannerLoaded) {
[self removeBanner:@"iAd" permanently:NO];
}
_showingiAd = NO;
}
}
_showingAdMob = YES;
[UIView animateWithDuration:0.25 animations:^{
[self layoutAds];
}];
}
- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error
{
// Ensure AdMob is hidden
if (_adMobView.frame.origin.y>=0 && _adMobView.frame.origin.y < _containerView.frame.size.height){
[self removeBanner:@"AdMob" permanently:NO];
}
_showingAdMob = NO;
if(kAdTesting) NSLog(@"Failed to receive AdMob. %@", error.localizedDescription);
// Request iAd if we haven't already created one.
if (_iAdView==nil && kUseiAd){
if(kAdTesting) NSLog(@"iAd view doesn't exist. Creating view...");
[self createBanner:@"iAd"];
}
else{
// Nothing to do here...
// If iAds are enabled, delegate methods will continue firing and eventually restart the whole process.
}
[UIView animateWithDuration:0.25 animations:^{
[self layoutAds];
}];
}
// Unused AdMob delegate methods
//- (void)adViewWillPresentScreen:(GADBannerView *)bannerView
//{
//
//}
//- (void)adViewDidDismissScreen:(GADBannerView *)bannerView
//{
//
//}
//- (void)adViewWillLeaveApplication:(GADBannerView *)bannerView
//{
//
//}
@end