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

标签 ios swift in-app-purchase swift3 storekit

大家下午好

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

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

1:用户在没有互联网连接(Wi-Fi 或 4G)的情况下进入应用程序 2:用户尝试离线购买应用程序并进入购买 View Controller 3:按iPhone HOME键,然后连接Wi-Fi或4G 4:返回app后再次按下购买按钮出现崩溃

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

我不明白为什么会发生这个错误。

下面是我的代码和有关错误的图像。

我当前的代码:

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

最佳答案

当您尝试访问不存在的内容时,会发生“意外发现 nil”错误。因此,在您给出的行上,它将是 IAPManager.sharedInstance.products?.object(at: 0)。

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

关于ios - (Swift) 应用内购买 Wi-Fi 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40708930/

相关文章:

ios - 如何在没有 prepareForSegue 的情况下在 Swift 文件之间传递数据?

ios - 在 swift 中从 firebase 检索数组元素

iphone - 如何按价格对应用内购买的产品进行排序?

iphone - NSString 到 NSNumber : what am I doing wrong here?

IOS:在 xcode 中为我的应用设置默认语言

ios - 使用导航 Controller 在滑出面板中显示右键

ios - ios中的编码字符串为普通字符串?

iphone - 应用内购买 : Stuck at paymentWithProductIdentifiers - which is deprecated

jquery - 通过 jquery 验证应用内购买 - 21002 错误

ios - RestKit崩溃— RKManagedObjectRequestOperation.m第873行