gpt4 book ai didi

swift - 我的恢复购买代码是否正确?如果正确,我应该在哪里执行更改用户默认值的操作?

转载 作者:行者123 更新时间:2023-11-30 12:19:39 25 4
gpt4 key购买 nike

我正在用 Swift 编写我的第一个 iOS 应用程序,但应用程序内购买功能遇到了一些问题。我认为我已经可以使用实际的购买功能,但我不确定恢复功能。我只是提供一种产品,它是应用程序的“专业”版本。它存储在“用户默认值”位置,并通过它解锁额外的功能。我的恢复功能让我感到困惑,有人可以看一下它并告诉我应该在哪里设置用户默认值吗?它应该在 paymentQueueRestoreCompletedTransactionsFinished 函数内还是 paymentQueue 函数内?

import UIKit
import StoreKit

class StoreController: UIViewController, SKProductsRequestDelegate,
SKPaymentTransactionObserver {

// Properties
var defaults = UserDefaults.standard
var list = [SKProduct]()
var p = SKProduct()

// Methods
override func viewDidLoad() {
super.viewDidLoad()
// Get list of products for the store
localTitle.isEnabled = false
localDescription.isEnabled = false
price.isEnabled = false
buy.isEnabled = false
restore.isEnabled = false
getProducts()
}

// Outlets
@IBOutlet weak var localTitle: UILabel!
@IBOutlet weak var localDescription: UILabel!
@IBOutlet weak var price: UILabel!
@IBOutlet weak var buy: UIButton!
@IBOutlet weak var restore: UIButton!

// Actions
@IBAction func buy(_ sender: UIButton) {
print("buy pressed")
for product in list {
let prodID = product.productIdentifier
if(prodID == "com.squidgylabs.pro") {
p = product
buyProduct()
}
}
}

@IBAction func restore(_ sender: UIButton) {
print("restore pressed")
SKPaymentQueue.default().add(self)
SKPaymentQueue.default().restoreCompletedTransactions()
}

// Methods
func getProducts() {
print("starting getproducts function")
if(SKPaymentQueue.canMakePayments()) {
print("IAP is enabled, loading")
let productID: NSSet = NSSet(objects: "com.squidgylabs.pro")
let request: SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>)
request.delegate = self
request.start()
} else {
print("please enable IAPS")
}
}


func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
print("starting productsrequest")
let myProduct = response.products
for product in myProduct {
print("product added")
list.append(product)
}
localTitle.isEnabled = true
localDescription.isEnabled = true
restore.isEnabled = true
buy.isEnabled = true
price.isEnabled = true

// Update labels
localTitle.text = list[0].localizedTitle
localDescription.text = list[0].localizedDescription
// Format the price and display
let formatter = NumberFormatter()
formatter.locale = Locale.current
formatter.numberStyle = .currency
if let formattedPrice = formatter.string(from: list[0].price){
price.text = formattedPrice
}
}


func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
print("starting paymentQueueResoreCompletedTransactionsFnished")
for transaction in queue.transactions {
let t: SKPaymentTransaction = transaction
let prodID = t.payment.productIdentifier as String

switch prodID {
case "com.squidgylabs.pro":
print("case is correct product ID in payment...finished")

default:
print("IAP not found")
}
}
}

func buyProduct() {
print("buy " + p.productIdentifier)
let pay = SKPayment(product: p)
SKPaymentQueue.default().add(self)
SKPaymentQueue.default().add(pay as SKPayment)
}

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
print("entering func paymentQueue")

for transaction: AnyObject in transactions {
let trans = transaction as! SKPaymentTransaction

switch trans.transactionState {
case .purchased:
print("buy ok, unlock IAP HERE")

print(p.productIdentifier)
let prodID = p.productIdentifier
switch prodID {
case "com.squidgylabs.pro":
print("setting pro in defaults...")
defaults.set(true, forKey: "pro")
default:
print("IAP not found")
}
queue.finishTransaction(trans)
break
case .failed:
print("buy error")
queue.finishTransaction(trans)
break
case .restored:
print("case .restored in paymentQ")
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
queue.finishTransaction(trans)
break
default:
print("Default")
break
}
}
}
}

最佳答案

可以这样完成。

public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case .purchased:
completePurchaseForTransaction(transaction)
case .restored:
completePurchaseForTransaction(transaction)
default:
break
}
}

private func completePurchaseForTransaction(_ transaction: SKPaymentTransaction) {
let productIdentifier = transaction.payment.productIdentifier
// Update UserDefaults here
SKPaymentQueue.default().finishTransaction(transaction)
}

您可能会考虑使用其他类来访问 StoreKit,而不是 UIViewController。因为

Your application should always expect to be notified of completed transactions.

https://developer.apple.com/documentation/storekit/skpaymentqueue/1506042-add

View Controller 来来去去。如果您不删除作为观察者添加到 SKPaymentQueue 的对象,它甚至可能会使应用程序崩溃。

我在 GitHub 上有一个简单且文档齐全的 StoreKit 库,您可以查看它来了解想法 -- https://github.com/suvov/VSStoreKit

关于swift - 我的恢复购买代码是否正确?如果正确,我应该在哪里执行更改用户默认值的操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44956505/

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