Search code examples
objective-cfirebaseadmob-rewardedvideoad

How to get the Ad Unit ID in Rewarded Video AdMob callbacks in Objective-C


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


Solution

  • 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.