gpt4 book ai didi

Swift tvOS 应用内购买没有响应

转载 作者:可可西里 更新时间:2023-11-01 01:37:52 33 4
gpt4 key购买 nike

我制作了一个 Apple TV 应用程序,但我的应用程序内购买没有响应。我不知道我做错了什么。我也没有收到任何构建错误。

我将应用内购买添加到 iTunes Connect,将授权添加到我的应用 ID,并添加了 StoreKit 框架。我在 Apple TV 上测试了我的应用程序,但当我按下按钮购买我的应用程序内购买时,没有任何反应。当我按下按钮以恢复应用程序内购买时,它确实要求使用我的 Apple 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")
}
}


<p>And these are my IBAction functions for my buttons:</p>

@IBAction func getSeasonsPack(sender: UIButton) {
InAppPurchase.sharedInstance.buyUnlockSeasonsPack()
}

@IBAction func RestorePurchases(sender: UIButton) {
InAppPurchase.sharedInstance.restoreTransactions()
}

请帮帮我。

最佳答案

您需要保留您的 productsRequest,因为它在 func unlockProduct() 结束时自动释放,并且不会触发以下回调。

只需将其声明为类级别变量即可。

关于Swift tvOS 应用内购买没有响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33842304/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com