我有这个代码来处理应用程序购买
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");
}
}
当加载视图时,我调用iap类:
class test: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
let inAppPurchase = iap()
}
}
调用有效,但我唯一看到的是启动iaps,而不是崩溃时出现此错误:消息发送到解除分配的实例。
我已尝试过所有内容,但结果出现此错误。我该如何解决这个问题?
答案 0 :(得分:3)
在您提出请求后,当您超出范围时,您的iap类会被释放。将它存放在一个属性中以使其保持活力:
class test: UITabBarController {
let inAppPurchase = iap()
override func viewDidLoad() {
super.viewDidLoad()
}
}