我发现此代码可以在iOS 9上使用swift 2执行应用内购买。 我的IAP(应用内购买)将位于表格中。它将显示为警告消息,您可以点按购买或取消。 如果您点击“购买”,则会显示来自Appstore的新警报以确认此消息。
我使用沙盒用户登录。确认后,我的控制台显示:
Transaction Failed
以下是代码:
import UIKit
import StoreKit
import iAd
protocol IAPurchaceViewControllerDelegate {
func didBuyPremium(Index: Int)
}
class iAP: UIViewController, UITableViewDelegate, UITableViewDataSource, SKProductsRequestDelegate, SKPaymentTransactionObserver {
@IBOutlet weak var LoadingScreen: UIView!
@IBOutlet weak var tblProducts: UITableView!
var delegate: IAPurchaceViewControllerDelegate!
var productIDs: Array<String!> = []
var productsArray: Array<SKProduct!> = []
var selectedProductIndex: Int!
var transactionInProgress = false
override func viewWillAppear(animated: Bool) {
view.bringSubviewToFront(LoadingScreen)
LoadingScreen.hidden = false
navigationController?.setNavigationBarHidden(false, animated: true)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
tblProducts.delegate = self
tblProducts.dataSource = self
// Replace the product IDs with your own values if needed.
productIDs.append("xxx_Iap")
requestProductInfo()
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
}
// MARK: UITableView method implementation
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return productsArray.count
}
func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
return 65
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("IAPCell") as! ModifyCellsIAP
let product = productsArray[indexPath.row]
cell.PremiumName.text = product.localizedTitle
cell.PremiumBeschreibung.text = product.localizedDescription
let price = "\(product.price)"
let FormatPrice = price.stringByReplacingOccurrencesOfString(".", withString: ",")
cell.PremiumPreis.setTitle("\(FormatPrice)€", forState: .Normal)
LoadingScreen.hidden = true
return cell
}
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
selectedProductIndex = indexPath.row
showActions()
tableView.cellForRowAtIndexPath(indexPath)?.selected = false
}
func requestProductInfo() {
if SKPaymentQueue.canMakePayments() {
let productIdentifiers = NSSet(array: productIDs)
let productRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productIdentifiers as! Set<String>)
productRequest.delegate = self
productRequest.start()
}
else {
print("Cannot perform In App Purchases.")
}
}
func showActions() {
if transactionInProgress {
return
}
let actionSheetController = UIAlertController(title: "Upgrade", message: "Möchten Sie diese Erweiterung freischalten?", preferredStyle: UIAlertControllerStyle.ActionSheet)
let buyAction = UIAlertAction(title: "Freischalten", style: UIAlertActionStyle.Default) { (action) -> Void in
let payment = SKPayment(product: self.productsArray[self.selectedProductIndex] as SKProduct)
SKPaymentQueue.defaultQueue().addPayment(payment)
self.transactionInProgress = true
}
let cancelAction = UIAlertAction(title: "Abbrechen", style: UIAlertActionStyle.Cancel) { (action) -> Void in
}
actionSheetController.addAction(buyAction)
actionSheetController.addAction(cancelAction)
presentViewController(actionSheetController, animated: true, completion: nil)
}
func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
if response.products.count != 0 {
for product in response.products {
productsArray.append(product )
}
tblProducts.reloadData()
}
else {
print("There are no products.")
}
if response.invalidProductIdentifiers.count != 0 {
print("Invalid: \(response.invalidProductIdentifiers.description)")
}
}
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case SKPaymentTransactionState.Purchased:
print("Transaction completed successfully.")
SKPaymentQueue.defaultQueue().finishTransaction(transaction)
transactionInProgress = false
delegate.didBuyPremium(selectedProductIndex)
case SKPaymentTransactionState.Failed:
print("Transaction Failed");
SKPaymentQueue.defaultQueue().finishTransaction(transaction)
transactionInProgress = false
default:
print("Default: \(transaction.transactionState.rawValue)")
}
}
}
}
答案 0 :(得分:0)
我最近在我的应用中成功实施了应用内购买。很高兴分享经验。 Objective-C中的代码,但我认为这不应该有任何根本的区别。
要检查的事项:
检查应用程序内购买:
if ([SKPaymentQueue canMakePayments] == YES) {
// proceed with payment
}
交易失败错误代码:
if (transaction.error.code != SKErrorPaymentCancelled) {
// This is the error that should be presented to user and/or processed
}
else {
// "Canceled" error should go silently for user as this is more or less expected.
// The user can re-issue the payment process later on
}