Search code examples
iosobjective-cvungle-ads

How to see if a vungle ad has loaded and been completed before providing an incentive?


In my current app, I'm implementing incentivized advertising through vungle. After the user plays the ad, I want to code it so that if the ad has loaded and the ad has been completed, my app redirects you to another SKScene called Continue. However, I am not sure how to check if the ad has been completed/loaded in order to effectively redirect the user. I want to ensure that there are no loopholes and that the incentive can't be accessed if there is no internet connection/if the ad doesn't load. Any help would be much appreciated, thanks in advance. Below is my current code

In AppDelegate.m

NSString* appID = @"XXXXXXXX";
VungleSDK* sdk = [VungleSDK sharedSDK];
// start vungle publisher library
[sdk startWithAppId:appID];

In GameViewController.m Under ViewDidLoad

[[NSNotificationCenter defaultCenter] addObserver:self     selector:@selector(handleNotification:) name:@"showVideo" object:nil];
Under handleNotification:(NSNotification *)notification

if ([notification.name isEqualToString:@"showVideo"]) {
    VungleSDK* sdk = [VungleSDK sharedSDK];
    NSError *error;
    [sdk playAd:self error:&error];
}

In GamePlay.m under a button class

[[NSNotificationCenter defaultCenter] postNotificationName:@"showVideo" object:nil];

Also in GamePlay.m

- (void)vungleSDKwillCloseAdWithViewInfo:(NSDictionary *)viewInfo willPresentProductSheet:(BOOL)willPresentProductSheet {
//Verify that the view was completed before rewarding the user
BOOL completedView = [[viewInfo valueForKey:@"completedView"] boolValue];
if (completedView) {


    Continue *Continue1 = [Continue sceneWithSize:self.frame.size];
    SKTransition *transition = [SKTransition revealWithDirection:SKTransitionDirectionDown duration:0.75];
    [self.view presentScene:Continue1 transition:transition];
    [self.button play];
}
}

Solution

  • You'll want to implement the VungleSDK Delegate, which can be found under Delegate Methods, in the advanced settings guide.

    The callback (void)vungleSDKwillCloseAdWithViewInfo: will pass you a viewInfo dictionary.

    If the key completedView returns YES, you can go ahead and reward the user for their view.