I have a simple single view application project - a one (1) page app that displays some random text to the user.
I have successfully incorporated Ad Banners and Interstitial Ads.
I have set up another view controller (InAppViewController.swift
) to handle a pop-up page that allows the user to make an in-app purchase to remove all ads (AdBanners & InterstitialAds).
In my second view controller (InAppViewController.swift
), I have the following code:
AMENDED CODE:
// InAppPViewController.swift
import UIKit
import StoreKit
import iAd
class InAppPViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {
let defaults = NSUserDefaults.standardUserDefaults()
var product_id: NSString?;
@IBOutlet weak var unlockAction: UIButton!
@IBOutlet var adBannerView: ADBannerView?
override func viewDidLoad() {
product_id = "holymoly.iap.removeads";
super.viewDidLoad()
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
//Check if product is purchased
if (defaults.boolForKey("purchased")){
self.adBannerView?.hidden = true
}
else if (!defaults.boolForKey("stonerPurchased")){
print("false")
self.adBannerView?.hidden = false
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func unlockAction(sender: AnyObject) {
print("About to fetch the products");
// We check that we are allow to make the purchase.
if (SKPaymentQueue.canMakePayments())
{
let productID:NSSet = NSSet(object: self.product_id!);
let productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>);
productsRequest.delegate = self;
productsRequest.start();
print("Fething Products");
}else{
print("can't make purchases");
}
}
func buyProduct(product: SKProduct){
print("Sending the Payment Request to Apple");
let payment = SKPayment(product: product)
SKPaymentQueue.defaultQueue().addPayment(payment);
}
//Delegate Methods for IAP
func productsRequest (request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
let count : Int = response.products.count
if (count>0) {
let validProduct: SKProduct = response.products[0] as SKProduct
if (validProduct.productIdentifier == self.product_id) {
print(validProduct.localizedTitle)
print(validProduct.localizedDescription)
print(validProduct.price)
buyProduct(validProduct);
} else {
print(validProduct.productIdentifier)
}
} else {
print("nothing")
}
}
func request(request: SKRequest, didFailWithError error: NSError) {
print("Error Fetching product information");
}
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print("Received Payment Transaction Response from Apple");
for transaction:AnyObject in transactions {
if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
switch trans.transactionState {
case .Purchased:
print("Product Purchased");
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
defaults.setBool(true , forKey: "purchased")
self.adBannerView?.hidden = true
break;
case .Failed:
print("Purchased Failed");
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
self.adBannerView?.hidden = false
break;
case .Restored:
print("Already Purchased");
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
default:
self.adBannerView?.hidden = true
break;
}
}
}
}
}
And in my 'original' view controller (ViewController.swift
) I have added this code:
// ViewController.swift
import UIKit
import MessageUI
import Social
import iAd
class ViewController: UIViewController, MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate, ADBannerViewDelegate, ADInterstitialAdDelegate
{
var interstitialAd:ADInterstitialAd!
var interstitialAdView: UIView = UIView()
@IBOutlet var adBannerView: ADBannerView?
@IBAction func someFunkyButton(sender: AnyObject) {
//Interstitial Ad:
let rand = Int(arc4random_uniform(4))
print(rand)
let adNo = 2
if(adNo == rand)
{
loadInterstitialAd()
}
}
let defaults = NSUserDefaults.standardUserDefaults()
override func viewDidLoad() {
super.viewDidLoad()
//Check if product is purchased
if (defaults.boolForKey("purchased")){
// Advertising Banner:
self.canDisplayBannerAds = true
self.adBannerView?.delegate = self
self.adBannerView?.hidden = true
}
else if (!defaults.boolForKey("stonerPurchased")){
print("false")
// Advertising Banner:
self.canDisplayBannerAds = true
self.adBannerView?.delegate = self
self.adBannerView?.hidden = false
}
The code shows as error-free.
It runs on my actual iPhone (simulator) and the in-app purchases work.
But the ad banners still show. I'm trying to show the ad banners with:
(i) In ViewController.swift:
self.canDisplayBannerAds = true
self.adBannerView?.delegate = self
self.adBannerView?.hidden = false
(ii) In InAppViewController.swift:
self.adBannerView?.hidden = false
and not show the ad banners with:
(i) In ViewController.swift:
self.canDisplayBannerAds = true
self.adBannerView?.delegate = self
self.adBannerView?.hidden = true
(ii) In InAppViewController.swift:
self.adBannerView?.hidden = true
but it's obviously not working.
Questions:
How can I amend my code and stop these ad banners from showing?
How can I also stop my interstitial ads from showing?
I feel I'm close, very close ... but that cigar is yet to land!
If you're implementing your own ADBannerView
then you need to remove self.canDisplayBannerAds = true
.
self.canDisplayBannerAds = true
can be used for a no hassle way of implementing iAd banners in your application. This will create an ADBannerView
for you and show or hide the ADBannerView
on the bottom of your view depending on whether it receives an ad or not from the iAd network.
You either implement your own ADBannerView
or use self.canDisplayBannerAds = true
, not both.