是否有一种简单的方法可以针对单个产品快速实施应用内购买?
我想要一个可以触发应用内购买的按钮,例如[广告删除]或[解锁高级内容]
我不明白它的全部逻辑。
我正在尝试从[Techotopia] http://www.techotopia.com/index.php/An_iOS_7_In- App_Purchase_Tutorial中阅读并翻译此教程
但这是我第一次使用StoreKit框架,也是使用Swift。
我只想了解Swift StoreKit框架内应用内购买交易的逻辑。
谢谢!
步骤0:在您的iTunes Connect帐户中,创建一个应用内购买。
对于单次购买,请使用以下方法:
进口
import StoreKit
符合StoreKit委托
class YOURViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {
let defaults = NSUserDefaults.standardUserDefaults()
var product_id: NSString? override func viewDidLoad() { product_id = "YOUR_PRODUCT_ID" super.viewDidLoad() SKPaymentQueue.defaultQueue().addTransactionObserver(self) //Check if product is purchased if (defaults.boolForKey("purchased")){ // Hide a view or show content depends on your requirement overlayView.hidden = true } else if (!defaults.boolForKey("stonerPurchased")) { print("false") } }
@IBAction func unlockAction(sender: AnyObject) { print("About to fetch the products") // We check that we are allow to make the purchase. if (SKPaymentQueue.canMakePayments()) { var productID:NSSet = NSSet(object: self.product_id!); var productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID); productsRequest.delegate = self; productsRequest.start(); println("Fetching Products"); } else { print("can't make purchases"); } }
func buyProduct(product: SKProduct) { println("Sending the Payment Request to Apple"); var payment = SKPayment(product: product) SKPaymentQueue.defaultQueue().addPayment(payment); }
func productsRequest (request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) { var count : Int = response.products.count if (count>0) { var validProducts = response.products var validProduct: SKProduct = response.products[0] as SKProduct if (validProduct.productIdentifier == self.product_id) { print(validProduct.localizedTitle) print(validProduct.localizedDescription) print(validProduct.price) buyProduct(validProduct); } else { print(validProduct.productIdentifier) } } else { print("nothing") } } func request(request: SKRequest!, didFailWithError error: NSError!) { print("Error Fetching product information"); } func paymentQueue(queue: SKPaymentQueue!, updatedTransactions transactions: [AnyObject]!) { print("Received Payment Transaction Response from Apple"); for transaction:AnyObject in transactions { if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{ switch trans.transactionState { case .Purchased: print("Product Purchased"); SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction) defaults.setBool(true , forKey: "purchased") overlayView.hidden = true break; case .Failed: print("Purchased Failed"); SKPaymentQueue.defaultQueue().finishTransaction(transaction as SKPaymentTransaction) break; case .Restored: print("Already Purchased"); SKPaymentQueue.defaultQueue().restoreCompletedTransactions() default: break; } } } }
迅捷 > 3.0
import StoreKit class YOURVIEWController:UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver { let product_id: NSString = "com.some.inappid" // <!-- Change it to your inapp id
在您的viewDidLoad添加中
viewDidLoad
override func viewDidLoad() { super.viewDidLoad() SKPaymentQueue.default().add(self)
在您的购买按钮操作中
@IBAction func buyNowAction(_ sender: UIButton) { if (SKPaymentQueue.canMakePayments()) { let productID:NSSet = NSSet(array: [self.product_id as NSString]); let productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>); productsRequest.delegate = self; productsRequest.start(); print("Fetching Products"); } else { print("can't make purchases"); } }
在“还原”按钮中
// MARK: - Restore In App Purchase @IBAction func restoreAction(_ sender: UIButton) { if (SKPaymentQueue.canMakePayments()) { SKPaymentQueue.default().add(self) SKPaymentQueue.default().restoreCompletedTransactions() } else { // show error } }
添加代表:
// SKProductRequest Delegate func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) { print(response.products) let count : Int = response.products.count if (count>0) { let validProduct: SKProduct = response.products[0] as SKProduct if (validProduct.productIdentifier == self.product_id as String) { print(validProduct.localizedTitle) print(validProduct.localizedDescription) print(validProduct.price) self.buyProduct(product: validProduct) } else { print(validProduct.productIdentifier) } } else { print("nothing") } } func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { for transaction:AnyObject in transactions { if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{ self.dismissPurchaseBtn.isEnabled = true self.restorePurchaseBtn.isEnabled = true self.buyNowBtn.isEnabled = true switch trans.transactionState { case .purchased: print("Product Purchased") //Do unlocking etc stuff here in case of new purchase SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction) break; case .failed: print("Purchased Failed"); SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction) break; case .restored: print("Already Purchased") //Do unlocking etc stuff here in case of restor SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction) default: break; } } } } //If an error occurs, the code will go to this function func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) { // Show some alert }