gpt4 book ai didi

ios - Swift语言产品请求崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 06:39:50 24 4
gpt4 key购买 nike

我有此代码可在应用程序购买中处理

import Foundation
import StoreKit

class iap: NSObject, SKProductsRequestDelegate,SKPaymentTransactionObserver {


var productsArray = Array<SKProduct>()
var product: SKProduct?

override init()
{
super.init()

if SKPaymentQueue.canMakePayments() {
print("starting IAPS")
let productIdentifiers = Set(["com.blabla.bla.pro"])
let request = SKProductsRequest(productIdentifiers: productIdentifiers as Set<String>)
request.delegate = self
request.start()
} else {
print("please enable IAPS")
}
}

// 1
var list = [SKProduct]()
var p = SKProduct()

// 2
func buyProduct() {
print("buy " + p.productIdentifier)
let pay = SKPayment(product: p)
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().addPayment(pay as SKPayment)
}

//3
func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
print("product request")
let myProduct = response.products

for product in myProduct {
print("product added")
print(product.productIdentifier)
print(product.localizedTitle)
print(product.localizedDescription)
print(product.price)

list.append(product )
}


}

// 4
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) {
print("transactions restored")

for transaction in queue.transactions {
let t: SKPaymentTransaction = transaction

let prodID = t.payment.productIdentifier as String

switch prodID {
case "com.blabla.bla.pro":
print("pro upgrade update only basic stuff futher updates must be payed")


default:
print("IAP not setup")
}

}
}

// 5
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print("add paymnet")

for transaction:AnyObject in transactions {
let trans = transaction as! SKPaymentTransaction
print(trans.error)

switch trans.transactionState {

case .Purchased:
print("buy, ok unlock iap here")
print(p.productIdentifier)

let prodID = p.productIdentifier as String
switch prodID {
case "com.blabla.bla.pro":
print("pro upgrade update only basic stuff futher updates must be payed")


default:
print("IAP not setup")
}

queue.finishTransaction(trans)
break;
case .Failed:
print("buy error")
queue.finishTransaction(trans)
break;
default:
print("default")
break;

}
}
}

// 6
func finishTransaction(trans:SKPaymentTransaction)
{
print("finish trans")
SKPaymentQueue.defaultQueue().finishTransaction(trans)
}

//7
func paymentQueue(queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction])
{
print("remove trans");
}

}

当加载 View 时,我调用 iap 类:

class test: UITabBarController {

override func viewDidLoad() {
super.viewDidLoad()

let inAppPurchase = iap()

}
}

调用有效,但我唯一看到的是启动 iaps,然后它崩溃并出现此错误:消息已发送到已释放的实例。

这是我用僵尸登录时的结果。 enter image description here

我已经尝试了所有方法,但结果是这个错误。我该如何解决这个问题?

最佳答案

当您发出请求后超出范围时,您的 iap 类将被释放。将其存储在属性中以使其保持事件状态:

class test: UITabBarController {

let inAppPurchase = iap()

override func viewDidLoad() {
super.viewDidLoad()
}
}

关于ios - Swift语言产品请求崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36958712/

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