AdsAdmob.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /****************************************************************************
  2. Copyright (c) 2013 cocos2d-x.org
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #import "AdsAdmob.h"
  21. #import "AdsWrapper.h"
  22. #define OUTPUT_LOG(...) if (self.debug) NSLog(__VA_ARGS__);
  23. @implementation AdsAdmob
  24. @synthesize debug = __debug;
  25. @synthesize strPublishID = __PublishID;
  26. @synthesize testDeviceIDs = __TestDeviceIDs;
  27. - (void) dealloc
  28. {
  29. if (self.bannerView != nil) {
  30. [self.bannerView release];
  31. self.bannerView = nil;
  32. }
  33. if (self.testDeviceIDs != nil) {
  34. [self.testDeviceIDs release];
  35. self.testDeviceIDs = nil;
  36. }
  37. [super dealloc];
  38. }
  39. #pragma mark InterfaceAds impl
  40. - (void) configDeveloperInfo: (NSMutableDictionary*) devInfo
  41. {
  42. self.strPublishID = (NSString*) [devInfo objectForKey:@"AdmobID"];
  43. }
  44. - (void) showAds: (NSMutableDictionary*) info position:(int) pos
  45. {
  46. if (self.strPublishID == nil ||
  47. [self.strPublishID length] == 0) {
  48. OUTPUT_LOG(@"configDeveloperInfo() not correctly invoked in Admob!");
  49. return;
  50. }
  51. NSString* strType = [info objectForKey:@"AdmobType"];
  52. int type = [strType intValue];
  53. switch (type) {
  54. case kTypeBanner:
  55. {
  56. NSString* strSize = [info objectForKey:@"AdmobSizeEnum"];
  57. int sizeEnum = [strSize intValue];
  58. [self showBanner:sizeEnum atPos:pos];
  59. break;
  60. }
  61. case kTypeFullScreen:
  62. OUTPUT_LOG(@"Now not support full screen view in Admob");
  63. break;
  64. default:
  65. OUTPUT_LOG(@"The value of 'AdmobType' is wrong (should be 1 or 2)");
  66. break;
  67. }
  68. }
  69. - (void) hideAds: (NSMutableDictionary*) info
  70. {
  71. NSString* strType = [info objectForKey:@"AdmobType"];
  72. int type = [strType intValue];
  73. switch (type) {
  74. case kTypeBanner:
  75. {
  76. if (nil != self.bannerView) {
  77. [self.bannerView removeFromSuperview];
  78. [self.bannerView release];
  79. self.bannerView = nil;
  80. }
  81. break;
  82. }
  83. case kTypeFullScreen:
  84. OUTPUT_LOG(@"Now not support full screen view in Admob");
  85. break;
  86. default:
  87. OUTPUT_LOG(@"The value of 'AdmobType' is wrong (should be 1 or 2)");
  88. break;
  89. }
  90. }
  91. - (void) queryPoints
  92. {
  93. OUTPUT_LOG(@"Admob not support query points!");
  94. }
  95. - (void) spendPoints: (int) points
  96. {
  97. OUTPUT_LOG(@"Admob not support spend points!");
  98. }
  99. - (void) setDebugMode: (BOOL) isDebugMode
  100. {
  101. self.debug = isDebugMode;
  102. }
  103. - (NSString*) getSDKVersion
  104. {
  105. return @"6.4.2";
  106. }
  107. - (NSString*) getPluginVersion
  108. {
  109. return @"0.2.0";
  110. }
  111. - (void) showBanner: (int) sizeEnum atPos:(int) pos
  112. {
  113. GADAdSize size = kGADAdSizeBanner;
  114. switch (sizeEnum) {
  115. case kSizeBanner:
  116. size = kGADAdSizeBanner;
  117. break;
  118. case kSizeIABMRect:
  119. size = kGADAdSizeMediumRectangle;
  120. break;
  121. case kSizeIABBanner:
  122. size = kGADAdSizeFullBanner;
  123. break;
  124. case kSizeIABLeaderboard:
  125. size = kGADAdSizeLeaderboard;
  126. break;
  127. case kSizeSkyscraper:
  128. size = kGADAdSizeSkyscraper;
  129. break;
  130. default:
  131. break;
  132. }
  133. if (nil != self.bannerView) {
  134. [self.bannerView removeFromSuperview];
  135. [self.bannerView release];
  136. self.bannerView = nil;
  137. }
  138. self.bannerView = [[GADBannerView alloc] initWithAdSize:size];
  139. self.bannerView.adUnitID = self.strPublishID;
  140. self.bannerView.delegate = self;
  141. [self.bannerView setRootViewController:[AdsWrapper getCurrentRootViewController]];
  142. [AdsWrapper addAdView:self.bannerView atPos:pos];
  143. GADRequest* request = [GADRequest request];
  144. request.testDevices = [NSArray arrayWithArray:self.testDeviceIDs];
  145. [self.bannerView loadRequest:request];
  146. }
  147. #pragma mark interface for Admob SDK
  148. - (void) addTestDevice: (NSString*) deviceID
  149. {
  150. if (nil == self.testDeviceIDs) {
  151. self.testDeviceIDs = [[NSMutableArray alloc] init];
  152. [self.testDeviceIDs addObject:GAD_SIMULATOR_ID];
  153. }
  154. [self.testDeviceIDs addObject:deviceID];
  155. }
  156. #pragma mark GADBannerViewDelegate impl
  157. // Since we've received an ad, let's go ahead and set the frame to display it.
  158. - (void)adViewDidReceiveAd:(GADBannerView *)adView {
  159. NSLog(@"Received ad");
  160. [AdsWrapper onAdsResult:self withRet:kAdsReceived withMsg:@"Ads request received success!"];
  161. }
  162. - (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error {
  163. NSLog(@"Failed to receive ad with error: %@", [error localizedFailureReason]);
  164. int errorNo = kUnknownError;
  165. switch ([error code]) {
  166. case kGADErrorNetworkError:
  167. errorNo = kNetworkError;
  168. break;
  169. default:
  170. break;
  171. }
  172. [AdsWrapper onAdsResult:self withRet:errorNo withMsg:[error localizedDescription]];
  173. }
  174. @end