2017-04-19 56 views
1

我试图在每次我的游戏转换到GameOver场景时显示AdMob插页式广告。但是,只有在我的视图控制器中将其初始化函数放入我的viewDidLoad()函数中时,广告才会显示。我在游戏中设置了一个通知中心,并且在进入GameOver场景时尝试发送通知,以触发初始化广告的功能,但这并没有成功。我想知道如何在任何给定的时间从场景中触发它,而不是在应用程序启动后立即显示它,这是将它放在视图控制器的viewDidLoad函数中。SpriteKit中的AdMob插页式广告游戏

在我GameViewController是这两个函数:

public func initAdMobInterstitial() { 

    adMobInterstitial = GADInterstitial(adUnitID: AD_MOB_INTERSTITIAL_UNIT_ID) 
    adMobInterstitial.delegate = self 
    let request = GADRequest() 
    request.testDevices = ["ddee708242e437178e994671490c1833"] 

    adMobInterstitial.load(request) 

} 

func interstitialDidReceiveAd(_ ad: GADInterstitial) { 

    ad.present(fromRootViewController: self) 

} 

这里我注释掉initAdMobInterstitial,但是当它被注释掉的广告弹出并正常工作。这个弹出窗口会在应用第一次启动时发生。

override func viewDidLoad() { 
    super.viewDidLoad() 

    //initAdMobInterstitial() 

    initAdMobBanner() 

    NotificationCenter.default.addObserver(self, selector: #selector(self.handle(notification:)), name: NSNotification.Name(rawValue: socialNotificationName), object: nil) 

    let scene = Scene_MainMenu(size: CGSize(width: 1024, height: 768)) 
    let skView = self.view as! SKView 

    skView.isMultipleTouchEnabled = true 

    skView.ignoresSiblingOrder = true 

    scene.scaleMode = .aspectFill 

    _ = SGResolution(screenSize: view.bounds.size, canvasSize: scene.size) 

    skView.presentScene(scene) 

} 

现在,在我的一个场景中,名为GameOver,我希望广告弹出。每当场景出现时我都希望它出现,所以每次玩家输掉游戏时都会出现。使用通知中心,你可以在我的视图控制器类看,我试图发送一个通知,并将它处理...

override func didMove(to view: SKView) { 

    self.sendNotification(named: "interNotif") 

}

...通过这个功能,也是在发现视图控制器类

func handle(notification: Notification) { 

    if (notification.name == NSNotification.Name(rawValue: interstitialNotificationName)) { 

     initAdMobInterstitial() 

    } 
} 

另请注意,在我的视图控制器我已经宣布interstitialNotificationName等于字符串“interNotif”来匹配发送的通知。

+0

请分享你的一些代码。 – Evana

回答

1

加载后不要呈现GADInterstitial。您的通知功能应该呈现它。然后,一旦用户驳回另一个广告请求。例如:

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Load the ad 
    initAdMobInterstitial() 
} 

func interstitialDidReceiveAd(_ ad: GADInterstitial) { 
    // Do not present here 
    // ad.present(fromRootViewController: self) 
} 

func handle(notification: Notification) { 
    if (notification.name == NSNotification.Name(rawValue: interstitialNotificationName)) { 
     // Check if the GADInterstitial is loaded 
     if adMobInterstitial.isReady { 
      // Loaded so present it 
      adMobInterstitial.present(fromRootViewController: self) 
     } 
    } 
} 

// Called just after dismissing an interstitial and it has animated off the screen. 
func interstitialDidDismissScreen(_ ad: GADInterstitial) { 
    // Request new GADInterstitial here 
    initAdMobInterstitial() 
} 

对于GADInterstitialDelegate广告事件的完整列表,请参阅AdMob iOS Ad Events

+0

我明白你为什么这么做了,但广告仍然没有显示出来。如果我没有从那里加载它,我的didRecieveAd函数应该怎么做? – Matt