我制作了Apple TV应用,但我的应用内购买并未做出回应。我不知道我做错了什么。我也没有任何构建错误。
我将应用内购买添加到iTunes Connect,添加了应用ID的权利并添加了StoreKit框架。 我在Apple TV上测试了我的应用程序但是当我按下按钮购买我的应用内购买时,没有任何反应。 当我按下按钮恢复应用内购买时,它确实要求使用我的苹果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()
}
请帮帮我。
答案 0 :(得分:2)
您需要保留您的productsRequest,因为它在func unlockProduct()结束时自动释放,并且不会触发以下回调。
只需将其声明为类级变量。