【发布时间】:2015-11-17 20:09:12
【问题描述】:
我正在为我的 iOS 应用程序使用 SpriteKit,并且需要一些全屏(或有时称为“插页式”iAds 以不时弹出。我可以找出何时发生的代码,但我需要知道如何添加 iAd 而无需更改视图控制器。
我尝试了 Techotopia 在 Interstitial iAds 上的 [tutorial][1],但要做到这一点,我需要在实际的视图控制器之间进行转换。好吧,我试过了,但是每当从 iAd 视图控制器转换回 GameViewController 时,它就搞砸了。 GameViewController.m 中的代码表明它最初设置为我的游戏(SKScene)的主菜单。因此,当我尝试从 iAd 转换回来时,不是保持相同的 SKScene,而是直接进入我的主菜单场景。
我需要这些答案:
- 除了使用两个视图控制器来显示插页式 iAd 之外,还有其他方法吗?
- 如果没有,我该如何解决这个问题?
- 如果我将“Interstitial iAds”更改为“Pre-roll Video iAds”,也会出现此问题吗?
-
编辑!编辑!编辑!编辑!编辑!编辑!编辑!编辑!编辑!编辑!编辑!编辑!编辑!编辑!
-
我最终决定使用 crashoverride777 的答案,并创建一个 Objective-C 桥接头将他的 Swift 代码转换为 Objective-C。但是当引用Ads.swift 的变量presentingViewController 时,它最终为零。这是我在GameViewController.m 中的代码:
// Set up interstitial iAds.
Ads *adsClass = [[Ads alloc] init];
Ads *adsInstance = [Ads sharedInstance];
adsInstance.presentingViewController = self;
然而,这并没有做任何事情。函数showInterAd 被我的GameScene.m 调用,就像它应该的那样,但没有广告出现。在我的Ads.swift 中,我添加了一个日志来查看presentingViewController 是否确实为nil。
print("iAd inter showing")
if (self.presentingViewController != nil) {
print("presentingViewController != nil")
iAdInterAdView.frame = presentingViewController.view.bounds
presentingViewController.view.addSubview(iAdInterAdView)
iAdInterAd!.presentInView(iAdInterAdView)
UIViewController.prepareInterstitialAds()
iAdInterAdView.addSubview(iAdInterAdCloseButton)
} else {
print("presentingViewController == nil")
}
日志每次都这样输出:"presentingViewController == nil"。我不确定这里出了什么问题。
【问题讨论】:
-
翻译:
iAdInterAdCloseButton = [UIButton buttonWithType:UIButtonTypeSystem]和if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) -
为什么会给您带来麻烦?第一个按钮代码是 swift 2.0 中的新内容,因为它在 swift 之前略有不同。在对象 C 它应该看起来像这样我认为 UIButton *iAdInterAdCloseButton = [UIButton buttonWithType:UIButtonTypeSystem];
-
关于设备代码。你的应用是通用的吗?否则,据我所知,它不会被调用?您可以阅读本文并尝试检查设备是否为 iPad stackoverflow.com/questions/10167221/… 的可能方法
-
我可以尽我所能帮助你,因为我对 obj c 的了解有限。我相信大部分代码都很简单,所以我认为它不应该那么难。不确定 obj c 是否有扩展之类的东西,这是我用于代表的东西。
-
我也在想,为什么不在那个 obj c 项目中使用 swift 文件并使用桥接头。对于较旧的应用程序来说,这可能是一个好主意developer.apple.com/library/ios/documentation/Swift/Conceptual/…
标签: ios sprite-kit iad viewcontroller interstitial