AdsFacebook.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /****************************************************************************
  2. Copyright (c) 2014 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 "AdsFacebook.h"
  21. #import "AdsWrapper.h"
  22. #define OUTPUT_LOG(...) if (self.debug) NSLog(__VA_ARGS__);
  23. @implementation AdsFacebook
  24. @synthesize debug = __debug;
  25. @synthesize strPublishID = __PublishID;
  26. @synthesize testDeviceIDs = __TestDeviceIDs;
  27. - (void) configDeveloperInfo: (NSMutableDictionary*) devInfo
  28. {
  29. self.strPublishID = (NSString*) [devInfo objectForKey:@"FacebookAdID"];
  30. }
  31. - (void) showAds: (NSMutableDictionary*) info position:(int) pos
  32. {
  33. if (self.strPublishID == nil ||
  34. [self.strPublishID length] == 0) {
  35. OUTPUT_LOG(@"configDeveloperInfo() not correctly invoked in FBAd!");
  36. return;
  37. }
  38. if (self.debug){
  39. // The hash ID is printed to console when running on a device.
  40. //[self addTestDevice:@"Your device Hash ID"];
  41. }
  42. NSString* strType = [info objectForKey:@"FBAdType"];
  43. int type = [strType intValue];
  44. switch (type) {
  45. case kTypeBanner:
  46. {
  47. NSString* strSize = [info objectForKey:@"FBAdSizeEnum"];
  48. int sizeEnum = [strSize intValue];
  49. [self showBanner:sizeEnum atPos:pos];
  50. break;
  51. }
  52. case kTypeInterstitial:
  53. [self loadInterstial];
  54. break;
  55. default:
  56. OUTPUT_LOG(@"The value of 'FBAdType' is wrong (should be 1 or 2)");
  57. break;
  58. }
  59. }
  60. - (void) hideAds: (NSMutableDictionary*) info
  61. {
  62. NSString* strType = [info objectForKey:@"FBAdType"];
  63. int type = [strType intValue];
  64. switch (type)
  65. {
  66. case kTypeBanner:
  67. {
  68. if (nil != self.bannerView) {
  69. [self.bannerView removeFromSuperview];
  70. self.bannerView = nil;
  71. }
  72. break;
  73. }
  74. case kTypeInterstitial:
  75. OUTPUT_LOG(@"Now not support full screen view in FBAd");
  76. break;
  77. default:
  78. OUTPUT_LOG(@"The value of 'FBAdType' is wrong (should be 1 or 2)");
  79. break;
  80. }
  81. }
  82. - (void) queryPoints
  83. {
  84. OUTPUT_LOG(@"FBAd not support query points!");
  85. }
  86. - (void) spendPoints: (int) points
  87. {
  88. OUTPUT_LOG(@"FBAd not support spend points!");
  89. }
  90. - (void) setDebugMode: (BOOL) isDebugMode
  91. {
  92. self.debug = isDebugMode;
  93. }
  94. - (NSString*) getSDKVersion
  95. {
  96. return @"3.17.1";
  97. }
  98. - (NSString*) getPluginVersion
  99. {
  100. return @"1.0";
  101. }
  102. - (void) showBanner:(int)sizeEnum atPos:(int)pos
  103. {
  104. FBAdSize size = kFBAdSize320x50;
  105. switch (sizeEnum)
  106. {
  107. case kSizeDefault:
  108. size = kFBAdSize320x50;
  109. break;
  110. case kSizeHeightFixed:
  111. case kSizeInterstital:
  112. {
  113. BOOL isIPAD = ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad);
  114. size = (isIPAD) ? kFBAdSizeHeight90Banner : kFBAdSizeHeight50Banner;
  115. }
  116. break;
  117. default:
  118. break;
  119. }
  120. if (nil != self.bannerView) {
  121. [self.bannerView removeFromSuperview];
  122. self.bannerView = nil;
  123. }
  124. // Create a banner's ad view with a unique placement ID (generate your own on the Facebook app settings).
  125. // Use different ID for each ad placement in your app.
  126. self.bannerView = [[FBAdView alloc] initWithPlacementID:self.strPublishID
  127. adSize:size
  128. rootViewController:[AdsWrapper getCurrentRootViewController]];
  129. // Set a delegate to get notified on changes or when the user interact with the ad.
  130. self.bannerView.delegate = self;
  131. // When testing on a device, add its hashed ID to force test ads.
  132. [FBAdSettings addTestDevices:self.testDeviceIDs];
  133. // Initiate a request to load an ad.
  134. [self.bannerView loadAd];
  135. [AdsWrapper addAdView:self.bannerView atPos:pos];
  136. }
  137. - (void) loadInterstial
  138. {
  139. // Create the interstitial unit with a placement ID (generate your own on the Facebook app settings).
  140. // Use different ID for each ad placement in your app.
  141. self.interstitialView = [[FBInterstitialAd alloc] initWithPlacementID:self.strPublishID];
  142. // Set a delegate to get notified on changes or when the user interact with the ad.
  143. self.interstitialView.delegate = self;
  144. [FBAdSettings addTestDevices:self.testDeviceIDs];
  145. // Initiate the request to load the ad.
  146. [self.interstitialView loadAd];
  147. }
  148. - (void) showInterstitial
  149. {
  150. if (!self.interstitialView || !self.interstitialView.isAdValid)
  151. {
  152. // Ad not ready to present.
  153. OUTPUT_LOG(@"Ad not loaded.");
  154. } else {
  155. // Ad is ready, present it!
  156. [self.interstitialView showAdFromRootViewController:[AdsWrapper getCurrentRootViewController]];
  157. [AdsWrapper onAdsResult:self withRet:kAdsShown withMsg:@"Ads is shown!"];
  158. }
  159. }
  160. - (void) addTestDevice: (NSString*) deviceID
  161. {
  162. if (nil == self.testDeviceIDs) {
  163. self.testDeviceIDs = [[NSMutableArray alloc] init];
  164. }
  165. [self.testDeviceIDs addObject:deviceID];
  166. }
  167. #pragma mark - FBViewAdDelegate implementation
  168. - (void)adViewDidClick:(FBAdView *)adView
  169. {
  170. OUTPUT_LOG(@"Ad was clicked.");
  171. }
  172. - (void)adViewDidFinishHandlingClick:(FBAdView *)adView
  173. {
  174. OUTPUT_LOG(@"Ad did finish click handling.");
  175. }
  176. - (void)adViewDidLoad:(FBAdView *)adView
  177. {
  178. OUTPUT_LOG(@"Ad was loaded.");
  179. // Now that the ad was loaded, show the view in case it was hidden before.
  180. adView.hidden = NO;
  181. [AdsWrapper onAdsResult:self withRet:kAdsReceived withMsg:@"Ad request received success!"];
  182. }
  183. - (void)adView:(FBAdView *)adView didFailWithError:(NSError *)error
  184. {
  185. OUTPUT_LOG(@"Ad failed to load with error: %@", error);
  186. // Hide the unit since no ad is shown.
  187. adView.hidden = YES;
  188. [AdsWrapper onAdsResult:self withRet:kUnknownError withMsg:error.localizedDescription];
  189. }
  190. #pragma mark - FBInterstitialAdDelegate implementation
  191. - (void)interstitialAdDidLoad:(FBInterstitialAd *)interstitialAd
  192. {
  193. OUTPUT_LOG(@"Interstitial ad was loaded. Can present now.");
  194. [AdsWrapper onAdsResult:self withRet:kAdsReceived withMsg:@"Interstitial Ad request received success!"];
  195. [self showInterstitial];
  196. }
  197. - (void)interstitialAd:(FBInterstitialAd *)interstitialAd didFailWithError:(NSError *)error
  198. {
  199. OUTPUT_LOG(@"Interstitial failed to load with error: %@", error.description);
  200. [AdsWrapper onAdsResult:self withRet:kUnknownError withMsg:error.description];
  201. }
  202. - (void)interstitialAdDidClick:(FBInterstitialAd *)interstitialAd
  203. {
  204. OUTPUT_LOG(@"Interstitial was clicked.");
  205. }
  206. - (void)interstitialAdDidClose:(FBInterstitialAd *)interstitialAd
  207. {
  208. OUTPUT_LOG(@"Interstitial closed.");
  209. // Optional, Cleaning up.
  210. self.interstitialView = nil;
  211. [AdsWrapper onAdsResult:self withRet:kAdsDismissed withMsg:@"Interstitial Ad is dismissed!"];
  212. }
  213. - (void)interstitialAdWillClose:(FBInterstitialAd *)interstitialAd
  214. {
  215. OUTPUT_LOG(@"Interstitial will close.");
  216. }
  217. @end