gpt4 book ai didi

ios - SKProduct RequestDelegate 离开 View 时崩溃

转载 作者:行者123 更新时间:2023-11-30 13:24:19 24 4
gpt4 key购买 nike

我已经实现了一个 IAP 场景 OptionsPage: SKScene,用户可以在其中购买“删除广告”和游戏币等。但是如果我从 MenuScene: SKScene 转到OptionsPage: SKScene 并快速返回菜单,它因 Thread 1: EXC_BAD_ACCESS (code=1, address=0x216d0716) 崩溃,底部日志中唯一的内容是(lldb)。

我正在 willMoveFromView() 中调用 SKPaymentQueue.defaultQueue().removeTransactionObserver(self)

在我的 didMoveToView 中,我调用 setIAP() 函数(如下所示)。

在不断地用头撞 table (第一次处理 IAP)之后,我认为这是因为我设置了 request.delegate = self ,所以当我进入场景时 request.start() 运行,但因为我很快离开场景 request.delegate = self 不再有效。

我认为删除事务观察器可以在 willMoveFromView 中解决这个问题。我该如何解决这个问题?

这是我在 touchesBegan() 中调用购买的方式

if removeAds.containsPoint(location) {
for product in list {
let prodID = product.productIdentifier
if (prodID == "removeAds") {
p = product
buyProduct()
break
}
}
}

以下是 IAP 部分的外观

// MARK: In App Purchases

func setIAP() {
// Set IAPS
if(SKPaymentQueue.canMakePayments()) {
print("IAP is enabled, loading")
let productID:NSSet = NSSet(objects: "removeAds, 10000coins")
let request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>)
request.delegate = self
request.start()
} else {
print("please enable IAPS")
}
}

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

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

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

for product in myProduct {
print("Product added: \(product.productIdentifier), \(product.localizedTitle), \(product.localizedDescription), \(product.price)")

list.append(product)
}
}

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 "removeAds":
defaults.setBool(true, forKey: "removeAdsPurchased")

default:
print("IAP not setup")
}
}

let alert = UIAlertController(title: "Thank You", message: "Thankyou for your purchase.", preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.view?.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)
}

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, .Restored:
print(p.productIdentifier)

let prodID = p.productIdentifier as String

switch prodID {
case "removeAds":

defaults.setBool(true, forKey: "removeAdsPurchased")

case "10000Coins":

defaults.setInteger(bank + 10000, forKey: "bankValue")
bank = defaults.integerForKey("bankValue")

default:
print("IAP not setup")
}

queue.finishTransaction(trans)
break;

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

default:
print("default")
break;
}
}
}

func finishTransaction(trans:SKPaymentTransaction) {
print("Transaction finished")
}

func paymentQueue(queue: SKPaymentQueue, removedTransactions transactions: [SKPaymentTransaction]) {
print("Transaction removed")
}

最佳答案

由于没有反馈,我解决这个问题的方法非常乏味,但如下:

我没有将菜单和选项场景组合在一起,而是将两个场景结合起来,以便当点击选项按钮时,它会运行一个操作,将菜单节点滑出屏幕并将选项滑到屏幕上。这样 request.delegate = self 不会改变,从而避免崩溃。那么唯一的问题是,如果我在 request.start 完成之前启动应用程序并快速按下播放按钮以移动到游戏场景,则会出现相同的崩溃。因此,我创建了一个加载初始屏幕,在菜单场景 didMoveToView 中运行 2 秒,这样它就有足够的时间在允许交互之前获取所有数据。

我很想听到一个更简单的解决方案,但根据我的经验,这就是我想出的。

关于ios - SKProduct RequestDelegate 离开 View 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37403008/

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