2016-11-20 38 views
0

下午好,人(SWIFT)应用程序内购买的Wi-Fi错误

观测值:我用的Rechibility类来检查用户的互联网连接或没有,但在这种情况下,类验证互联网,但我不接收来自苹果的信息。 正是在这种情况下

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

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

回答

0

的 “意外发现无” 当您尝试访问一些错误恰巧不存在。所以在你给出的这一行上,它将是IAPManager.sharedInstance.products?.object(at:0)。

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

+0

是的,丹尼我知道这件事,但是很奇怪,因为离线的用户不会收到信息,所以我使用类Rechiabilty来检查互联网是否连接,但是当我检查有关产品的信息不返回 –