我制作了一个Apple TV应用程序,但我的应用内购买没有响应。我不知道自己做错了什么。我也没有得到任何构建错误。
我将应用内购买添加到iTunes Connect,将权限添加到我的应用ID,并添加StoreKit框架。我在苹果电视上测试了我的应用程序,但当我按下按钮购买应用程序内购买时,什么也没发生。当我按下按钮恢复应用内购买时,它确实要求使用我的苹果ID登录,所以我想这是有效的。
这是我用于应用内购买的类:
import Foundation
import StoreKit
class InAppPurchase : NSObject, SKProductsRequestDelegate, SKPaymentTransactionObserver {
let kInAppProductPurchasedNotification = "InAppProductPurchasedNotification"
let kInAppPurchaseFailedNotification = "InAppPurchaseFailedNotification"
let kInAppProductRestoredNotification = "InAppProductRestoredNotification"
let kInAppPurchasingErrorNotification = "InAppPurchasingErrorNotification"
class var sharedInstance : InAppPurchase {
struct Static {
static var onceToken: dispatch_once_t = 0
static var instance: InAppPurchase? = nil
}
dispatch_once(&Static.onceToken) {
Static.instance = InAppPurchase()
}
return Static.instance!
}
override init() {
super.init()
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
}
func buyProduct(product: SKProduct) {
print("Sending the Payment Request to Apple")
let payment = SKPayment(product: product)
SKPaymentQueue.defaultQueue().addPayment(payment)
}
func restoreTransactions() {
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}
func request(request: SKRequest, didFailWithError error: NSError) {
print("Error %@ (error)")
NSNotificationCenter.defaultCenter().postNotificationName(kInAppPurchasingErrorNotification, object: error.description)
}
func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
print("Got the request from Apple")
let count: Int = response.products.count
if count > 0 {
_ = response.products
let validProduct: SKProduct = response.products[0]
print(validProduct.localizedTitle)
print(validProduct.localizedDescription)
print(validProduct.price)
buyProduct(validProduct);
}
else {
print("No products")
}
}
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
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")
savePurchasedProductIdentifier(trans.payment.productIdentifier)
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
NSNotificationCenter.defaultCenter().postNotificationName(kInAppProductPurchasedNotification, object: nil)
break
case .Failed:
print("Purchased Failed")
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
NSNotificationCenter.defaultCenter().postNotificationName(kInAppPurchaseFailedNotification, object: nil)
break
case .Restored:
print("Product Restored")
savePurchasedProductIdentifier(trans.payment.productIdentifier)
SKPaymentQueue.defaultQueue().finishTransaction(transaction as! SKPaymentTransaction)
NSNotificationCenter.defaultCenter().postNotificationName(kInAppProductRestoredNotification, object: nil)
break
default:
break
}
}
}
}
func savePurchasedProductIdentifier(productIdentifier: String!) {
NSUserDefaults.standardUserDefaults().setObject(productIdentifier, forKey: productIdentifier)
NSUserDefaults.standardUserDefaults().synchronize()
}
func unlockProduct(productIdentifier: String!) {
if SKPaymentQueue.canMakePayments() {
let productID: NSSet = NSSet(object: productIdentifier)
let productsRequest: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set)
productsRequest.delegate = self
productsRequest.start()
print("Fetching Products")
}
else {
print("Сan't make purchases")
NSNotificationCenter.defaultCenter().postNotificationName(kInAppPurchasingErrorNotification, object: NSLocalizedString("CANT_MAKE_PURCHASES", comment: "Can't make purchases"))
}
}
func buyUnlockSeasonsPack() {
unlockProduct("Company.SeasonsPack")
}
}
And these are my IBAction functions for my buttons:
@IBAction func getSeasonsPack(sender: UIButton) { InAppPurchase.sharedInstance.buyUnlockSeasonsPack() } @IBAction func RestorePurchases(sender: UIButton) { InAppPurchase.sharedInstance.restoreTransactions() }
请帮帮我。
您需要保留您的productsRequest,因为它在func unlockProduct()结束时自动释放,并且不会触发以下回调。
只需将其声明为类级变量即可。