我正在尝试在我的 sprite 套件 skscene 中添加 Vungle 视频广告。我有一个 sprite 节点,点击它应该加载广告。 Vungle 提供的指南 https://github.com/Vungle/vungle-resources/blob/master/iOS-resources/iOS-dev-guide.md显示如何通过 View Controller 放置广告。
VungleSDK* sdk = [VungleSDK sharedSDK];
[sdk playAd:self];
我有不同的 SKScene,我想在场景中播放广告,而不是在 View Controller 中播放。我怎样才能实现它。
以下是我的 SKScene 代码,其中用户单击 SKSpriteNode,我希望加载广告。
- (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
}
}
这会产生错误,因为我没有将 View Controller 传递给 playAd 方法。有人可以指导我吗?
最佳答案
解决了这个问题,所以如果其他人遇到同样的问题,这里是解决方案:-
在您的 View Controller 中,在 viewDidLoad 方法中执行此操作
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:@"playVungle" object:nil];
同时创建一个方法
-(void)playVungleAd{
VungleSDK* sdk = [VungleSDK sharedSDK];
[sdk playAd:self];
}
不要忘记导入 VungleSDK/VungleSDK.h 现在在您的 skscene 中,在您的 touches begin 方法中执行此操作
- (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.
}
}
这里我们向 View Controller 发送消息以播放 vungle 广告。现在,当您触摸场景中的“硬币”skspritenode 时,它应该会播放视频广告。
关于ios - Sprite 套件场景中来自 Vungle 的视频广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26569560/