我是SWIFT和编程的初学者。我正在创建一些应用游戏。我在我的应用程序中添加了一些IAP。它的工作非常完美。我有一个非消耗品购买。我需要你的帮助。如何查看购买的商品?当我按下恢复按钮时,我的应用程序恢复我的IAP并显示一切正常。但如果用户没有买东西 - 没有任何反应。但我想告诉用户他们没有买任何东西。
这是我的代码
class MainMenuScene: SKScene, AVAudioPlayerDelegate, SKPaymentTransactionObserver, SKProductsRequestDelegate
var request:SKProductsRequest = SKProductsRequest()
if(SKPaymentQueue.canMakePayments())
{print("OK!")}else{print("please enable IAPS")}
let productID:NSSet = NSSet(objects:
"IAP ID1",
"IAP ID2",
"IAP ID3")
request = SKProductsRequest(productIdentifiers: productID as Set<NSObject> as Set<NSObject> as! Set<String>)
request.delegate = self
request.start()
var list = [SKProduct]()
var p = SKProduct()
func buyProduct()
{
print("buy " + p.productIdentifier)
let pay = SKPayment(product: p)
SKPaymentQueue.default().add(self)
SKPaymentQueue.default().add(pay as SKPayment)
}
func productsRequest(_ request: SKProductsRequest, didReceive 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 as SKProduct)
}
}
func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue)
{
for transaction in queue.transactions
{
let t: SKPaymentTransaction = transaction
let prodID = t.payment.productIdentifier as String
switch prodID
{
case "IAP ID1": add1000Coins()
case "IAP ID2": add5000Coins()
case "IAP ID3": unlockAfterPurchase()
default:
print("complete")
}
}
}
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction])
{
for transaction:AnyObject in transactions
{
let trans = transaction as! SKPaymentTransaction
print(trans.error)
switch trans.transactionState
{
case .purchased:
print(p.productIdentifier)
let prodID = p.productIdentifier as String
switch prodID
{
case "IAP ID1": add1000Coins()
case "IAP ID2": add5000Coins()
case "IAP ID3": unlockAfterPurchase()
default:
print("IAP no found")
}
queue.finishTransaction(trans)
case .failed:
print("buy error")
queue.finishTransaction(trans)
break
case .restored:
let alert = UIAlertView(title: "Thank You", message: "Your purchase(s) were restored.", delegate: nil, cancelButtonTitle: "OK")
alert.show()
break
default: print("default")
break
}
}
}
func finishTransaction(trans:SKPaymentTransaction)
{
}
private func paymentQueue(queue: SKPaymentQueue!, removedTransactions transactions: [AnyObject]!)
{
}
func add1000Coins()
{
//...
}
func add5000Coins()
{
//...
}
func transactionObserver()
{
SKPaymentQueue.default().remove(self)
}
func restorePreviousPurchases()
{
//...
}
func unlockAfterPurchase()
{
//...
}
我正在看一些教程,但我不明白如何解决我的问题
请帮我解决代码
P.S。对不起我的英文,感谢您的关注。