(Swift)应用程序内购买 - SKProductResquest为0(零)

时间:2016-11-21 13:11:58

标签: ios swift in-app-purchase swift3 storekit

下午好。

Obs:我使用Rechiability类检查用户互联网是否已连接,但在这种情况下,该类验证互联网,但我没有收到来自apple的信息。 所以我知道这是一个unwrapping nil值,因为在productRequest方法中,response.products没有收到任何产品,但只是在下面的情况下。

我创建了一个支付系统并且工作正常,但是有一种情况是它崩溃了:

1用户在没有互联网连接(Wi-Fi或4G)的情况下进入应用程序。

2用户尝试离线购买应用并进入购买视图控制器。

3按iphone HOME按钮,然后连接Wi-Fi或4G。

4返回应用程序并按下按钮再次购买并出现崩溃。

这只发生在这种情况下,在其他测试案例中我没有收到任何错误。

我看不出为什么会发生这种错误。

以下是我的错误代码和图片。

我目前的代码:

import UIKit
import StoreKit

protocol IAPManagerDelegate 
{
    func managerDidRestorePurchases()
}

class IAPManager: NSObject, SKProductsRequestDelegate,     SKPaymentTransactionObserver, SKRequestDelegate 
{

static let sharedInstance = IAPManager()

var request:SKProductsRequest!
var products:NSArray!

//Load product identifiers for store usage
func setupInAppPurchases()
{
    self.validateProductIdentifiers(self.getProductIdentifiersFromMainBundle())

    SKPaymentQueue.default().add(self)
}

//Get product identifiers
func getProductIdentifiersFromMainBundle() -> NSArray 
{
    var identifiers = NSArray()
    if let url = Bundle.main.url(forResource: "iap_product_ids", withExtension: "plist")
    {
        identifiers = NSArray(contentsOf: url)!
    }

    return identifiers
}

//Retrieve product information
func validateProductIdentifiers(_ identifiers:NSArray) 
{

    if Reachability.isConnectedToNetwork() == true
    {
        print("Enter")
        let productIdentifiers = NSSet(array: identifiers as [AnyObject])
        let productRequest = SKProductsRequest(productIdentifiers: productIdentifiers as! Set<String>)
        self.request = productRequest
        productRequest.delegate = self
        productRequest.start()
    }

}

func createPaymentRequestForProduct(_ product:SKProduct)
{

    if Reachability.isConnectedToNetwork() == true
    {
        let payment = SKMutablePayment(product: product)
        payment.quantity = 1
        SKPaymentQueue.default().add(payment)
    }
}


//MARK: SKProductsRequestDelegate 
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) 
{
    //
    self.products = response.products as NSArray!

    for product in products
    {
        let temp = product as! SKProduct

        if temp.productIdentifier == "monthly.subscription"
        {
            print("price: \(temp.price)")
            formatProductMonth(free: temp)
        }
        if temp.productIdentifier == "weekly.subscription"
        {
            print("price: \(temp.price)")
            formatProductFree(free: temp)
        }
        if temp.productIdentifier == "yearly.subscription"
        {
            print("price: \(temp.price)")
            formatProductYear(free: temp)
        }
    }
    // print("Product[0]: \(string)")
    // print("Product[1]: \(prod1.productIdentifier)")
    // print("Product[2]: \(prod2.productIdentifier)")
}

//MARK: SKPaymentTransactionObserver Delegate Protocol
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) 
{


    //
    for transaction in transactions as [SKPaymentTransaction]
    {
        switch transaction.transactionState
        {
            case .purchasing:
            print("Purchasing")
            UIApplication.shared.isNetworkActivityIndicatorVisible = true
            break
            case .deferred:
            print("Deferrred")

            let alertController: UIAlertController = UIAlertController(title: "Deferred", message: "Purchase deferred", preferredStyle: .alert)
            let dismiss = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
            alertController.addAction(dismiss)
            alertController.show()
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            break
            case .failed:
            print("Failed")
            //print(transaction.error?.localizedDescription)
            UIApplication.shared.isNetworkActivityIndicatorVisible = false
            SKPaymentQueue.default().finishTransaction(transaction)
            let alertController: UIAlertController = UIAlertController(title: "Failed", message: "Purchase failed", preferredStyle: .alert)
            let dismiss = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
            alertController.addAction(dismiss)
            alertController.show()
            //UIApplication.shared.keyWindow?.rootViewController?.present(alertController, animated: true, completion: nil)
            break
            case.purchased:
            StopActivator()
            print("Purchased")
            self.verifyReceipt(transaction)
            let thankyou = UserDefaults.standard.bool(forKey: "Purchased")

            if thankyou == true
            {

                let alertController: UIAlertController = UIAlertController(title: "Thank You", message: "Purchase completed", preferredStyle: .alert)
                let dismiss = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: { (action: UIAlertAction!) in
                UIApplication.shared.keyWindow?.rootViewController?.dismiss(animated: true, completion: nil)
                })
                alertController.addAction(dismiss)
                alertController.show()

            }

            break
            case .restored:
            print("Restored")
            let alertController: UIAlertController = UIAlertController(title: "Restore Success", message: "Your purchases have been restored", preferredStyle: .alert)
            let dismiss = UIAlertAction(title: "OK", style: UIAlertActionStyle.cancel, handler: nil)
            alertController.addAction(dismiss)
            alertController.show()
            break

        }
    }
}

上述错误的图片:

First Image

Second Image

请知道什么是解包可选值“Obs:我使用Rechiability类来检查用户互联网是否已连接,但在这种情况下,该类验证互联网,但我没有收到来自苹果的信息。所以我知道这是给出unwrapping nil值,因为在productRequest方法中,response.products没有收到任何产品,但只有在下面的情况下。“

我的问题是为什么在这种情况下会出现这个错误:“我创建了一个支付系统并且工作正常,但有一种情况是崩溃的情况是:1用户输入没有互联网连接的应用程序(Wi-Fi或4G).2用户试图离线购买应用程序并进入购买视图控制器.3按iphone HOME按钮,然后连接Wi-Fi或4G.4返回应用程序后再按按钮再次购买和崩溃出现。“

0 个答案:

没有答案