Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 

543 rader
26 KiB

  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. #import "GADUAdLoader.h"
  3. #import "GADUBanner.h"
  4. #import "GADUInterstitial.h"
  5. #import "GADUNativeCustomTemplateAd.h"
  6. #import "GADUPluginUtil.h"
  7. #import "GADUAdNetworkExtras.h"
  8. #import "GADUObjectCache.h"
  9. #import "GADURequest.h"
  10. #import "GADURewardBasedVideoAd.h"
  11. #import "GADUTypes.h"
  12. /// Returns an NSString copying the characters from |bytes|, a C array of UTF8-encoded bytes.
  13. /// Returns nil if |bytes| is NULL.
  14. static NSString *GADUStringFromUTF8String(const char *bytes) { return bytes ? @(bytes) : nil; }
  15. /// Returns a C string from a C array of UTF8-encoded bytes.
  16. static const char *cStringCopy(const char *string) {
  17. if (!string) {
  18. return NULL;
  19. }
  20. char *res = (char *)malloc(strlen(string) + 1);
  21. strcpy(res, string);
  22. return res;
  23. }
  24. /// Returns a C string from a C array of UTF8-encoded bytes.
  25. static const char **cStringArrayCopy(NSArray *array) {
  26. if (array == nil) {
  27. return nil;
  28. }
  29. const char **stringArray;
  30. stringArray = calloc(array.count, sizeof(char *));
  31. for (int i = 0; i < array.count; i++) {
  32. stringArray[i] = cStringCopy([array[i] UTF8String]);
  33. }
  34. return stringArray;
  35. }
  36. /// Defines the native ad types.
  37. struct AdTypes {
  38. int CustomTemplateAd;
  39. };
  40. /// Configures the SDK using the settings associated with the given application ID.
  41. void GADUInitialize(const char *appId) {
  42. [GADMobileAds configureWithApplicationID:GADUStringFromUTF8String(appId)];
  43. }
  44. // The application’s audio volume. Affects audio volumes of all ads relative to
  45. // other audio output. Valid ad volume values range from 0.0 (silent) to 1.0
  46. // (current device volume). Use this method only if your application has its own
  47. // volume controls (e.g., custom music or sound effect volumes). Defaults
  48. // to 1.0.
  49. void GADUSetApplicationVolume(float volume) {
  50. [[GADMobileAds sharedInstance] setApplicationVolume:volume];
  51. }
  52. // Indicates if the application’s audio is muted. Affects initial mute state for
  53. // all ads. Use this method only if your application has its own volume controls
  54. // (e.g., custom music or sound effect muting). Defaults to NO.
  55. void GADUSetApplicationMuted(BOOL muted) {
  56. [[GADMobileAds sharedInstance] setApplicationMuted:muted];
  57. }
  58. // Indicates if the Unity app should be paused when a full screen ad (interstitial
  59. // or rewarded video ad) is displayed.
  60. void GADUSetiOSAppPauseOnBackground(BOOL pause) { [GADUPluginUtil setPauseOnBackground:pause]; }
  61. /// Creates a GADBannerView with the specified width, height, and position. Returns a reference to
  62. /// the GADUBannerView.
  63. GADUTypeBannerRef GADUCreateBannerView(GADUTypeBannerClientRef *bannerClient, const char *adUnitID,
  64. NSInteger width, NSInteger height,
  65. GADAdPosition adPosition) {
  66. GADUBanner *banner =
  67. [[GADUBanner alloc] initWithBannerClientReference:bannerClient
  68. adUnitID:GADUStringFromUTF8String(adUnitID)
  69. width:(int)width
  70. height:(int)height
  71. adPosition:adPosition];
  72. GADUObjectCache *cache = [GADUObjectCache sharedInstance];
  73. [cache.references setObject:banner forKey:[banner gadu_referenceKey]];
  74. return (__bridge GADUTypeBannerRef)banner;
  75. }
  76. /// Creates a GADBannerView with the specified width, height, and custom position. Returns
  77. /// a reference to the GADUBannerView.
  78. GADUTypeBannerRef GADUCreateBannerViewWithCustomPosition(GADUTypeBannerClientRef *bannerClient,
  79. const char *adUnitID, NSInteger width,
  80. NSInteger height, NSInteger x,
  81. NSInteger y) {
  82. CGPoint adPosition = CGPointMake(x, y);
  83. GADUBanner *banner =
  84. [[GADUBanner alloc] initWithBannerClientReference:bannerClient
  85. adUnitID:GADUStringFromUTF8String(adUnitID)
  86. width:(int)width
  87. height:(int)height
  88. customAdPosition:adPosition];
  89. GADUObjectCache *cache = [GADUObjectCache sharedInstance];
  90. [cache.references setObject:banner forKey:[banner gadu_referenceKey]];
  91. return (__bridge GADUTypeBannerRef)banner;
  92. }
  93. /// Creates a full-width GADBannerView in the current orientation. Returns a reference to the
  94. /// GADUBannerView.
  95. GADUTypeBannerRef GADUCreateSmartBannerView(GADUTypeBannerClientRef *bannerClient,
  96. const char *adUnitID, GADAdPosition adPosition) {
  97. GADUBanner *banner = [[GADUBanner alloc]
  98. initWithSmartBannerSizeAndBannerClientReference:bannerClient
  99. adUnitID:GADUStringFromUTF8String(adUnitID)
  100. adPosition:adPosition];
  101. GADUObjectCache *cache = [GADUObjectCache sharedInstance];
  102. [cache.references setObject:banner forKey:[banner gadu_referenceKey]];
  103. return (__bridge GADUTypeBannerRef)banner;
  104. }
  105. /// Creates a full-width GADBannerView in the current orientation with a custom position. Returns a
  106. /// reference to the GADUBannerView.
  107. GADUTypeBannerRef GADUCreateSmartBannerViewWithCustomPosition(GADUTypeBannerClientRef *bannerClient,
  108. const char *adUnitID, NSInteger x,
  109. NSInteger y) {
  110. CGPoint adPosition = CGPointMake(x, y);
  111. GADUBanner *banner = [[GADUBanner alloc]
  112. initWithSmartBannerSizeAndBannerClientReference:bannerClient
  113. adUnitID:GADUStringFromUTF8String(adUnitID)
  114. customAdPosition:adPosition];
  115. GADUObjectCache *cache = [GADUObjectCache sharedInstance];
  116. [cache.references setObject:banner forKey:[banner gadu_referenceKey]];
  117. return (__bridge GADUTypeBannerRef)banner;
  118. }
  119. /// Creates a GADUInterstitial and returns its reference.
  120. GADUTypeInterstitialRef GADUCreateInterstitial(GADUTypeInterstitialClientRef *interstitialClient,
  121. const char *adUnitID) {
  122. GADUInterstitial *interstitial = [[GADUInterstitial alloc]
  123. initWithInterstitialClientReference:interstitialClient
  124. adUnitID:GADUStringFromUTF8String(adUnitID)];
  125. GADUObjectCache *cache = [GADUObjectCache sharedInstance];
  126. [cache.references setObject:interstitial forKey:[interstitial gadu_referenceKey]];
  127. return (__bridge GADUTypeInterstitialRef)interstitial;
  128. }
  129. /// Creates a GADURewardBasedVideo and returns its reference.
  130. GADUTypeRewardBasedVideoAdRef GADUCreateRewardBasedVideoAd(
  131. GADUTypeRewardBasedVideoAdClientRef *rewardBasedVideoAdClient) {
  132. GADURewardBasedVideoAd *rewardBasedVideoAd = [[GADURewardBasedVideoAd alloc]
  133. initWithRewardBasedVideoClientReference:rewardBasedVideoAdClient];
  134. GADUObjectCache *cache = [GADUObjectCache sharedInstance];
  135. [cache.references setObject:rewardBasedVideoAd forKey:[rewardBasedVideoAd gadu_referenceKey]];
  136. return (__bridge GADUTypeRewardBasedVideoAdRef)rewardBasedVideoAd;
  137. }
  138. /// Creates a GADUAdLoader and returns its reference.
  139. GADUTypeAdLoaderRef GADUCreateAdLoader(GADUTypeAdLoaderClientRef *adLoaderClient,
  140. const char *adUnitID,
  141. const char **templateIDs, NSInteger templateIDLength,
  142. struct AdTypes *types) {
  143. NSMutableArray *templateIDsArray = [[NSMutableArray alloc] init];
  144. for (int i = 0; i < templateIDLength; i++) {
  145. [templateIDsArray addObject:GADUStringFromUTF8String(templateIDs[i])];
  146. }
  147. NSMutableArray *adTypesArray = [[NSMutableArray alloc] init];
  148. if (types->CustomTemplateAd) {
  149. [adTypesArray addObject:kGADAdLoaderAdTypeNativeCustomTemplate];
  150. }
  151. NSArray *options = nil;
  152. GADUAdLoader *adLoader =
  153. [[GADUAdLoader alloc] initWithAdLoaderClientReference:adLoaderClient
  154. adUnitID:GADUStringFromUTF8String(adUnitID)
  155. templateIDs:templateIDsArray
  156. adTypes:adTypesArray
  157. options:options];
  158. GADUObjectCache *cache = [GADUObjectCache sharedInstance];
  159. [cache.references setObject:adLoader forKey:[adLoader gadu_referenceKey]];
  160. return (__bridge GADUTypeAdLoaderRef)adLoader;
  161. }
  162. /// Sets the banner callback methods to be invoked during banner ad events.
  163. void GADUSetBannerCallbacks(GADUTypeBannerRef banner,
  164. GADUAdViewDidReceiveAdCallback adReceivedCallback,
  165. GADUAdViewDidFailToReceiveAdWithErrorCallback adFailedCallback,
  166. GADUAdViewWillPresentScreenCallback willPresentCallback,
  167. GADUAdViewDidDismissScreenCallback didDismissCallback,
  168. GADUAdViewWillLeaveApplicationCallback willLeaveCallback) {
  169. GADUBanner *internalBanner = (__bridge GADUBanner *)banner;
  170. internalBanner.adReceivedCallback = adReceivedCallback;
  171. internalBanner.adFailedCallback = adFailedCallback;
  172. internalBanner.willPresentCallback = willPresentCallback;
  173. internalBanner.didDismissCallback = didDismissCallback;
  174. internalBanner.willLeaveCallback = willLeaveCallback;
  175. }
  176. /// Sets the interstitial callback methods to be invoked during interstitial ad events.
  177. void GADUSetInterstitialCallbacks(
  178. GADUTypeInterstitialRef interstitial, GADUInterstitialDidReceiveAdCallback adReceivedCallback,
  179. GADUInterstitialDidFailToReceiveAdWithErrorCallback adFailedCallback,
  180. GADUInterstitialWillPresentScreenCallback willPresentCallback,
  181. GADUInterstitialDidDismissScreenCallback didDismissCallback,
  182. GADUInterstitialWillLeaveApplicationCallback willLeaveCallback) {
  183. GADUInterstitial *internalInterstitial = (__bridge GADUInterstitial *)interstitial;
  184. internalInterstitial.adReceivedCallback = adReceivedCallback;
  185. internalInterstitial.adFailedCallback = adFailedCallback;
  186. internalInterstitial.willPresentCallback = willPresentCallback;
  187. internalInterstitial.didDismissCallback = didDismissCallback;
  188. internalInterstitial.willLeaveCallback = willLeaveCallback;
  189. }
  190. /// Sets the reward based video callback methods to be invoked during reward based video ad events.
  191. void GADUSetRewardBasedVideoAdCallbacks(
  192. GADUTypeRewardBasedVideoAdRef rewardBasedVideoAd,
  193. GADURewardBasedVideoAdDidReceiveAdCallback adReceivedCallback,
  194. GADURewardBasedVideoAdDidFailToReceiveAdWithErrorCallback adFailedCallback,
  195. GADURewardBasedVideoAdDidOpenCallback didOpenCallback,
  196. GADURewardBasedVideoAdDidStartPlayingCallback didStartCallback,
  197. GADURewardBasedVideoAdDidCloseCallback didCloseCallback,
  198. GADURewardBasedVideoAdDidRewardCallback didRewardCallback,
  199. GADURewardBasedVideoAdWillLeaveApplicationCallback willLeaveCallback,
  200. GADURewardBasedVideoAdDidCompleteCallback didCompleteCallback) {
  201. GADURewardBasedVideoAd *internalRewardBasedVideoAd =
  202. (__bridge GADURewardBasedVideoAd *)rewardBasedVideoAd;
  203. internalRewardBasedVideoAd.adReceivedCallback = adReceivedCallback;
  204. internalRewardBasedVideoAd.adFailedCallback = adFailedCallback;
  205. internalRewardBasedVideoAd.didOpenCallback = didOpenCallback;
  206. internalRewardBasedVideoAd.didStartPlayingCallback = didStartCallback;
  207. internalRewardBasedVideoAd.didCloseCallback = didCloseCallback;
  208. internalRewardBasedVideoAd.didRewardCallback = didRewardCallback;
  209. internalRewardBasedVideoAd.willLeaveCallback = willLeaveCallback;
  210. internalRewardBasedVideoAd.didCompleteCallback = didCompleteCallback;
  211. }
  212. /// Sets the banner callback methods to be invoked during native ad events.
  213. void GADUSetAdLoaderCallbacks(
  214. GADUTypeAdLoaderRef adLoader,
  215. GADUAdLoaderDidReceiveNativeCustomTemplateAdCallback customTemplateAdReceivedCallback,
  216. GADUAdLoaderDidFailToReceiveAdWithErrorCallback adFailedCallback) {
  217. GADUAdLoader *internalAdLoader = (__bridge GADUAdLoader *)adLoader;
  218. internalAdLoader.customTemplateAdReceivedCallback = customTemplateAdReceivedCallback;
  219. internalAdLoader.adFailedCallback = adFailedCallback;
  220. }
  221. /// Sets the GADBannerView's hidden property to YES.
  222. void GADUHideBannerView(GADUTypeBannerRef banner) {
  223. GADUBanner *internalBanner = (__bridge GADUBanner *)banner;
  224. [internalBanner hideBannerView];
  225. }
  226. /// Sets the GADBannerView's hidden property to NO.
  227. void GADUShowBannerView(GADUTypeBannerRef banner) {
  228. GADUBanner *internalBanner = (__bridge GADUBanner *)banner;
  229. [internalBanner showBannerView];
  230. }
  231. /// Removes the GADURemoveBannerView from the view hierarchy.
  232. void GADURemoveBannerView(GADUTypeBannerRef banner) {
  233. GADUBanner *internalBanner = (__bridge GADUBanner *)banner;
  234. [internalBanner removeBannerView];
  235. }
  236. float GADUGetBannerViewHeightInPixels(GADUTypeBannerRef banner) {
  237. GADUBanner *internalBanner = (__bridge GADUBanner *)banner;
  238. return internalBanner.heightInPixels;
  239. }
  240. float GADUGetBannerViewWidthInPixels(GADUTypeBannerRef banner) {
  241. GADUBanner *internalBanner = (__bridge GADUBanner *)banner;
  242. return internalBanner.widthInPixels;
  243. }
  244. /// Returns YES if the GADInterstitial is ready to be shown.
  245. BOOL GADUInterstitialReady(GADUTypeInterstitialRef interstitial) {
  246. GADUInterstitial *internalInterstitial = (__bridge GADUInterstitial *)interstitial;
  247. return [internalInterstitial isReady];
  248. }
  249. /// Shows the GADInterstitial.
  250. void GADUShowInterstitial(GADUTypeInterstitialRef interstitial) {
  251. GADUInterstitial *internalInterstitial = (__bridge GADUInterstitial *)interstitial;
  252. [internalInterstitial show];
  253. }
  254. /// Returns YES if the GADRewardBasedVideo is ready to be shown.
  255. BOOL GADURewardBasedVideoAdReady(GADUTypeRewardBasedVideoAdRef rewardBasedVideo) {
  256. GADURewardBasedVideoAd *internalRewardBasedVideoAd =
  257. (__bridge GADURewardBasedVideoAd *)rewardBasedVideo;
  258. return [internalRewardBasedVideoAd isReady];
  259. }
  260. /// Sets the user ID to be used in server-to-server reward callbacks.
  261. void GADUSetRewardBasedVideoAdUserId(GADUTypeRewardBasedVideoAdRef rewardBasedVideo,
  262. const char *userId) {
  263. GADURewardBasedVideoAd *internalRewardBasedVideoAd =
  264. (__bridge GADURewardBasedVideoAd *)rewardBasedVideo;
  265. [internalRewardBasedVideoAd setUserId:GADUStringFromUTF8String(userId)];
  266. }
  267. /// Shows the GADRewardBasedVideo.
  268. void GADUShowRewardBasedVideoAd(GADUTypeRewardBasedVideoAdRef rewardBasedVideoAd) {
  269. GADURewardBasedVideoAd *internalRewardBasedVideoAd =
  270. (__bridge GADURewardBasedVideoAd *)rewardBasedVideoAd;
  271. [internalRewardBasedVideoAd show];
  272. }
  273. /// Creates an empty GADRequest and returns its reference.
  274. GADUTypeRequestRef GADUCreateRequest() {
  275. GADURequest *request = [[GADURequest alloc] init];
  276. GADUObjectCache *cache = [GADUObjectCache sharedInstance];
  277. [cache.references setObject:request forKey:[request gadu_referenceKey]];
  278. return (__bridge GADUTypeRequestRef)(request);
  279. }
  280. /// Adds a test device to the GADRequest.
  281. void GADUAddTestDevice(GADUTypeRequestRef request, const char *deviceID) {
  282. GADURequest *internalRequest = (__bridge GADURequest *)request;
  283. [internalRequest addTestDevice:GADUStringFromUTF8String(deviceID)];
  284. }
  285. /// Adds a keyword to the GADRequest.
  286. void GADUAddKeyword(GADUTypeRequestRef request, const char *keyword) {
  287. GADURequest *internalRequest = (__bridge GADURequest *)request;
  288. [internalRequest addKeyword:GADUStringFromUTF8String(keyword)];
  289. }
  290. /// Sets the request agent for the GADRequest.
  291. void GADUSetRequestAgent(GADUTypeRequestRef request, const char *requestAgent) {
  292. GADURequest *internalRequest = (__bridge GADURequest *)request;
  293. [internalRequest setRequestAgent:GADUStringFromUTF8String(requestAgent)];
  294. }
  295. /// Sets the user's birthday on the GADRequest.
  296. void GADUSetBirthday(GADUTypeRequestRef request, NSInteger year, NSInteger month, NSInteger day) {
  297. GADURequest *internalRequest = (__bridge GADURequest *)request;
  298. [internalRequest setBirthdayWithMonth:month day:day year:year];
  299. }
  300. /// Sets the user's gender on the GADRequest.
  301. void GADUSetGender(GADUTypeRequestRef request, NSInteger genderCode) {
  302. GADURequest *internalRequest = (__bridge GADURequest *)request;
  303. [internalRequest setGenderWithCode:genderCode];
  304. }
  305. /// Tags a GADRequest to specify whether it should be treated as child-directed for purposes of the
  306. /// Children’s Online Privacy Protection Act (COPPA) -
  307. /// http://business.ftc.gov/privacy-and-security/childrens-privacy.
  308. void GADUTagForChildDirectedTreatment(GADUTypeRequestRef request, BOOL childDirectedTreatment) {
  309. GADURequest *internalRequest = (__bridge GADURequest *)request;
  310. internalRequest.tagForChildDirectedTreatment = childDirectedTreatment;
  311. }
  312. /// Creates an empty NSMutableableDictionary returns its reference.
  313. GADUTypeMutableDictionaryRef GADUCreateMutableDictionary() {
  314. NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
  315. GADUObjectCache *cache = [GADUObjectCache sharedInstance];
  316. [cache.references setObject:dictionary forKey:[dictionary gadu_referenceKey]];
  317. return (__bridge GADUTypeMutableDictionaryRef)(dictionary);
  318. }
  319. /// Sets an mediation extra key value pair on a NSMutableableDictionary.
  320. void GADUMutableDictionarySetValue(GADUTypeMutableDictionaryRef dictionary, const char *key,
  321. const char *value) {
  322. NSMutableDictionary *internalDictionary = (__bridge NSMutableDictionary *)dictionary;
  323. [internalDictionary setValue:GADUStringFromUTF8String(value)
  324. forKey:GADUStringFromUTF8String(key)];
  325. }
  326. /// Create a GADMediatonExtras object from the specified NSMutableableDictionary of extras and
  327. /// include it in the ad request.
  328. void GADUSetMediationExtras(GADUTypeRequestRef request, GADUTypeMutableDictionaryRef dictionary,
  329. const char *adNetworkExtraClassName) {
  330. GADURequest *internalRequest = (__bridge GADURequest *)request;
  331. NSMutableDictionary *internalDictionary = (__bridge NSMutableDictionary *)dictionary;
  332. GADUObjectCache *cache = [GADUObjectCache sharedInstance];
  333. id<GADUAdNetworkExtras> extra =
  334. [[NSClassFromString(GADUStringFromUTF8String(adNetworkExtraClassName)) alloc] init];
  335. if (![extra respondsToSelector:@selector(adNetworkExtrasWithDictionary:)]) {
  336. NSLog(@"Unable to create mediation ad network class: %@",
  337. GADUStringFromUTF8String(adNetworkExtraClassName));
  338. [cache.references removeObjectForKey:[internalDictionary gadu_referenceKey]];
  339. return;
  340. }
  341. [internalRequest.mediationExtras
  342. addObject:[extra adNetworkExtrasWithDictionary:internalDictionary]];
  343. [cache.references removeObjectForKey:[internalDictionary gadu_referenceKey]];
  344. }
  345. /// Sets an extra parameter to be included in the ad request.
  346. void GADUSetExtra(GADUTypeRequestRef request, const char *key, const char *value) {
  347. GADURequest *internalRequest = (__bridge GADURequest *)request;
  348. [internalRequest setExtraWithKey:GADUStringFromUTF8String(key)
  349. value:GADUStringFromUTF8String(value)];
  350. }
  351. /// Makes a banner ad request.
  352. void GADURequestBannerAd(GADUTypeBannerRef banner, GADUTypeRequestRef request) {
  353. GADUBanner *internalBanner = (__bridge GADUBanner *)banner;
  354. GADURequest *internalRequest = (__bridge GADURequest *)request;
  355. [internalBanner loadRequest:[internalRequest request]];
  356. }
  357. void GADUSetBannerViewAdPosition(GADUTypeBannerRef banner, int position) {
  358. GADUBanner *internalBanner = (__bridge GADUBanner *)banner;
  359. [internalBanner setAdPosition:(GADAdPosition)position];
  360. }
  361. void GADUSetBannerViewCustomPosition(GADUTypeBannerRef banner, int x, int y) {
  362. GADUBanner *internalBanner = (__bridge GADUBanner *)banner;
  363. [internalBanner setCustomAdPosition:CGPointMake(x, y)];
  364. }
  365. /// Makes an interstitial ad request.
  366. void GADURequestInterstitial(GADUTypeInterstitialRef interstitial, GADUTypeRequestRef request) {
  367. GADUInterstitial *internalInterstitial = (__bridge GADUInterstitial *)interstitial;
  368. GADURequest *internalRequest = (__bridge GADURequest *)request;
  369. [internalInterstitial loadRequest:[internalRequest request]];
  370. }
  371. /// Makes a rewarded video ad request.
  372. void GADURequestRewardBasedVideoAd(GADUTypeRewardBasedVideoAdRef rewardBasedVideoAd,
  373. GADUTypeRequestRef request, const char *adUnitID) {
  374. GADURewardBasedVideoAd *internalRewardBasedVideoAd =
  375. (__bridge GADURewardBasedVideoAd *)rewardBasedVideoAd;
  376. GADURequest *internalRequest = (__bridge GADURequest *)request;
  377. [internalRewardBasedVideoAd loadRequest:[internalRequest request]
  378. withAdUnitID:GADUStringFromUTF8String(adUnitID)];
  379. }
  380. /// Makes a native ad request.
  381. void GADURequestNativeAd(GADUTypeAdLoaderRef adLoader, GADUTypeRequestRef request) {
  382. GADUAdLoader *internalAdLoader = (__bridge GADUAdLoader *)adLoader;
  383. GADURequest *internalRequest = (__bridge GADURequest *)request;
  384. [internalAdLoader loadRequest:[internalRequest request]];
  385. }
  386. /// Return the template ID of the native custom template ad.
  387. const char *GADUNativeCustomTemplateAdTemplateID(
  388. GADUTypeNativeCustomTemplateAdRef nativeCustomTemplateAd) {
  389. GADUNativeCustomTemplateAd *internalNativeCustomTemplateAd =
  390. (__bridge GADUNativeCustomTemplateAd *)nativeCustomTemplateAd;
  391. return cStringCopy([internalNativeCustomTemplateAd templateID].UTF8String);
  392. }
  393. /// Returns the image corresponding to the specifed key as a base64 encoded byte array.
  394. const char *GADUNativeCustomTemplateAdImageAsBytesForKey(
  395. GADUTypeNativeCustomTemplateAdRef nativeCustomTemplateAd, const char *key) {
  396. GADUNativeCustomTemplateAd *internalNativeCustomTemplateAd =
  397. (__bridge GADUNativeCustomTemplateAd *)nativeCustomTemplateAd;
  398. NSData *imageData = UIImageJPEGRepresentation(
  399. [internalNativeCustomTemplateAd imageForKey:GADUStringFromUTF8String(key)], 0.0);
  400. NSString *base64String = [imageData base64EncodedStringWithOptions:nil];
  401. return cStringCopy(base64String.UTF8String);
  402. }
  403. /// Returns the string corresponding to the specifed key.
  404. const char *GADUNativeCustomTemplateAdStringForKey(
  405. GADUTypeNativeCustomTemplateAdRef nativeCustomTemplateAd, const char *key) {
  406. GADUNativeCustomTemplateAd *internalNativeCustomTemplateAd =
  407. (__bridge GADUNativeCustomTemplateAd *)nativeCustomTemplateAd;
  408. return cStringCopy(
  409. [internalNativeCustomTemplateAd stringForKey:GADUStringFromUTF8String(key)].UTF8String);
  410. }
  411. /// Call when the ad is played on screen to the user.
  412. void GADUNativeCustomTemplateAdRecordImpression(
  413. GADUTypeNativeCustomTemplateAdRef nativeCustomTemplateAd) {
  414. GADUNativeCustomTemplateAd *internalNativeCustomTemplateAd =
  415. (__bridge GADUNativeCustomTemplateAd *)nativeCustomTemplateAd;
  416. [internalNativeCustomTemplateAd recordImpression];
  417. }
  418. /// Call when the user clicks on an ad.
  419. void GADUNativeCustomTemplateAdPerformClickOnAssetWithKey(
  420. GADUTypeNativeCustomTemplateAdRef nativeCustomTemplateAd, const char *key,
  421. BOOL customClickAction) {
  422. GADUNativeCustomTemplateAd *internalNativeCustomTemplateAd =
  423. (__bridge GADUNativeCustomTemplateAd *)nativeCustomTemplateAd;
  424. [internalNativeCustomTemplateAd performClickOnAssetWithKey:GADUStringFromUTF8String(key)
  425. withCustomClickAction:customClickAction];
  426. }
  427. /// Returns the list of available asset keys for a custom native template ad.
  428. const char **GADUNativeCustomTemplateAdAvailableAssetKeys(
  429. GADUTypeNativeCustomTemplateAdRef nativeCustomTemplateAd) {
  430. GADUNativeCustomTemplateAd *internalNativeCustomTemplateAd =
  431. (__bridge GADUNativeCustomTemplateAd *)nativeCustomTemplateAd;
  432. NSArray *availableAssetKeys = [internalNativeCustomTemplateAd availableAssetKeys];
  433. return cStringArrayCopy(availableAssetKeys);
  434. }
  435. /// Returns the number of available asset keys for a custom native template ad.
  436. int GADUNativeCustomTemplateAdNumberOfAvailableAssetKeys(
  437. GADUTypeNativeCustomTemplateAdRef nativeCustomTemplateAd) {
  438. GADUNativeCustomTemplateAd *internalNativeCustomTemplateAd =
  439. (__bridge GADUNativeCustomTemplateAd *)nativeCustomTemplateAd;
  440. return (int)[internalNativeCustomTemplateAd availableAssetKeys].count;
  441. }
  442. /// Sets the Unity native custom template ad client reference on GADUNativeCustomTemplateAd.
  443. void GADUSetNativeCustomTemplateAdUnityClient(
  444. GADUTypeNativeCustomTemplateAdRef nativeCustomTemplateAd,
  445. GADUTypeNativeCustomTemplateAdClientRef *nativeCustomTemplateClient) {
  446. GADUNativeCustomTemplateAd *internalNativeCustomTemplateAd =
  447. (__bridge GADUNativeCustomTemplateAd *)nativeCustomTemplateAd;
  448. internalNativeCustomTemplateAd.nativeCustomTemplateClient = nativeCustomTemplateClient;
  449. }
  450. /// Sets the ad callback methods to be invoked during native custom template ad events.
  451. void GADUSetNativeCustomTemplateAdCallbacks(
  452. GADUTypeNativeCustomTemplateAdRef nativeCustomTemplateAd,
  453. GADUNativeCustomTemplateDidReceiveClickCallback adClickedCallback) {
  454. GADUNativeCustomTemplateAd *internalNativeCustomTemplateAd =
  455. (__bridge GADUNativeCustomTemplateAd *)nativeCustomTemplateAd;
  456. internalNativeCustomTemplateAd.didReceiveClickCallback = adClickedCallback;
  457. }
  458. /// Removes an object from the cache.
  459. void GADURelease(GADUTypeRef ref) {
  460. if (ref) {
  461. GADUObjectCache *cache = [GADUObjectCache sharedInstance];
  462. [cache.references removeObjectForKey:[(__bridge NSObject *)ref gadu_referenceKey]];
  463. }
  464. }
  465. const char *GADUMediationAdapterClassNameForBannerView(GADUTypeBannerRef bannerView) {
  466. GADUBanner *banner = (__bridge GADUBanner *)bannerView;
  467. return cStringCopy(banner.mediationAdapterClassName.UTF8String);
  468. }
  469. const char *GADUMediationAdapterClassNameForRewardedVideo(
  470. GADUTypeRewardBasedVideoAdRef rewardedVideo) {
  471. GADURewardBasedVideoAd *rewarded = (__bridge GADURewardBasedVideoAd *)rewardedVideo;
  472. return cStringCopy(rewarded.mediationAdapterClassName.UTF8String);
  473. }
  474. const char *GADUMediationAdapterClassNameForInterstitial(GADUTypeInterstitialRef interstitial) {
  475. GADUInterstitial *interstitialAd = (__bridge GADUInterstitial *)interstitial;
  476. return cStringCopy(interstitialAd.mediationAdapterClassName.UTF8String);
  477. }