1

We're developing an iOS app in objective-C and we've decided to add Firebase in our app. We also decided to add the GoogleMobileAds framework that comes with it in order to have Rewarded Video Ads.

I've implemented AdMob as detailed in the official guide but I can't figure out how to get the ad unit ID in each of the callbacks. The only parameter I have is of type GADRewardBasedVideoAd and it doesn't seem to have any accessible data that provide the ad ID.

Here's one of the callbacks:

- (void)rewardBasedVideoAdDidOpen:(GADRewardBasedVideoAd *)rewardBasedVideoAd
{
    NSLog(@"Opened reward based video ad.");
}

I need the ad ID because we're using several ads in our app and I need to know which one is ready/opened/completed/failed/etc.

I tried to use rewardBasedVideoAd.adMetadata[@"AdId"] but it returns nil.

Any help would be appreciated. Thanks

Elyakim Levi
  • 360
  • 4
  • 16

1 Answers1

1

here:

@implementation GameViewController{
    GADRewardedAd *gameOverRewardedAd, *extraCoinsRewardedAd;
}

-(void)viewDidLoad{
    gameOverRewardedAd = [self createAndLoadRewardedAdForAdUnit:@"ca-app-pub-YOURID"];
    extraCoinsRewardedAd = [self createAndLoadRewardedAdForAdUnit:@"ca-app-pub-YOURID"];
}


-(GADRewardedAd *)createAndLoadRewardedAdForAdUnit:(NSString *) adUnitId {
    GADRewardedAd *rewardedAd = [[GADRewardedAd alloc] initWithAdUnitID:adUnitId];
    GADRequest *request = [GADRequest request];
    [rewardedAd loadRequest:request completionHandler:^(GADRequestError * _Nullable error) {
        if (error) {
            // Handle ad failed to load case.

        } else {
            // Ad successfully loaded.

        }
    }];
    return rewardedAd;
}

Then:

#pragma mark admob reward Ad delegate
- (void)rewardedAdDidDismiss:(GADRewardedAd *)rewardedAd {
    //NSLog(@"rewardedAdDidDismiss:");

    if (rewardedAd == gameOverRewardedAd) {
        //do your things here
    }else if (rewardedAd == extraCoinsRewardedAd){

    }
}

Hope this helps.

Dan Dan
  • 86
  • 1
  • 6
  • The example you showed looks like a different library than what is used in the official documentation. And you left out the important bit: what does the createAndLoadRewardedAdForUnitId return? How do you get the instance? The official documentation uses this line of code: [[GADRewardBasedVideoAd sharedInstance] loadRequest:[GADRequest request] withAdUnitID:@"ca-app-pub-..."]; and returns void so I don't have any reference to the ad. – Elyakim Levi Sep 09 '19 at 10:17
  • 1
    Updated the createAndLoadRewardedAdForUnitId() function, those example was based on google's new API for rewardedADs, here's the link: https://developers.google.com/admob/ios/rewarded-ads – Dan Dan Sep 10 '19 at 03:25
  • This looks promising. It supports multiple ads and returns an instance for each ad, so tracking them is possible. This API is still in beta version so I'm not sure how reliable it is. I'll accept the answer though :) – Elyakim Levi Sep 11 '19 at 10:31
  • Thanks, I published my App month ago with this beta API form Admob, https://apps.apple.com/ca/app/room-escape-world-challenge/id1475441218 it works fine. Beside that, as google doc mentioned we can only have one reward AD at same time if using the previous version API. – Dan Dan Sep 14 '19 at 05:21