-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
GADUBanner.m
172 lines (144 loc) · 5.31 KB
/
GADUBanner.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
// Copyright 2014 Google Inc. All Rights Reserved.
@import CoreGraphics;
@import Foundation;
@import GoogleMobileAds;
@import UIKit;
#import "GADUBanner.h"
#import "UnityAppController.h"
@interface GADUBanner ()<GADBannerViewDelegate>
/// Defines where the ad should be positioned on the screen.
@property(nonatomic, assign) GADAdPosition adPosition;
@end
@implementation GADUBanner
/// Returns the Unity view controller.
+ (UIViewController *)unityGLViewController {
return ((UnityAppController *)[UIApplication sharedApplication].delegate).rootViewController;
}
- (id)initWithBannerClientReference:(GADUTypeBannerClientRef *)bannerClient
adUnitID:(NSString *)adUnitID
width:(CGFloat)width
height:(CGFloat)height
adPosition:(GADAdPosition)adPosition {
GADAdSize adSize = GADAdSizeFromCGSize(CGSizeMake(width, height));
return [self initWithBannerClientReference:bannerClient
adUnitID:adUnitID
adSize:adSize
adPosition:adPosition];
}
- (id)initWithSmartBannerSizeAndBannerClientReference:(GADUTypeBannerClientRef *)bannerClient
adUnitID:(NSString *)adUnitID
adPosition:(GADAdPosition)adPosition {
// Choose the correct Smart Banner constant according to orientation.
UIDeviceOrientation currentOrientation = [[UIDevice currentDevice] orientation];
GADAdSize adSize;
if (UIInterfaceOrientationIsPortrait(currentOrientation)) {
adSize = kGADAdSizeSmartBannerPortrait;
} else {
adSize = kGADAdSizeSmartBannerLandscape;
}
return [self initWithBannerClientReference:bannerClient
adUnitID:adUnitID
adSize:adSize
adPosition:adPosition];
}
- (id)initWithBannerClientReference:(GADUTypeBannerClientRef *)bannerClient
adUnitID:(NSString *)adUnitID
adSize:(GADAdSize)size
adPosition:(GADAdPosition)adPosition {
self = [super init];
if (self) {
_bannerClient = bannerClient;
_adPosition = adPosition;
_bannerView = [[GADBannerView alloc] initWithAdSize:size];
_bannerView.adUnitID = adUnitID;
_bannerView.delegate = self;
_bannerView.rootViewController = [GADUBanner unityGLViewController];
}
return self;
}
- (void)loadRequest:(GADRequest *)request {
if (!self.bannerView) {
NSLog(@"GoogleMobileAdsPlugin: BannerView is nil. Ignoring ad request.");
return;
}
[self.bannerView loadRequest:request];
}
- (void)hideBannerView {
if (!self.bannerView) {
NSLog(@"GoogleMobileAdsPlugin: BannerView is nil. Ignoring call to hideBannerView");
return;
}
self.bannerView.hidden = YES;
}
- (void)showBannerView {
if (!self.bannerView) {
NSLog(@"GoogleMobileAdsPlugin: BannerView is nil. Ignoring call to showBannerView");
return;
}
self.bannerView.hidden = NO;
}
- (void)removeBannerView {
if (!self.bannerView) {
NSLog(@"GoogleMobileAdsPlugin: BannerView is nil. Ignoring call to removeBannerView");
return;
}
[self.bannerView removeFromSuperview];
}
#pragma mark GADBannerViewDelegate implementation
- (void)adViewDidReceiveAd:(GADBannerView *)adView {
UIView *unityView = [[GADUBanner unityGLViewController] view];
CGPoint center = CGPointMake(CGRectGetMidX(unityView.bounds), CGRectGetMidY(_bannerView.bounds));
// Position the GADBannerView.
switch (self.adPosition) {
case kGADAdPositionTopOfScreen:
center = CGPointMake(CGRectGetMidX(unityView.bounds), CGRectGetMidY(_bannerView.bounds));
break;
case kGADAdPositionBottomOfScreen:
center = CGPointMake(CGRectGetMidX(unityView.bounds),
CGRectGetMaxY(unityView.bounds) - CGRectGetMidY(_bannerView.bounds));
break;
}
// Remove existing banner view from superview.
[self.bannerView removeFromSuperview];
// Add the new banner view.
self.bannerView = adView;
self.bannerView.center = center;
[unityView addSubview:self.bannerView];
if (self.adReceivedCallback) {
self.adReceivedCallback(self.bannerClient);
}
}
- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error {
if (self.adFailedCallback) {
NSString *errorMsg = [NSString
stringWithFormat:@"Failed to receive ad with error: %@", [error localizedFailureReason]];
self.adFailedCallback(self.bannerClient, [errorMsg cStringUsingEncoding:NSUTF8StringEncoding]);
}
}
- (void)adViewWillPresentScreen:(GADBannerView *)adView {
if (self.willPresentCallback) {
self.willPresentCallback(self.bannerClient);
}
}
- (void)adViewWillDismissScreen:(GADBannerView *)adView {
if (self.willDismissCallback) {
self.willDismissCallback(self.bannerClient);
}
}
- (void)adViewDidDismissScreen:(GADBannerView *)adView {
if (self.didDismissCallback) {
self.didDismissCallback(self.bannerClient);
}
}
- (void)adViewWillLeaveApplication:(GADBannerView *)adView {
if (self.willLeaveCallback) {
self.willLeaveCallback(self.bannerClient);
}
}
#pragma mark Cleanup
- (void)dealloc {
_bannerView.delegate = nil;
[_bannerView release];
[super dealloc];
}
@end