-
-
Save Callonski/9f93b4eb087f40c803ad3234a776eea2 to your computer and use it in GitHub Desktop.
In app purchase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver { | |
| let productIdentifiers = Set(["20diamonds", "premium"]) | |
| var productsArray = Array<SKProduct>() | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| // Do any additional setup after loading the view, typically from a nib. | |
| SKPaymentQueue.default().add(self) | |
| requestProductData() | |
| } | |
| @IBAction func buyProduct1(_ sender: AnyObject) { | |
| let payment = SKPayment(product: productsArray[0]) | |
| SKPaymentQueue.default().add(payment) | |
| } | |
| @IBAction func restorePurchases(_ sender: AnyObject) { | |
| SKPaymentQueue.default().restoreCompletedTransactions() | |
| } | |
| func requestProductData() | |
| { | |
| if SKPaymentQueue.canMakePayments() { | |
| let request = SKProductsRequest(productIdentifiers: self.productIdentifiers as Set<String>) | |
| request.delegate = self | |
| request.start() | |
| } else { | |
| // Kan ej köpa | |
| } | |
| } | |
| func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) { | |
| var products = response.products | |
| if (products.count != 0) { | |
| for i in 0 ..< products.count | |
| { | |
| self.productsArray.append(products[i]) | |
| print("*****************") | |
| print(products[i].localizedTitle) | |
| print(products[i].localizedDescription) | |
| print(products[i].price) | |
| print(products[i].priceLocale) . // vilket språk valutan är i | |
| } | |
| // Sätter valuta efter land | |
| let currency_format = NumberFormatter() | |
| currency_format.numberStyle = NumberFormatter.Style.currency | |
| currency_format.locale = products[0].priceLocale | |
| product1title.text = self.productsArray[0].localizedTitle+" "+currency_format.string(from: products[0].price)!; | |
| product1description.text = self.productsArray[0].localizedDescription; | |
| product2title.text = self.productsArray[1].localizedTitle+" "+currency_format.string(from: products[1].price)!; | |
| product2description.text = self.productsArray[1].localizedDescription; | |
| buy1Button.isEnabled = true | |
| buy2Button.isEnabled = true | |
| } else { | |
| print("No products found") | |
| } | |
| } | |
| func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { | |
| print("paymentQueue updatedTransactions") | |
| for transaction in transactions { | |
| switch transaction.transactionState { | |
| case SKPaymentTransactionState.purchased: | |
| print("Transaction Approved") | |
| print("Product Identifier: \(transaction.payment.productIdentifier)") | |
| self.deliverProduct(transaction) | |
| SKPaymentQueue.default().finishTransaction(transaction) | |
| case SKPaymentTransactionState.failed: | |
| print("Transaction Failed") | |
| SKPaymentQueue.default().finishTransaction(transaction) | |
| default: | |
| break | |
| } | |
| } | |
| } | |
| func deliverProduct(_ transaction:SKPaymentTransaction) { | |
| print("deliverProduct "+transaction.payment.productIdentifier) | |
| if transaction.payment.productIdentifier == "20diamonds" | |
| { | |
| print("20diamonds bought") | |
| } | |
| else if transaction.payment.productIdentifier == "premium" | |
| { | |
| print("premium bought") | |
| } | |
| } | |
| func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) { | |
| print("Transactions Restored") | |
| for transaction:SKPaymentTransaction in queue.transactions | |
| { | |
| if transaction.payment.productIdentifier == "20diamonds" | |
| { | |
| print("20diamonds Restored") | |
| } | |
| else if transaction.payment.productIdentifier == "premium" | |
| { | |
| print("premium Restored") | |
| } | |
| } | |
| } | |
| func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) { | |
| print("Restore failed") | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment