I followed the tutorial on creating an IAP for my game. My goal is to click the "Remove Ads" button. After the user receives this IAP, the delete advertisement button will disappear and the ads will stop showing. With future launches of the application, this purchase is automatically downloaded.
I have the basics of this, but here are my problems.
I created a sandbox, and every time I launch my application, I ask you to enter itunes. I would think that in fact I only need to register when I decide to purchase the application. Is this because I am a sandboxed user?
I also restore purchases every time I launch the application. This seems to happen automatically without logging in. So why do I ask you to log in every time? Should I use NSUserDefaults to avoid future purchases?
Here is my code:
override func viewDidLoad() {
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
self.getProductInfo()
}
func getProductInfo(){
if SKPaymentQueue.canMakePayments() {
let request = SKProductsRequest(productIdentifiers: NSSet(object: self.productID))
request.delegate = self
request.start()
}
}
Delegate Methods
func productsRequest(request: SKProductsRequest!, didReceiveResponse response: SKProductsResponse!) {
var products = response.products
if products.count != 0 {
self.product = products[0] as? SKProduct
}
}
func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue!) {
if queue.transactions.count != 0 {
if let purchase = queue.transactions[0] as? SKPaymentTransaction {
if purchase.payment.productIdentifier == self.productID {
println("you bought it already")
}
}
}
}
func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) {
for transaction in transactions as [SKPaymentTransaction] {
switch transaction.transactionState {
case SKPaymentTransactionState.Purchased:
SKPaymentQueue.defaultQueue().finishTransaction(transaction)
case SKPaymentTransactionState.Failed:
SKPaymentQueue.defaultQueue().finishTransaction(transaction)
default:
break
}
}
}
Is this the right approach?
source
share