How to implement Interstitial iAds in Swift (Xcode 6.1)

I'm trying to figure out how to switch from iAds to iSds to interstitial iAds to make room for a tabbed controller. For some reason, I can’t find any resource even to get started with these fast ads.

Can someone please give me some information about interstitial iAds with Swift and how I can implement them in a project.

+4
source share
4 answers

It’s relatively cleaner and easier to follow the Interstitial Ads implementation method, since this method does not require the use of NSNotificationCentre

 import UIKit import iAd class ViewController: UIViewController, ADInterstitialAdDelegate { var interstitialAd:ADInterstitialAd! var interstitialAdView: UIView = UIView() override func viewDidLoad() { super.viewDidLoad() loadInterstitialAd() } func loadInterstitialAd() { interstitialAd = ADInterstitialAd() interstitialAd.delegate = self } func interstitialAdWillLoad(interstitialAd: ADInterstitialAd!) { } func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) { interstitialAdView = UIView() interstitialAdView.frame = self.view.bounds view.addSubview(interstitialAdView) interstitialAd.presentInView(interstitialAdView) UIViewController.prepareInterstitialAds() } func interstitialAdActionDidFinish(interstitialAd: ADInterstitialAd!) { interstitialAdView.removeFromSuperview() } func interstitialAdActionShouldBegin(interstitialAd: ADInterstitialAd!, willLeaveApplication willLeave: Bool) -> Bool { return true } func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) { } func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) { interstitialAdView.removeFromSuperview() } 

And this can help if you put println("Function Name") in each function just to track the process of interstitial ads. If you have any questions or a way to improve this block of code, please leave a comment. Thank you.

+17
source

This is how I use in my project

// add iAd infrastructure

  import iAd 

// compatible iAd delegate

 class ViewController: UIViewController,ADInterstitialAdDelegate //create instance variable var interstitial:ADInterstitialAd! 

// standard iAd cross-site ads do not provide a close button, so we need to create one manually

 var placeHolderView:UIView! var closeButton:UIButton! override func viewDidLoad() { super.viewDidLoad() //iAD interstitial NSNotificationCenter.defaultCenter().addObserver(self, selector: ("runAd:"), name:UIApplicationWillEnterForegroundNotification, object: nil) } //iAD interstitial func runAd(notification:NSNotification){ var timer = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self, selector: Selector("dislayiAdInterstitial"), userInfo: nil, repeats: false) cycleInterstitial() } func cycleInterstitial(){ // Clean up the old interstitial... // interstitial.delegate = nil; // and create a new interstitial. We set the delegate so that we can be notified of when interstitial = ADInterstitialAd() interstitial.delegate = self; } func presentInterlude(){ // If the interstitial managed to load, then we'll present it now. if (interstitial.loaded) { placeHolderView = UIView(frame: self.view.frame) self.view.addSubview(placeHolderView) closeButton = UIButton(frame: CGRect(x: 270, y: 25, width: 25, height: 25)) closeButton.setBackgroundImage(UIImage(named: "error"), forState: UIControlState.Normal) closeButton.addTarget(self, action: Selector("close"), forControlEvents: UIControlEvents.TouchDown) self.view.addSubview(closeButton) interstitial.presentInView(placeHolderView) } } // iAd Delegate Mehtods // When this method is invoked, the application should remove the view from the screen and tear it down. // The content will be unloaded shortly after this method is called and no new content will be loaded in that view. // This may occur either when the user dismisses the interstitial view via the dismiss button or // if the content in the view has expired. func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!){ placeHolderView.removeFromSuperview() closeButton.removeFromSuperview() interstitial = nil cycleInterstitial() } func interstitialAdActionDidFinish(_interstitialAd: ADInterstitialAd!){ placeHolderView.removeFromSuperview() closeButton.removeFromSuperview() interstitial = nil println("called just before dismissing - action finished") } // This method will be invoked when an error has occurred attempting to get advertisement content. // The ADError enum lists the possible error codes. func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!){ cycleInterstitial() } //Load iAd interstitial func dislayiAdInterstitial() { //iAd interstitial presentInterlude() } func close() { placeHolderView.removeFromSuperview() closeButton.removeFromSuperview() interstitial = nil } 
+3
source

I know it's old - but I just stumbled upon

 override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { let destination = segue.destinationViewController as UIViewController destination.interstitialPresentationPolicy = ADInterstitialPresentationPolicy.Automatic } 
+1
source

If you add this to your application delegate, you will avoid this 3 second delay mentioned above.

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { UIViewController.prepareInterstitialAds() return true } 
+1
source

All Articles