我已在我的应用中实施了应用内购买。它间歇性地工作。
大约1/3会崩溃,这是我在SKScene中实现的方式:
class GameOverMenu: SKScene, UIAlertViewDelegate, GKGameCenterControllerDelegate, SKProductsRequestDelegate, SKPaymentTransactionObserver {
var activeProduct: SKProduct?
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch (transaction.transactionState) {
case .purchased:
SKPaymentQueue.default().finishTransaction(transaction)
print("Purchased")
case .failed:
SKPaymentQueue.default().finishTransaction(transaction)
print("Failed")
default:
break
}
}
}
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
print("loaded products")
for product in response.products {
print("Product: \(product.productIdentifier), \(product.localizedTitle), \(product.price.floatValue)")
activeProduct = product
}
}
override func didMove(to view: SKView) {
SKPaymentQueue.default().add(self)
let productIdentifiers: Set<String> = ["gold_space_coins"]
let productsRequest = SKProductsRequest(productIdentifiers: productIdentifiers)
productsRequest.delegate = self
productsRequest.start()
}
if(atPoint(location) == buyCoins){
if let activeProduct = activeProduct {
print("Buying \(activeProduct.productIdentifier)")
let payment = SKPayment(product: activeProduct)
SKPaymentQueue.default().add(payment)
}else{
print("No product")
}
}
以下是崩溃发生时的日志:
loaded products
Product: gold_space_coins, 500 Pieces of Gold Coins, 0.99
我退出现场然后返回**
loaded products
Product: gold_space_coins, 500 Pieces of Gold Coins, 0.99
Buying gold_space_coins
warning: could not load any Objective-C class information. This will significantly reduce the quality of type information available.
它在线失败:
SKPaymentQueue.default().add(payment)
我应该在视图控制器中实现这个吗?或者我做错了什么?
答案 0 :(得分:1)
解决:
离开现场时,我没有移除paymentQueue,导致场景重新出现时出现内存问题。
这解决了它:
override func willMove(from view: SKView) {
SKPaymentQueue.default().remove(self)
}
答案 1 :(得分:0)
这对我有用:
Swift 3
override func willMove(toParentViewController parent: UIViewController?) {
SKPaymentQueue.default().remove(self)
}