didFailToReceiveAdWithError function of iad is not working
You're probably not setting your ADBannerView
's delegate
properly. Your code should look similar to this:
class ViewController: UIViewController, ADBannerViewDelegate { // Include the delegate for our ADBannerView
And then wherever you are setting up your ADBannerView
you need to set its delegate
. For example:
yourAdBannerView.delegate = self
You will probably want to print
your error
also so you know why it has failed. For example:
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
println("didFailToReceiveAdWithError: \(error)")
}
iAds (even iAd suite form apple's examples) stopped showing up on my iPhone, yet they run on the simulator
I checked today and it seems to be working fine! I have been researching for the past few days, there are lots of people having the same problem for the last few days(from all over the world). So I guess, it was a problem from Apple's side while giving out iAds.
Interstitial iAds in SpriteKit?
You can make use of my helper I posted on github. Its was made specifically for spritekit and you can call ads from anywhere without delegates etc or changing view controllers.
https://github.com/crashoverride777/Swift-2-iAds-and-AdMob-Helper
I think having a look at that helper should give you a good idea of where to start. Here is a cut down example of how it could look just for iAds Inter ads.
This is in swift so I am not sure if it is still helpful for you.
import iAd
class Ads: NSObject {
// MARK: - Static Properties
/// Shared instance
static let sharedInstance = Ads()
// MARK: - Properties
/// Presenting view controller
var presentingViewController: UIViewController!
/// iAd inter ad
private var iAdInterAd: ADInterstitialAd?
/// iAd inter ad view
private var iAdInterAdView = UIView()
/// iAd inter ad close button
private var iAdInterAdCloseButton = UIButton(type: UIButtonType.System)
// MARK: - Init
private override init() {
super.init()
print("Ads helper init")
iAdInterAd = iAdLoadInterAd()
}
// MARK: - User Methods
/// Show inter ad
func showInterAd() {
iAdShowInterAd()
}
/// Show inter ad randomly (33% chance)
func showInterAdRandomly() {
let randomInterAd = Int(arc4random() % 3)
print("randomInterAd = \(randomInterAd)")
if randomInterAd == 1 {
iAdShowInterAd()
}
}
/// Remove all ads
func removeAllAds() {
print("Removed all ads")
if iAdInterAd != nil {
iAdInterAd!.delegate = nil
iAdInterAdCloseButton.removeFromSuperview()
iAdInterAdView.removeFromSuperview()
}
}
// MARK: - Internal Methods
/// iAd load inter ad
private func iAdLoadInterAd() -> ADInterstitialAd {
print("iAd inter ad loading...")
let iAdInterAd = ADInterstitialAd()
iAdInterAd.delegate = self
if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
iAdInterAdCloseButton.frame = CGRectMake(18, 18, 27, 27)
} else {
iAdInterAdCloseButton.frame = CGRectMake(13, 13, 22, 22)
}
iAdInterAdCloseButton.layer.cornerRadius = 11
iAdInterAdCloseButton.setTitle("X", forState: .Normal)
iAdInterAdCloseButton.setTitleColor(UIColor.grayColor(), forState: .Normal)
iAdInterAdCloseButton.backgroundColor = UIColor.whiteColor()
iAdInterAdCloseButton.layer.borderColor = UIColor.grayColor().CGColor
iAdInterAdCloseButton.layer.borderWidth = 2
iAdInterAdCloseButton.addTarget(self, action: "iAdPressedInterAdCloseButton:", forControlEvents: UIControlEvents.TouchDown)
return iAdInterAd
}
/// iAd show inter ad
private func iAdShowInterAd() {
guard iAdInterAd != nil else {
print("iAd inter is nil, reloading")
iAdInterAd = iAdLoadInterAd()
return
}
if iAdInterAd!.loaded {
print("iAd inter showing")
iAdInterAdView.frame = presentingViewController.view.bounds
presentingViewController.view.addSubview(iAdInterAdView)
iAdInterAd!.presentInView(iAdInterAdView)
UIViewController.prepareInterstitialAds()
iAdInterAdView.addSubview(iAdInterAdCloseButton)
//pauseTasks() // not really needed for inter as you tend to show them when not playing.
} else {
print("iAd inter not ready, reloading again...")
iAdInterAd = iAdLoadInterAd()
}
}
/// iAd inter ad pressed close button
func iAdPressedInterAdCloseButton(sender: UIButton) { // dont make private as its called with a selector
print("iAd inter closed")
iAdInterAd!.delegate = nil
iAdInterAdCloseButton.removeFromSuperview()
iAdInterAdView.removeFromSuperview()
iAdInterAd = iAdLoadInterAd()
//resumeTasks() // not really needed for inter as you tend to not show them during gameplay
}
/// Pause tasks in the app/game
private func pauseTasks() {
// Pause app/game, music etc here.
// you could use NSNotifactionCenter or Delegates to call methods in other SKScenes / ViewControllers
}
/// Resume tasks in the app/game
private func resumeTasks() {
// Resume app/game, music etc here.
// you could use NSNotifactionCenter or Delegates to call methods in other SKScenes / ViewControllers
}
}
// MARK: - Delegates iAd Inter
extension Ads: ADInterstitialAdDelegate {
func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
print("iAd inter did load")
}
func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
print("iAd inter did unload")
}
func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
print("iAd inter error \(error)")
iAdInterAd!.delegate = nil
iAdInterAdCloseButton.removeFromSuperview()
iAdInterAdView.removeFromSuperview()
}
}
Now you simply call
Ads.sharedInstance.presentingViewController = self
in your GameViewController before doing anything else. This will init the helper and preload the first inter ad.
Than you simply call these anywhere you would like inter ads to show
GameData.sharedInstance.showInterAd()
or
GameData.sharedInstance.showInterAdRandomly() // 33% chance for ad
Are Interstitial ads broken? iOS 8?
Are you running on the iPhone 4s? If so try switching to 5/6 in the simulator or in real life and it works!
Easy to replicate.
I've logged a bug with Apple.
The other issue I've faced in the past, especially for new iTunesConnect accounts, is that you need to complete all of the outstanding banking and iAd contracts in iTunes Connect, even for a free App.
Also, sometimes ads aren't served to the simulator (try restarting the app).
Also, if you look at Settings in the Simulator you can change the fill-rate of ads and if infinite ads are provided (and more).
All can be contributing factors.
Related Topics
Pure Swiftui Login, Signup, Register Flow, Is It Possible
Swiftui Present View Modally via Tabview
Storyboard Localization in Swift 4.0
iOS - Setobject VS. Synchronize
Displaying Strings in iOS Randomly Without Repeating Them
Generate a Hmac Swift Sdk8.3 Using Cchmac()
Xcode 6 Ignoring Breakpoints - Swift
Nsexceptionallowsinsecurehttploads Not Working for Ip Addresses
Swift3 - How to Protect Secret Key
Get "No Keychain Available" Error When Try to Access Keychain from App Extension
How to Convert Base64 into Nsdata in Swift
Nsclassfromstring Returning Nil for Nested Class
Keep a View Always on Top (Don't Scroll with Keyboard) in Iqkeyboardmanager