gpt4 book ai didi

ios - 如何在 Swift 中检查用户是否已在应用内购买中支付自动续订订阅费用

转载 作者:行者123 更新时间:2023-12-02 04:42:19 27 4
gpt4 key购买 nike

我目前在 iTunesConnect 中设置了非消耗品自动续订订阅应用内购买。我的问题是,我不确定如何检查是否可以为用户解锁自动续订订阅的内容。我对应用程序购买中的非消耗品没有任何问题,我通过检查UserDefaults中是否存在产品ID来验证它们,如果存在,我会解锁内容,否则我通知用户,但此方法不适用于自动续订订阅应用内购买。当我测试它时,我可以通过应用程序商店进行购买交易,但是当我尝试查看 UserDefaults 中是否存在产品 ID 时,它返回 false。事实上,我手动检查了 key 是否存在,但不存在,它只显示 Non-Consumable 购买的 key 。

这是我正在使用的代码。

这是我多年来一直使用的工作代码,用于验证应用购买中的Non-Consumable

这是我正在使用的In App Manager 类。

import UIKit
import StoreKit

protocol IAPManagerDelegate {
func managerDidRestorePurchases()
}

class IAPManager: NSObject, SKProductsRequestDelegate, SKPaymentTransactionObserver, SKRequestDelegate {
static let sharedInstance = IAPManager()
var request:SKProductsRequest!
var products:NSArray!
var delegate:IAPManagerDelegate?

func setupInAppPurchases(){
self.validateProductIdentifiers(self.getProductIdentifiersFromMainBundle())

SKPaymentQueue.default().add(self)
}

func getProductIdentifiersFromMainBundle() -> NSArray {
var identifiers = NSArray()
if let url = Bundle.main.url(forResource: "iap_product_ids", withExtension: "plist"){
identifiers = NSArray(contentsOf: url)!
}
return identifiers
}

func validateProductIdentifiers(_ identifiers:NSArray) {
let productIdentifiers = NSSet(array: identifiers as [AnyObject])
let productRequest = SKProductsRequest(productIdentifiers: productIdentifiers as! Set<String>)
self.request = productRequest
productRequest.delegate = self
productRequest.start()
}

func createPaymentRequestForProduct(_ product:SKProduct){
let payment = SKMutablePayment(product: product)
payment.quantity = 1
SKPaymentQueue.default().add(payment)
}

func verifyReceipt(_ transaction:SKPaymentTransaction?){
let receiptURL = Bundle.main.appStoreReceiptURL!
if let receipt = try? Data(contentsOf: receiptURL){

let requestContents = ["receipt-data" : receipt.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))]

do {
let requestData = try JSONSerialization.data(withJSONObject: requestContents, options: JSONSerialization.WritingOptions(rawValue: 0))
// PRODUCTION URL
// let storeURL = URL(string: "https://buy.itunes.apple.com/verifyReceipt")
// TESTING URL: Uncomment for testing InAppPurchases
let storeURL = URL(string: "https:/sandbox.itunes.apple.com/verifyReceipt")

var request = URLRequest(url: storeURL!)
request.httpMethod = "Post"
request.httpBody = requestData

let session = URLSession.shared
let task = session.dataTask(with: request,
completionHandler: { (responseData, response, error) -> Void in
do {
let json = try JSONSerialization.jsonObject(with: responseData!, options: .mutableLeaves) as! NSDictionary
if (json.object(forKey: "status") as! NSNumber) == 0 {
if let latest_receipt = json["latest_receipt_info"]{
self.validatePurchaseArray(latest_receipt as! NSArray)
} else {
let receipt_dict = json["receipt"] as! NSDictionary
if let purchases = receipt_dict["in_app"] as? NSArray{
self.validatePurchaseArray(purchases)
}
}
if transaction != nil {
SKPaymentQueue.default().finishTransaction(transaction!)
}
DispatchQueue.main.sync(execute: { () -> Void in
self.delegate?.managerDidRestorePurchases()
})
} else {
print(json.object(forKey: "status") as! NSNumber)
}
} catch {
print(error)
}
})
task.resume()
} catch {
print(error)
}
} else {
print("No Receipt")
}
}

