Search code examples
swiftdelayiadinterstitial

Interstitial iAd delayed after pressing button


I'm trying to setup iAd after clicking cancel button on JSSAlert. I have in JSSAlert function that set alpha for full view 0.7.. And in view controller I have function for iAd and set back alpha to 1.0...

    override func viewDidLoad() {
        super.viewDidLoad()
        self.navigationItem.title = "Vyhodnotenie testu"
        self.showAlert()

    }


    func showAlert() {
        func callback(){}
        if numberOfPoints > 49 {
            let customIcon = UIImage(named: "smile")
            let alertview = JSSAlertView().show(self, title: "Gratulujeme! Uspeli ste.", text: "Dokončili ste test s počtom bodov \(numberOfPoints + 1) z \(maximumNumberOfPoints)!", buttonText: "OK!", color: UIColorFromHex(0x22c411, alpha: 1), iconImage: customIcon)
            alertview.setTextTheme(.Light)
            alertview.addAction(myCancelCallback)
            self.navigationController?.navigationBar.alpha = 0.7
        } else {
            let customIcon = UIImage(named: "sad")
            let alertview = JSSAlertView().show(self, title: "Ľutujeme! Neuspeli ste.", text: "Dokončili ste test s počtom bodov \(numberOfPoints + 1) z \(maximumNumberOfPoints)!", buttonText: "OK!", color: UIColorFromHex(0xd20606, alpha: 1), iconImage: customIcon)
            alertview.addAction(myCancelCallback)
            alertview.setTextTheme(.Light)
            self.navigationController?.navigationBar.alpha = 0.7
        }

    }

    func myCancelCallback() {
        self.navigationController?.navigationBar.alpha = 1.0
        self.interstitialPresentationPolicy = ADInterstitialPresentationPolicy.Automatic
    }


    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(var interstitialAd: ADInterstitialAd!) {
        interstitialAd = nil
        interstitialAdView.removeFromSuperview()
    }

    func interstitialAdActionShouldBegin(interstitialAd: ADInterstitialAd!, willLeaveApplication willLeave: Bool) -> Bool {
        return true
    }

    func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {

    }

    func interstitialAdDidUnload(var interstitialAd: ADInterstitialAd!) {
        interstitialAd = nil
        interstitialAdView.removeFromSuperview()
    }

Alpha back to 1.0 in function myCancelCallback is working but iAd is delayed.. What can cause that delay? Or how can I deal with it?

I want to show iAd immediately after pressing OK!.

Video how it's working:

https://www.youtube.com/watch?v=r6LKN-cjaz8&feature=youtu.be


Solution

  • Update:

    iAd App Network Shutdown As of December 31, 2016, the iAd App Network is no longer available. If you'd like to promote your apps, you can advertise using Search Ads, Apple News, or third party networks and advertising sellers.

    Reference: https://developer.apple.com/support/iad/

    Here is what your gonna do, you have to create the interstitial including close button programmatically , i just made you a sample :

    Add row in info.plist : View controller-based status bar appearance -> NO

    in AppDelegate didFinishLaunchingWithOptions method add the following line to insure that status bar will be not hidden :

    UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.None)
    

    Here is a full sample view controller of how your gonna add the interstitial programmatically , but you only need to write animation when the interstitial is showing instead of just using addSubView. you can use animate transform translation you will find a lot of samples about that.

    import UIKit
    import iAd
    
    class ViewController: UIViewController, ADInterstitialAdDelegate {
        
        
        var interstitialAd:ADInterstitialAd!
        var interstitialAdView: UIView = UIView()
        var closeButton:UIButton!
        
       
        override func viewDidLoad() {
            super.viewDidLoad()
            
            NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "loadInterstitialAd", userInfo: nil, repeats: false)
        }
        
    
        
        func loadInterstitialAd() {
            interstitialAd = ADInterstitialAd()
            interstitialAd.delegate = self
        }
        
        func interstitialAdWillLoad(interstitialAd: ADInterstitialAd!) {
            print("interstitialAdWillLoad")
        }
        
        func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
            UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.Fade)
    
            print("interstitialAdDidLoad")
            UIApplication.sharedApplication().statusBarHidden = true
            interstitialAdView = UIView()
            interstitialAdView.frame = self.view.bounds
            
            self.navigationController?.navigationBar.addSubview(interstitialAdView)
            
            closeButton = UIButton(frame: CGRect(x: 15, y:  15, width: 20, height: 20))
            //add a cross shaped graphics into your project to use as close button
            closeButton.setBackgroundImage(UIImage(named: "close"), forState: UIControlState.Normal)
            closeButton.addTarget(self, action: Selector("close"), forControlEvents: UIControlEvents.TouchDown)
            
            self.navigationController?.navigationBar.addSubview(closeButton)
            
            interstitialAd.presentInView(interstitialAdView)
            UIViewController.prepareInterstitialAds()
        }
        
        func close() {
            interstitialAdView.removeFromSuperview()
            closeButton.removeFromSuperview()
            interstitialAd = nil
            UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.Fade)
    
            
        }
        
        func interstitialAdActionDidFinish(interstitialAd: ADInterstitialAd!) {
            print("interstitialAdActionDidFinish")
            UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.Fade)
        }
    
        
        func interstitialAdActionShouldBegin(interstitialAd: ADInterstitialAd!, willLeaveApplication willLeave: Bool) -> Bool {
            return true
        }
        
        func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
            print("didFailWithError")
        }
        
        func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
            print("interstitialAdDidUnload")
            close()
        }
    
        
        
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Release any cached data, images, etc that aren't in use.
        }
    
    }
    

    Update: It might be you just have to call : UIViewController.prepareInterstitialAds() in viewDidLoad of your class to download the content of the interstitial so whenever you ask to present it will be ready and then might be no delay.