I've made a game using SpriteBuilder and it's written in Swift. I'd like to add advertisements before I ship an update to the App Store. Can someone please help me with how to do this?
-
You should show some effort by showing what you tried to do. – Devapploper Aug 14 '15 at 14:48
2 Answers
It's fairly easy.
1) Import the iAd framework >Build Phases> Link Binary With Libraries> find iAd
2) drag an iAd BannerView from the object library
3) Make an outlet connecting the iAd BannerView to your VC file
4) Add the delegate to your VC class
import iAd
class ViewController: UIViewController, ADBannerViewDelegate{
@IBOutlet var adBannerView: ADBannerView?
override func viewDidLoad(){
self.canDisplayBannerAds = true
self.ADBannerView?.delegate = self
self.ADBannerView.hidden = true
}
func bannerViewWillLoadAd(banner: AdBannerView!){ // any addition set up you want
}
func bannerViewDidLoadAd(banner: ADBannerView!){
self.ADBannerView?.hidden = false
}
func bannerViewActionDidFinish(banner: ADBannerView!){// any addition set up you want
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Boll) -> Bool {
return true
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!){
self.adBannerView?.hidden = false
}
}
Of course you'll have to adjust to display when ever and where you wan it.

- 818
- 5
- 12
-
Do not use `self.canDisplayBannerAds = true` and create your own `ADBannerView`. You will be creating two banner ads. `self.canDisplayBannerAds = true` creates and manages an `ADBannerView` for you. You either use `self.canDisplayBannerAds = true` or implement your own `ADBannerView` **not both**. – Daniel Storm Aug 11 '15 at 15:11
To add iAd to your game, you need to add the iAd framework to your app (via Build Phases) and add the import statement import iAd in the class that handles your iAd implementation.
The simplest iAd ad to add to your Swift game is a banner ad:
override func viewDidLoad() {
super.viewDidLoad()
self.canDisplayBannerAds = true
}
Make sure to import iAd and add the AdBannerViewDelegate protocol to your class declaration:
import iAd
class MasterViewController: UITableViewController, NSFetchedResultsControllerDelegate,ADBannerViewDelegate
Then you need to create the actual banner view, set the delegate (in ViewDidLoad) and add two delegate methods, so that you know when to add/remove the advertisement banner from the view:
bannerViewDidLoadAd:
bannerView:didFailToReceiveAdWithError:

- 3,227
- 12
- 38
- 42
-
Do not use `self.canDisplayBannerAds = true` and create your own `ADBannerView`. You will be creating two banner ads. `self.canDisplayBannerAds = true` creates and manages an `ADBannerView` for you. You either use `self.canDisplayBannerAds = true` or implement your own `ADBannerView` **not both**. – Daniel Storm Aug 11 '15 at 15:10