Reputation: 1729
I am trying to add Vungle video ads in my sprite kit skscene. I have a sprite node which is when clicked, should load the ad. The guide provided by Vungle https://github.com/Vungle/vungle-resources/blob/master/iOS-resources/iOS-dev-guide.md shows how to place an ad through a view controller.
VungleSDK* sdk = [VungleSDK sharedSDK];
[sdk playAd:self];
I have different SKScene and i want to play ad in the scene rather than i the view controller. How can i achieve it.
Following is my SKScene code where the user is clicking an SKSpriteNode and i want the ad to load.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
SKNode *n = [self nodeAtPoint:[touch locationInNode:self]];
if ( [n.name isEqual: @"play"]) {
[self levelSelect];
}
else if( [n.name isEqual: @"coins"]){
VungleSDK* sdk = [VungleSDK sharedSDK];
[sdk playAd:self.view]; //TODO
}
}
This gives error as i am not passing a view controller to the method playAd. Can someone guide me?
Upvotes: 1
Views: 416
Reputation: 1729
Sovled this so if anyone else gets the same problem, here is the solution:-
In your view controller , Do this inside viewDidLoad method
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"playVungle" object:nil];
also create a method
-(void)playVungleAd{
VungleSDK* sdk = [VungleSDK sharedSDK];
[sdk playAd:self];
}
Don't forget to import VungleSDK/VungleSDK.h Now in your skscene, inside your touches began method do this
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
SKNode *n = [self nodeAtPoint:[touch locationInNode:self]];
if ( [n.name isEqual: @"play"]) {
[self levelSelect];
}
else if( [n.name isEqual: @"coins"]){
[[NSNotificationCenter defaultCenter] postNotificationName:@"playVungle" object:nil]; //Sends message to viewcontroller to show ad.
}
}
Here we are sending a message to view controller to play the vungle ad. Now when you touch your "coins" skspritenode in your scene, it should play the video ad.
Upvotes: 2