gpt4 book ai didi

ios - 使用 SKPaymentTransactionObserver 打开 View 后,应用程序不断询问 Apple ID

转载 作者:行者123 更新时间:2023-11-28 08:44:59 24 4
gpt4 key购买 nike

所以我知道问题出在 IAP 的 ViewController 上,但不确定如何修复它。

我在 viewDidLoad 中添加了 SKPaymentQueue.defaultQueue().addTransactionObserver(self) 并拥有该类的代表; SKProductsRequestDelegate,SKPaymentTransactionObserver

我使用这些方法来执行购买交易、恢复任何购买以及对非消费品购买进行所需的任何更改。

但无论用户做什么,在加载 IAPViewController 后,应用都会提示用户登录。它主要是当用户退出应用程序然后重新打开它时。

我尝试在关闭 View 时添加 SKPaymentQueue.defaultQueue().removeTransactionObserver(self) 但这似乎不起作用。

有人有什么建议吗??由于这个错误,我不得不取消我的应用程序审查:(

这是完整的代码;

class IAPViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {

var productIDs: Array<String!> = []
var productsArray: Array<SKProduct!> = []

var selectedProductIndex: Int!
var transactionInProgress = false

override func viewDidLoad() {
super.viewDidLoad()


productIDs.append("com.COMPANY.NAME.BUY01")
productIDs.append("com.COMPANY.NAME.BUY02")
productIDs.append("com.COMPANY.NAME.BUY03")
productIDs.append("com.COMPANY.NAME.BUY04")

requestProductInfo()
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
}

@IBAction func purchase04(sender: AnyObject) {

selectedProductIndex = 0
transactionAction()


}

@IBAction func restoreAction(sender: AnyObject) {
SKPaymentQueue.defaultQueue().addTransactionObserver(self)
SKPaymentQueue.defaultQueue().restoreCompletedTransactions()
}


//StoreKit


func transactionAction() {
let payment = SKPayment(product: self.productsArray[self.selectedProductIndex] as SKProduct)
SKPaymentQueue.defaultQueue().addPayment(payment)
self.transactionInProgress = true

}

//Request Products
func requestProductInfo() {
if SKPaymentQueue.canMakePayments() {
let productIdentifiers = NSSet(array: productIDs)
let productRequest = SKProductsRequest(productIdentifiers: productIdentifiers as! Set<String>)

productRequest.delegate = self
productRequest.start()

}
else {
print("Cannot perform In App Purchases.")
}

}

func productsRequest(request: SKProductsRequest, didReceiveResponse response: SKProductsResponse) {
if response.products.count != 0 {
for product in response.products {
print("\(product.localizedTitle)")
productsArray.append(product)
}
}
else {
print("There are no products.")
}

if response.invalidProductIdentifiers.count != 0 {
print("\(response.invalidProductIdentifiers.description)")
}

print("Number of products in productsArray \(productsArray.count) - Number of products in productIDs \(productIDs.count)")

}


//Payment Observer
func paymentQueue(queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case SKPaymentTransactionState.Purchased:
print("Transaction completed successfully.", terminator: "")
self.deliverProduct(transaction)
SKPaymentQueue.defaultQueue().finishTransaction(transaction)
transactionInProgress = false

case SKPaymentTransactionState.Failed:
print("Transaction Failed", terminator: "");
SKPaymentQueue.defaultQueue().finishTransaction(transaction)
transactionInProgress = false

default:
print(transaction.transactionState.rawValue, terminator: "")
}
}
}

func deliverProduct(transaction:SKPaymentTransaction) {

if transaction.payment.productIdentifier == "com.COMPANY.NAME.BUY01"
{
print("Consumable Product 1 Purchased")

}
else if transaction.payment.productIdentifier == "com.COMPANY.NAME.BUY02"
{
print("Consumable Product 2 Purchased")
}
else if transaction.payment.productIdentifier == "com.COMPANY.NAME.BUY03"
{
print("Product 3 purchased")
}
else if transaction.payment.productIdentifier == "com.COMPANY.NAME.BUY04"
{
print("Product 4 purchased")
}

}

//Restore Purchases

func paymentQueueRestoreCompletedTransactionsFinished(queue: SKPaymentQueue) {
print("Transactions Restored")

let purchasedItemIDS = []


for transaction:SKPaymentTransaction in queue.transactions {

if transaction.payment.productIdentifier == "com.COMPANY.NAME.BUY04"
{
print("Consumable Product Purchased")

}

}

let alertController = UIAlertController(title: "Restored Purchases", message: "Your purchases have been restored. ", preferredStyle: .Alert)

let OKAction = UIAlertAction(title: "OK", style: .Default) { (action) in
// ...
}
alertController.addAction(OKAction)

self.presentViewController(alertController, animated: true) {
// ...
}

}

@IBAction func exit(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)

SKPaymentQueue.defaultQueue().removeTransactionObserver(self)

}

}

最佳答案

听起来您没有按照文档 here 调用 finishTransaction :

Transactions stay in the payment queue until they are removed. StoreKit will call your observer’s paymentQueue: updatedTransactions: every time that your app launches or resumes from background until they are removed. To that effect, your customers may be repeatedly asked to authenticate their purchases or be prevented from purchasing your products.

Call finishTransaction: on your transaction to remove it from the queue. Finished transactions are not recoverable. Therefore, be sure to provide your content, to download all Apple-hosted content of a product, or complete your purchase process before finishing your transaction. See Finishing the Transaction for more information.

您知道对 finishTransaction 的两次调用之一实际上正在运行吗?

关于ios - 使用 SKPaymentTransactionObserver 打开 View 后,应用程序不断询问 Apple ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35567115/

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