Sprite Kit Create an iAdBanner
iAd has been discontinued, you will have to choose another ad provider.
From Apple's site:
The iAd App Network will be discontinued as of June 30, 2016. Although we are no longer accepting new apps into the network, advertising campaigns may continue to run and you can still earn advertising revenue until June 30.
Implementing iAd in Sprite Kit with Swift
Code with AutoLayout(code in viewDidLoad()
):
let bannerAd = ADBannerView(adType: ADAdType.Banner)
bannerAd.delegate = self
bannerAd.setTranslatesAutoresizingMaskIntoConstraints(false)
self.view.addSubview(bannerAd)
let constraintsH = NSLayoutConstraint.constraintsWithVisualFormat("|[bannerAd]|", options: nil, metrics: nil, views: ["bannerAd":bannerAd])
let constraintsV = NSLayoutConstraint.constraintsWithVisualFormat("V:[bannerAd(50)]|", options: nil, metrics: nil, views: ["bannerAd":bannerAd])
self.view.addConstraints(constraintsH)
self.view.addConstraints(constraintsV)
Don't forget to implement delegate methods.
Swift SpriteKit iAd
If you simply want to display an iAd banner then you'll need to do several things. First, import the iAd framework and import iAd the the top of your code. Then, use this function to display the banner. (oh, and adBannerView should be declared as a global variables within your skscene).
func loadAds()->ADBannerView{
adBannerView = ADBannerView(frame: CGRect.zeroRect)
adBannerView.center = CGPoint(x: adBannerView.center.x, y: view!.frame.size.height - adBannerView.frame.size.height / 2)
adBannerView.delegate = self
self.view?.addSubview(adBannerView)
return adBannerView
}
You may also want to include these functions. This one right here runs when the banner cannot load (this will most likely occur due to a network problem).
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
println("Ad cannot load")
self.adBannerView.hidden = true
}
This runs when the banner successfully loads.
func bannerViewDidLoadAd(banner: ADBannerView!) {
println("ad did load.")
self.adBannerView.hidden = false
}
How to stop Sprite Kit from reinitializing the scene with iAd banner
Thanks for the response. That was the problem. I had been initializing the scene in the viewWillLayoutSubviews
method in my root view controller. I moved the scene initialization to the viewDidLoad
method and it fixed the issue.
Two scenes at the same time in SpriteKit
No don't do that. Even if possible it is meaningless.
In a such case I use storyboard. Created menu at top of the screen and give the rest of the screen to SKView
. By this way you can control your SKScene
easily also you can use UIKit for you menu which is more advanced than SpriteKit ui elements.
Also you can use one scene and create menu nodes with SpriteKit utilities.
Related Topics
No Such File and Directory Found Xcode 7
Disable the Interactive Dismissal of Presented View Controller
Differencebetween the App Id and the Bundle Id? Where Is the App Id in the Xcode Project
Bringing a Subview to Be in Front of All Other Views
Any Way to Print in Color with Nslog
Nsuserdefaults Unreliable in iOS 8
Xcode Instruments Allocations: Look at Live Bytes or Overall Bytes
How to Change the Highlighted Color of a Uibutton
Xcode 8: Preparing Archive Takes Forever
How to Only Show Bottom Border of Uitextfield in Swift
Return Lighter Color from Skcolor Using Hsl Lightness Factor
Over the Air (Ota) iOS IPA File Distribution for Public
Convert Existing iOS Paid App to Freemium Model with In-App Purchase
How to Get the Size of a Scaled Uiimage in Uiimageview
Popping and Pushing View Controllers in Same Action
Accessing the Host App Code from the Xcode 7 UI Test Target
Warning: Attempt to Present Viewcontroller Whose View Is Not in the Window Hierarchy