gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:53:14 25 4
gpt4 key购买 nike

大家下午好

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

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

1:用户在没有互联网连接(Wi-Fi 或 4G)的情况下进入应用程序2:用户尝试离线购买应用程序并进入购买 View Controller 3:按iPhone HOME键,然后连接Wi-Fi或4G4:返回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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com