簡體   English   中英

iOS Sprite Kit中SKScene中的插頁式iAd

[英]Interstitial iAd within SKScene in iOS Sprite Kit

插頁式廣告的iAd示例代碼使用主ViewController展示了全屏插頁式廣告。

但是,我希望能夠在我的iOS Sprite Kit游戲中從SKScene觸發廣告。

我可以像這樣在主視圖控制器( ViewController.m )中輕松地做到這一點:

[self requestInterstitialAdPresentation];

但是,我嘗試訪問根視圖控制器,該控制器在以下視圖的viewDidLoad方法中設置了場景:

SKView * skView = (SKView *)self.view;
SKScene * scene = [SKScene sceneWithSize:skView.bounds.size];
[skView presentScene:scene];

SKScene.m我嘗試了以下方法:

[self.view.window.rootViewController requestInterstitialAdPresentation];

而X-Code拋出了這個:

No visible @interface for 'UIViewController' declares the selector 'requestInterstitialAdPresentation'

這可能很簡單,基本上我需要訪問創建SKView和SKScene的ViewController對象,以便可以將requestInterstitialAdPresentation消息發送到該對象(並顯示廣告)。

為什么不將實現了requestInterstitialAdPresentation視圖控制器實例傳遞給SKScene實例? 這樣,您可以直接引用VC,而不必經過似乎容易出錯的窗口。

如果您要接受此建議-在不了解您的代碼庫的情況下-我可能會猜測,為場景創建委托接口並在視圖控制器中實現它可能是有意義的。 該界面如下所示:

@protocol AdDelegate
- (BOOL)requestInterstitialAdPresentation;
@end

就了解為什么您的根視圖控制器不實現requestInterstitialAdPresentation ,我認為您最好的選擇是調試應用程序並查看發生了什么。 我的懷疑是您正在處理包裝主VC的容器視圖控制器,或者您要呈現的場景顯示在另一個窗口中。

您可以使用簡單的通知與您的viewController通信。在viewController中:

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(requestInterstitialAdPresentation) name:@"showAdd" object:nil];

然后,只要您想展示廣告,就可以在場景中發布通知。 在您的SKScene中:

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

希望這會有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM