25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

236 lines
7.8 KiB

  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. #import "GADUBanner.h"
  3. #import <CoreGraphics/CoreGraphics.h>
  4. #import <UIKit/UIKit.h>
  5. #import "GADUPluginUtil.h"
  6. #import "UnityAppController.h"
  7. @interface GADUBanner () <GADBannerViewDelegate>
  8. /// Defines where the ad should be positioned on the screen with a GADAdPosition.
  9. @property(nonatomic, assign) GADAdPosition adPosition;
  10. /// Defines where the ad should be positioned on the screen with a CGPoint.
  11. @property(nonatomic, assign) CGPoint customAdPosition;
  12. @end
  13. @implementation GADUBanner
  14. - (id)initWithBannerClientReference:(GADUTypeBannerClientRef *)bannerClient
  15. adUnitID:(NSString *)adUnitID
  16. width:(CGFloat)width
  17. height:(CGFloat)height
  18. adPosition:(GADAdPosition)adPosition {
  19. return [self initWithBannerClientReference:bannerClient
  20. adUnitID:adUnitID
  21. adSize:[GADUPluginUtil adSizeForWidth:width height:height]
  22. adPosition:adPosition];
  23. }
  24. - (id)initWithBannerClientReference:(GADUTypeBannerClientRef *)bannerClient
  25. adUnitID:(NSString *)adUnitID
  26. width:(CGFloat)width
  27. height:(CGFloat)height
  28. customAdPosition:(CGPoint)customAdPosition {
  29. return [self initWithBannerClientReference:bannerClient
  30. adUnitID:adUnitID
  31. adSize:[GADUPluginUtil adSizeForWidth:width height:height]
  32. customAdPosition:customAdPosition];
  33. }
  34. - (id)initWithSmartBannerSizeAndBannerClientReference:(GADUTypeBannerClientRef *)bannerClient
  35. adUnitID:(NSString *)adUnitID
  36. adPosition:(GADAdPosition)adPosition {
  37. // Choose the correct Smart Banner constant according to orientation.
  38. UIDeviceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
  39. GADAdSize adSize;
  40. if (UIInterfaceOrientationIsPortrait(currentOrientation)) {
  41. adSize = kGADAdSizeSmartBannerPortrait;
  42. } else {
  43. adSize = kGADAdSizeSmartBannerLandscape;
  44. }
  45. return [self initWithBannerClientReference:bannerClient
  46. adUnitID:adUnitID
  47. adSize:adSize
  48. adPosition:adPosition];
  49. }
  50. - (id)initWithSmartBannerSizeAndBannerClientReference:(GADUTypeBannerClientRef *)bannerClient
  51. adUnitID:(NSString *)adUnitID
  52. customAdPosition:(CGPoint)customAdPosition {
  53. // Choose the correct Smart Banner constant according to orientation.
  54. UIDeviceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation;
  55. GADAdSize adSize;
  56. if (UIInterfaceOrientationIsPortrait(currentOrientation)) {
  57. adSize = kGADAdSizeSmartBannerPortrait;
  58. } else {
  59. adSize = kGADAdSizeSmartBannerLandscape;
  60. }
  61. return [self initWithBannerClientReference:bannerClient
  62. adUnitID:adUnitID
  63. adSize:adSize
  64. customAdPosition:customAdPosition];
  65. }
  66. - (id)initWithBannerClientReference:(GADUTypeBannerClientRef *)bannerClient
  67. adUnitID:(NSString *)adUnitID
  68. adSize:(GADAdSize)size
  69. adPosition:(GADAdPosition)adPosition {
  70. self = [super init];
  71. if (self) {
  72. _bannerClient = bannerClient;
  73. _adPosition = adPosition;
  74. _bannerView = [[GADBannerView alloc] initWithAdSize:[GADUPluginUtil safeAdSizeForAdSize:size]];
  75. _bannerView.adUnitID = adUnitID;
  76. _bannerView.delegate = self;
  77. _bannerView.rootViewController = [GADUPluginUtil unityGLViewController];
  78. }
  79. return self;
  80. }
  81. - (id)initWithBannerClientReference:(GADUTypeBannerClientRef *)bannerClient
  82. adUnitID:(NSString *)adUnitID
  83. adSize:(GADAdSize)size
  84. customAdPosition:(CGPoint)customAdPosition {
  85. self = [super init];
  86. if (self) {
  87. _bannerClient = bannerClient;
  88. _customAdPosition = customAdPosition;
  89. _adPosition = kGADAdPositionCustom;
  90. _bannerView = [[GADBannerView alloc] initWithAdSize:[GADUPluginUtil safeAdSizeForAdSize:size]];
  91. _bannerView.adUnitID = adUnitID;
  92. _bannerView.delegate = self;
  93. _bannerView.rootViewController = [GADUPluginUtil unityGLViewController];
  94. }
  95. return self;
  96. }
  97. - (void)dealloc {
  98. _bannerView.delegate = nil;
  99. }
  100. - (void)loadRequest:(GADRequest *)request {
  101. if (!self.bannerView) {
  102. NSLog(@"GoogleMobileAdsPlugin: BannerView is nil. Ignoring ad request.");
  103. return;
  104. }
  105. [self.bannerView loadRequest:request];
  106. }
  107. - (void)hideBannerView {
  108. if (!self.bannerView) {
  109. NSLog(@"GoogleMobileAdsPlugin: BannerView is nil. Ignoring call to hideBannerView");
  110. return;
  111. }
  112. self.bannerView.hidden = YES;
  113. }
  114. - (void)showBannerView {
  115. if (!self.bannerView) {
  116. NSLog(@"GoogleMobileAdsPlugin: BannerView is nil. Ignoring call to showBannerView");
  117. return;
  118. }
  119. self.bannerView.hidden = NO;
  120. }
  121. - (void)removeBannerView {
  122. if (!self.bannerView) {
  123. NSLog(@"GoogleMobileAdsPlugin: BannerView is nil. Ignoring call to removeBannerView");
  124. return;
  125. }
  126. [self.bannerView removeFromSuperview];
  127. }
  128. - (NSString *)mediationAdapterClassName {
  129. return [self.bannerView adNetworkClassName];
  130. }
  131. - (CGFloat)heightInPixels {
  132. return CGRectGetHeight(CGRectStandardize(self.bannerView.frame)) * [UIScreen mainScreen].scale;
  133. }
  134. - (CGFloat)widthInPixels {
  135. return CGRectGetWidth(CGRectStandardize(self.bannerView.frame)) * [UIScreen mainScreen].scale;
  136. }
  137. - (void)setAdPosition:(GADAdPosition)adPosition {
  138. _adPosition = adPosition;
  139. [self positionBannerView];
  140. }
  141. - (void)setCustomAdPosition:(CGPoint)customPosition {
  142. _customAdPosition = customPosition;
  143. _adPosition = kGADAdPositionCustom;
  144. [self positionBannerView];
  145. }
  146. - (void)positionBannerView {
  147. /// Align the bannerView in the Unity view bounds.
  148. UIView *unityView = [GADUPluginUtil unityGLViewController].view;
  149. if (self.adPosition != kGADAdPositionCustom) {
  150. [GADUPluginUtil positionView:self.bannerView inParentView:unityView adPosition:self.adPosition];
  151. } else {
  152. [GADUPluginUtil positionView:self.bannerView
  153. inParentView:unityView
  154. customPosition:self.customAdPosition];
  155. }
  156. }
  157. #pragma mark GADBannerViewDelegate implementation
  158. - (void)adViewDidReceiveAd:(GADBannerView *)adView {
  159. // Remove existing banner view from superview.
  160. [self.bannerView removeFromSuperview];
  161. // Add the new banner view.
  162. self.bannerView = adView;
  163. /// Align the bannerView in the Unity view bounds.
  164. UIView *unityView = [GADUPluginUtil unityGLViewController].view;
  165. [self positionBannerView];
  166. [unityView addSubview:self.bannerView];
  167. if (self.adReceivedCallback) {
  168. self.adReceivedCallback(self.bannerClient);
  169. }
  170. }
  171. - (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error {
  172. if (self.adFailedCallback) {
  173. NSString *errorMsg = [NSString
  174. stringWithFormat:@"Failed to receive ad with error: %@", [error localizedFailureReason]];
  175. self.adFailedCallback(self.bannerClient, [errorMsg cStringUsingEncoding:NSUTF8StringEncoding]);
  176. }
  177. }
  178. - (void)adViewWillPresentScreen:(GADBannerView *)adView {
  179. if (self.willPresentCallback) {
  180. self.willPresentCallback(self.bannerClient);
  181. }
  182. }
  183. - (void)adViewWillDismissScreen:(GADBannerView *)adView {
  184. // Callback is not forwarded to Unity.
  185. }
  186. - (void)adViewDidDismissScreen:(GADBannerView *)adView {
  187. if (self.didDismissCallback) {
  188. self.didDismissCallback(self.bannerClient);
  189. }
  190. }
  191. - (void)adViewWillLeaveApplication:(GADBannerView *)adView {
  192. if (self.willLeaveCallback) {
  193. self.willLeaveCallback(self.bannerClient);
  194. }
  195. }
  196. @end