我在我的iOS应用中使用SpriteKit,并且需要一些全屏(或者有时称为"插页式广告" iAds常常弹出。我可以找出它的代码。发生了,但我需要知道如何添加iAd而不必更改View Controller。
我在Techtopia上尝试了关于插页式iAd的[教程] [1],但要做到这一点,我需要在实际的视图控制器之间进行切换。好吧,我尝试了这个,但每当从iAd视图控制器转换回GameViewController时,它都搞砸了。 GameViewController.m中的代码表示它最初设置为我游戏的主菜单(SKScene)。因此,当我尝试从iAd转换回来时,而不是保持相同的SKScene,它会进入我的主菜单场景。
我需要这些答案:
-
修改!编辑!编辑!编辑!编辑!编辑!编辑!编辑!编辑!编辑!编辑!编辑!编辑! EDIT!
-
我最终决定使用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
是否确实为零。
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")
}
每次都会出现这样的日志:" presentsViewController == nil" 。我不知道这里出了什么问题。
答案 0 :(得分:1)
你可以利用我在github上发布的助手。它是专门为spritekit制作的,您可以从任何地方调用广告,无需代表等或更改视图控制器。
https://github.com/crashoverride777/Swift-2-iAds-and-AdMob-Helper
我想看看那个助手应该会让你知道从哪里开始。以下是iAds Inter广告的简洁示例。
这很快,所以我不确定它是否对你有帮助。
import iAd
class Ads: NSObject {
// MARK: - Static Properties
/// Shared instance
static let sharedInstance = Ads()
// MARK: - Properties
/// Presenting view controller
var presentingViewController: UIViewController!
/// iAd inter ad
private var iAdInterAd: ADInterstitialAd?
/// iAd inter ad view
private var iAdInterAdView = UIView()
/// iAd inter ad close button
private var iAdInterAdCloseButton = UIButton(type: UIButtonType.System)
// MARK: - Init
private override init() {
super.init()
print("Ads helper init")
iAdInterAd = iAdLoadInterAd()
}
// MARK: - User Methods
/// Show inter ad
func showInterAd() {
iAdShowInterAd()
}
/// Show inter ad randomly (33% chance)
func showInterAdRandomly() {
let randomInterAd = Int(arc4random() % 3)
print("randomInterAd = \(randomInterAd)")
if randomInterAd == 1 {
iAdShowInterAd()
}
}
/// Remove all ads
func removeAllAds() {
print("Removed all ads")
if iAdInterAd != nil {
iAdInterAd!.delegate = nil
iAdInterAdCloseButton.removeFromSuperview()
iAdInterAdView.removeFromSuperview()
}
}
// MARK: - Internal Methods
/// iAd load inter ad
private func iAdLoadInterAd() -> ADInterstitialAd {
print("iAd inter ad loading...")
let iAdInterAd = ADInterstitialAd()
iAdInterAd.delegate = self
if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
iAdInterAdCloseButton.frame = CGRectMake(18, 18, 27, 27)
} else {
iAdInterAdCloseButton.frame = CGRectMake(13, 13, 22, 22)
}
iAdInterAdCloseButton.layer.cornerRadius = 11
iAdInterAdCloseButton.setTitle("X", forState: .Normal)
iAdInterAdCloseButton.setTitleColor(UIColor.grayColor(), forState: .Normal)
iAdInterAdCloseButton.backgroundColor = UIColor.whiteColor()
iAdInterAdCloseButton.layer.borderColor = UIColor.grayColor().CGColor
iAdInterAdCloseButton.layer.borderWidth = 2
iAdInterAdCloseButton.addTarget(self, action: "iAdPressedInterAdCloseButton:", forControlEvents: UIControlEvents.TouchDown)
return iAdInterAd
}
/// iAd show inter ad
private func iAdShowInterAd() {
guard iAdInterAd != nil else {
print("iAd inter is nil, reloading")
iAdInterAd = iAdLoadInterAd()
return
}
if iAdInterAd!.loaded {
print("iAd inter showing")
iAdInterAdView.frame = presentingViewController.view.bounds
presentingViewController.view.addSubview(iAdInterAdView)
iAdInterAd!.presentInView(iAdInterAdView)
UIViewController.prepareInterstitialAds()
iAdInterAdView.addSubview(iAdInterAdCloseButton)
//pauseTasks() // not really needed for inter as you tend to show them when not playing.
} else {
print("iAd inter not ready, reloading again...")
iAdInterAd = iAdLoadInterAd()
}
}
/// iAd inter ad pressed close button
func iAdPressedInterAdCloseButton(sender: UIButton) { // dont make private as its called with a selector
print("iAd inter closed")
iAdInterAd!.delegate = nil
iAdInterAdCloseButton.removeFromSuperview()
iAdInterAdView.removeFromSuperview()
iAdInterAd = iAdLoadInterAd()
//resumeTasks() // not really needed for inter as you tend to not show them during gameplay
}
/// Pause tasks in the app/game
private func pauseTasks() {
// Pause app/game, music etc here.
// you could use NSNotifactionCenter or Delegates to call methods in other SKScenes / ViewControllers
}
/// Resume tasks in the app/game
private func resumeTasks() {
// Resume app/game, music etc here.
// you could use NSNotifactionCenter or Delegates to call methods in other SKScenes / ViewControllers
}
}
// MARK: - Delegates iAd Inter
extension Ads: ADInterstitialAdDelegate {
func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
print("iAd inter did load")
}
func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
print("iAd inter did unload")
}
func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
print("iAd inter error \(error)")
iAdInterAd!.delegate = nil
iAdInterAdCloseButton.removeFromSuperview()
iAdInterAdView.removeFromSuperview()
}
}
现在你只需致电
Ads.sharedInstance.presentingViewController = self
在做其他任何事情之前,在你的GameViewController中。这将启动帮助程序并预加载第一个inter广告。
您只需在任何想要展示广告的地方打电话
GameData.sharedInstance.showInterAd()
或
GameData.sharedInstance.showInterAdRandomly() // 33% chance for ad
答案 1 :(得分:0)
在sprite工具包中显示插页式广告几乎与应用程序的其他部分相同。唯一的区别是你通过使用'self.view'调用所有iAd方法来呈现add。这将获得SKScene所呈现的视图并在其上呈现iAd
这也可用于在不切换视图控制器的情况下呈现插页式广告
currentView.requestInterstitialAdPresentation()
从现场请求:
self.view?.requestInterstitialAdPresentation()
注意函数调用中如何表示请求。这就是它有时只出现的原因。当您“请求”广告时,设备会向苹果发送请求广告的请求。如果您在合理的时间内收到回复,那么广告就会出现。如果您没有在合理的时间内收到回复(很可能是由于互联网不佳),那么广告就不会出现,因为没有任何内容可以呈现。但是,如果您请求一次并且它没有出现,则下次您提出它时肯定会出现,因为您已经从上次请求时加载了广告,但它从未出现过。