func validatePurchaseArray(_ purchases:NSArray){
for purchase in purchases as! [NSDictionary]{
self.unlockPurchasedFunctionalityForProductIdentifier(purchase["product_id"] as! String)
}
}

func unlockPurchasedFunctionalityForProductIdentifier(_ productIdentifier:String){
UserDefaults.standard.set(true, forKey: productIdentifier)
UserDefaults.standard.synchronize()
DispatchQueue.main.async {
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}
}

func lockPurchasedFunctionalityForProductIdentifier(_ productIdentifier:String){
UserDefaults.standard.set(false, forKey: productIdentifier)
UserDefaults.standard.synchronize()
UIApplication.shared.isNetworkActivityIndicatorVisible = false
}

func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
let inAppPurchases = response.products
// Sort the items
self.products = inAppPurchases.reversed() as NSArray
}

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions as [SKPaymentTransaction]{
switch transaction.transactionState{
case .purchasing:
print("Purchasing")
UIApplication.shared.isNetworkActivityIndicatorVisible = true
case .deferred:
print("Deferrred")
UIApplication.shared.isNetworkActivityIndicatorVisible = false
case .failed:
print("Failed")
//print(transaction.error?.localizedDescription)
UIApplication.shared.isNetworkActivityIndicatorVisible = false
SKPaymentQueue.default().finishTransaction(transaction)
case.purchased:
print("Purchased")
self.verifyReceipt(transaction)
case .restored:
print("Restored")

}
}
}

func restorePurchases(){
let request = SKReceiptRefreshRequest()
request.delegate = self
request.start()
}

func requestDidFinish(_ request: SKRequest) {
self.verifyReceipt(nil)
}
}

以下是我在 UITableView 中呈现应用内购买的方式。

class StoreTableViewController: UIViewController, UITableViewDataSource, UITableViewDelegate, IAPManagerDelegate {
@IBOutlet weak var premiumFeaturesTable: UITableView!
@IBOutlet weak var buttonClose: UIButton!
@IBOutlet weak var buttonRestore: UIButton!

override func viewDidLoad() {
super.viewDidLoad()
IAPManager.sharedInstance.delegate = self
}

func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return IAPManager.sharedInstance.products.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellInAppPurchase")as! CustomCellForInAppPurchasesTableViewCell
let product = IAPManager.sharedInstance.products.object(at: indexPath.row) as! SKProduct

cell.labelIAppItem.text = product.localizedTitle
return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
IAPManager.sharedInstance.createPaymentRequestForProduct(IAPManager.sharedInstance.products.object(at: indexPath.row) as! SKProduct)
}

@IBAction func closeViewController(_ sender: AnyObject) {
self.presentingViewController!.dismiss(animated: true, completion: nil)
}

@IBAction func restorePurchases(_ sender: AnyObject) {
IAPManager.sharedInstance.restorePurchases()
}
}

这是我解锁内容的方法

if NSUserDefaults.standardUserDefaults().boolForKey("com.theAppID.app"){
// Unlock content.
}else{
// Notify user.
}

同样,一切都适用于非消耗品,但对于自动续订订阅,我不确定如何在用户购买后解锁内容。

我缺少什么,根据上述代码检查用户是否已支付自动续订订阅的正确方法是什么?

编辑:最简单的答案是...将 RevenueCat 用于基于订阅的应用程序,它让您的生活更轻松。

最佳答案

请检查此link用于自动续订订阅。

您可以使用应用内管理器类中的以下函数。

func verifyReceipt(_ transaction:SKPaymentTransaction?) 

验证后,您将收到以下响应代码以及您最后订阅日期的详细信息。请检查这个link .

注意:不要忘记在自动续订订阅的收据验证中传递“密码”字段。

关于ios - 如何在 Swift 中检查用户是否已在应用内购买中支付自动续订订阅费用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56319444/

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