应用内购买:删除广告横幅和插页式广告 - 代码帮助

In-App Purchase : Remove Ad Banners & Interstitial Ads - Code Assistance

我有一个简单的单视图应用程序项目 - 一个向用户显示一些随机文本的一 (1) 页应用程序。

我已成功整合广告横幅和插页式广告。

我已经设置了另一个视图控制器 (InAppViewController.swift) 来处理允许用户进行应用内购买以删除所有广告(广告横幅和插播广告)的弹出页面。

在我的第二个视图控制器 (InAppViewController.swift) 中,我有以下代码:

修改后的代码:

//  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;
            }
        }
    }
    
}

}

并且在我的 'original' 视图控制器 (ViewController.swift) 中添加了以下代码:

//  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     
    }
    

代码显示没有错误。

它在我的实际 iPhone(模拟器)上运行,并且应用内购买有效。

但广告横幅仍然显示。我正在尝试 显示 广告横幅:

(i) 在 ViewController.swift 中:

        self.canDisplayBannerAds = true
        self.adBannerView?.delegate = self
        self.adBannerView?.hidden = false

(ii) InAppViewController.swift:

        self.adBannerView?.hidden = false

不显示广告横幅:

(i) 在 ViewController.swift 中:

        self.canDisplayBannerAds = true
        self.adBannerView?.delegate = self
        self.adBannerView?.hidden = true

(ii) InAppViewController.swift:

        self.adBannerView?.hidden = true

但是显然不行。

问题:

  1. 如何修改我的代码并停止显示这些广告横幅?

  2. 我如何停止展示我的插页式广告?

    我觉得我很接近,非常接近...但是那支雪茄还没有落地!

首先 removeAds 在里面 btnRemoveAds 所以把它移到外面。其次,一种方法是将已购买产品的产品标识符保存在 UserDefaults 中,这样在应用程序退出和启动时,广告将不会出现,因为用户已经购买了 inapp。因此,您将在 viewDidLoad 上做的是检查产品标识符是否已购买(来自 NSUserDefaults,如果早先购买,则会设置),如果已购买,则从一开始就禁用广告,否则显示广告.购买商品后,在 NSUserDefaults 中设置状态并相应地更新 UI。

如果您要实现自己的 ADBannerView,则需要删除 self.canDisplayBannerAds = true

self.canDisplayBannerAds = true 可用于在您的应用程序中轻松实现 iAd 横幅。这将为您创建一个 ADBannerView,并根据它是否从 iAd 网络收到广告来显示或隐藏视图底部的 ADBannerView

您可以实现自己的 ADBannerView 或使用 self.canDisplayBannerAds = true,但不能同时使用。