应用内购买和恢复按钮:单一产品 - 非消耗品

In-App Purchase & Restore Button : Single Product - Non-Consumable

这个问题困扰了我好几天。

我有一个简单的应用程序,可以显示横幅和插页式广告。

我正在使用单视图应用程序,有主视图控制器 (ViewController.swift) 并设置了另一个视图控制器 (InAppViewController.swift) 来处理弹出页面:

  1. 允许用户进行应用内购买以移除所有广告(广告横幅和插页式广告);或
  2. 恢复购买。

我的代码在 运行 时没有错误。

应用内购买运行没问题,但偶尔我会收到两次登录 iTunes 的请求。

但我的恢复按钮和相关功能似乎是问题所在。

我已经设置了多个沙盒测试员帐户进行测试,没有购买该应用程序的新用户能够成功恢复购买。这应该是不可能的,所以我这里肯定做错了。

这是我的代码:

主视图控制器:

//  ViewController.swift

import UIKit
import MessageUI
import Social
import iAd
import StoreKit

class ViewController: UIViewController, MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate, ADBannerViewDelegate, ADInterstitialAdDelegate
{

let defaults = NSUserDefaults.standardUserDefaults()
var product_id: NSString?;

override func viewDidLoad() {
    product_id = "some.product.id";
    super.viewDidLoad()

     //Check if product is purchased
     if (defaults.boolForKey("purchased")){
        print("already purchased")

        // Hide or show banner ads is purchased/not purchased.
        // Advertising Banner:
        self.canDisplayBannerAds = false
        }

    else if (!defaults.boolForKey("stonerPurchased")){
        print("not yet purchased")

        // Advertising Banner:
        self.canDisplayBannerAds = true
        }

这段代码似乎工作得很好。当应用程序加载时,它能够确定谁支付了移除广告的费用以及那些没有支付的费用,并适当显示广告横幅。

我在第二个视图控制器 (InAppPViewController.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() {
     super.viewDidLoad()

    // Do any additional setup after loading the view.
    }

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

  @IBAction func restorePurchases(sender: UIButton) {
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)
    SKPaymentQueue.defaultQueue().restoreCompletedTransactions()

}
  func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) {

    print("Transactions Restored")
    let alert = UIAlertView(title: "Thank You", message: "Your purchase(s) were restored.", delegate: nil, cancelButtonTitle: "OK")
    alert.show()
}


@IBAction func unlockAction(sender: AnyObject) {

product_id = "some.product.id";

    SKPaymentQueue.defaultQueue().addTransactionObserver(self)

    //Check if product is purchased

    if (defaults.boolForKey("purchased")){

    }
    else if (!defaults.boolForKey("stonerPurchased")){
        print("false")     
    }


    print("About to fetch the products");
    // We check that we are allowed 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")
                break;

            case .Failed:
                print("Purchased Failed");
                SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                break;

            case .Restored:
                print("Already Purchased");
                SKPaymentQueue.defaultQueue().restoreCompletedTransactions()


            default:
                break;
            }
        }
    }

}

}

我哪里错了?

问题:

  1. 我上面的代码正确吗?
  2. 我应该修改什么以及为什么?

提前致歉,我是这个美妙的编码世界的新手......但喜欢它的每一分钟!

我已将 InAppPViewController.swift 文件的代码修改如下:

//  InAppPViewController.swift

import UIKit
import StoreKit

class InAppPViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {

let defaults = NSUserDefaults.standardUserDefaults()
var product_id: NSString?;

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.  
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

@IBAction func restorePurchases(sender: UIButton) {
    // Set up the observer
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)


    //Check if user can make payments and then proceed to restore purchase
    if (SKPaymentQueue.canMakePayments()) {
        SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
    }

}

@IBAction func unlockAction(sender: AnyObject) {

    product_id = "some.product.id";
    // Adding the observer
    SKPaymentQueue.defaultQueue().addTransactionObserver(self)

    //Check if product is purchased
    if (defaults.boolForKey("purchased")){
        print("User already purchased this")
        // Hide a view or show content depends on your requirement
    }

    else if (!defaults.boolForKey("Purchased")){
        print("User has not yet pur hased this") 
    }


    print("About to fetch the products");

    // Check if user can make payments and then proceed 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("User can make purchases and will fetch products from Apple Store now");
    }else{
        print("User can't make purchases");
    }

}

func buyProduct(product: SKProduct){
    print("Sending the Payment Request to Apple");
    let payment = SKPayment(product: product)
    SKPaymentQueue.defaultQueue().addPayment(payment);

}

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");
}

// Allowing for all possible outcomes:
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")
                let alert = UIAlertView(title: "Thank You", message: "Thank you for your purchase!", delegate: nil, cancelButtonTitle: "OK")
                alert.show();
                SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                defaults.setBool(true , forKey: "purchased")
                break;

            case .Failed:
                print("Purchased Failed");
                SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
                break;

            case .Restored:
                print("Already Purchased");
                SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
                break;

            default:
                break;
            }
        }
    }

}

}

我保留了 ViewController.swift 文件。

产品购买现在似乎可以使用了。

但是关于恢复购买,我可以运行我的物理设备上的代码,但是不能测试恢复购买功能。

我遇到了 previous 恢复仍未解决并在系统中循环的购买。我无法手动清除我的 SKPaymentsQueue。因此我的代码拒绝接受更多新的恢复购买请求。

  1. SKPaymentQueue.defaultQueue().addTransactionObserver(self) 应该在 viewDidLoad() 中而不是在 restore func
  2. 你可以这样写SKPaymentQueue.defaultQueue().restoreCompletedTransactions() override func viewWillDisappear(animated: Bool) { SKPaymentQueue.defaultQueue().removeTransactionObserver(self) }
  3. 沙盒登录两次是正常的。

希望我有所帮助。