(Swift)应用内购买Wi-Fi错误

时间:2016-11-20 20:19:28

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

下午好,

Obs:我使用Rechibility类来检查用户互联网是否已连接,但在这种情况下,该类验证互联网,但我没有收到来自apple的信息。 就这种情况而言

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

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

1 个答案:

答案 0 :(得分:0)

&#34;出乎意料地发现没有&#34;当您尝试访问不存在的内容时会发生错误。所以在你提供的那一行上,它是IAPManager.sharedInstance.products?.object(at:0)。

值得跟踪这个,并找出它为什么是空的(如果你试图访问它,我认为它应该有一个值)。