产品在 Swift 语言中请求崩溃
Posted
技术标签:
【中文标题】产品在 Swift 语言中请求崩溃【英文标题】:Products request crash in Swift language 【发布时间】:2016-04-30 18:35:15 【问题描述】:我有这段代码要在应用购买中处理
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,然后它因以下错误而崩溃:消息已发送到已释放实例。
这是我用僵尸登录时的结果。
我已经尝试了所有方法,但结果出现了这个错误。我该如何解决这个问题?
【问题讨论】:
【参考方案1】:在您提出请求后,当您超出范围时,您的 iap 类将被释放。将其存储在属性中以使其保持活力:
class test: UITabBarController
let inAppPurchase = iap()
override func viewDidLoad()
super.viewDidLoad()
【讨论】:
我试过了,没什么区别。 (我在我的问题中添加了你的代码) 我觉得很愚蠢,知道了,它有效!非常感谢! 不用担心。可能有助于重新阅读 Swift 指南。 还有一个问题,为什么会崩溃?是因为系统忙于处理 iap 类,并且视图确实加载释放它以进入下一个块? 您将您的 iap 类设置为 SKProductRequest 委托,发出请求,然后释放该类(因为您没有维护对它的强引用)。当一个对象有 0 个强引用时,ARC 会释放它。当 SKProductRequest 尝试调用已发布类的方法时发生崩溃。以上是关于产品在 Swift 语言中请求崩溃的主要内容,如果未能解决你的问题,请参考以下文章