productsRequest 从未在应用程序购买中调用 swift 5

Posted

技术标签:

【中文标题】productsRequest 从未在应用程序购买中调用 swift 5【英文标题】:productsRequest never called in app purchases swift 5 【发布时间】:2020-12-01 11:13:54 【问题描述】:

我是 swift 新手,所以我正在创建一个新应用程序。我正在尝试在我的应用程序中实现应用程序购买,但从未调用过 productsRequest 方法。我试图关注所有其他堆栈溢出响应,但没有任何改变。谁能帮我?非常感谢。

这是我的 RemoveAdsManager.swift:

import Foundation
import StoreKit

public class RemoveAdsManager 

     class func removeAds()
         let iap = IAPManager(productID: IAPManager.removeAdsID)
         iap.beginPurchase()
     

     class func restoreRemoveAds()
         let iap = IAPManager(productID: IAPManager.removeAdsID)
         iap.beginRestorePurchases()
     

     class func areAdsRemoved() -> Bool
         return UserDefaults.standard.bool(forKey: "pro_version")
     

     class func removeAdsSuccess()
         UserDefaults.standard.set(true, forKey: "pro_version")
         UserDefaults.standard.synchronize()
     

     class func restoreRemoveAdsSuccess()
         removeAdsSuccess()
     

     class func removeAdsFailure()

     

     class func removeAdsDeferred()

     

 

这是我的 IAPManager.swift:

import Foundation
import StoreKit

public class IAPManager: NSObject, SKProductsRequestDelegate, SKPaymentTransactionObserver
    static let removeAdsID = "com.******.*****.******"

    let productID: String
    init(productID: String)
        self.productID = productID
    

    public func beginPurchase()
        if SKPaymentQueue.canMakePayments()
            let request = SKProductsRequest(productIdentifiers: [productID])
            request.delegate = self
            request.start()
        
        else
        
    

    public func beginRestorePurchases()
        SKPaymentQueue.default().add(self)
        SKPaymentQueue.default().restoreCompletedTransactions()
    

    public func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse)
        if let product = response.products.first
            let payment = SKPayment(product: product)
            SKPaymentQueue.default().add(self)
            SKPaymentQueue.default().add(payment)
        
        else
            RemoveAdsManager.removeAdsFailure()
        
    

    public func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue)
        
        for transaction in queue.transactions
            if transaction.transactionState == .restored
                let productID = transaction.payment.productIdentifier

                if(productID.lowercased() == IAPManager.removeAdsID.lowercased())
                    RemoveAdsManager.restoreRemoveAdsSuccess()
                

                SKPaymentQueue.default().finishTransaction(transaction)
            
        
    

    public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction])
        for transaction in transactions
            let productID = transaction.payment.productIdentifier

            switch transaction.transactionState
            case .purchasing:
                break
            case .purchased:
                RemoveAdsManager.removeAdsSuccess()
                SKPaymentQueue.default().finishTransaction(transaction)
            case .restored:
                RemoveAdsManager.restoreRemoveAdsSuccess()
                SKPaymentQueue.default().finishTransaction(transaction)
            case .failed:
                RemoveAdsManager.removeAdsFailure()
                SKPaymentQueue.default().finishTransaction(transaction)
            case .deferred:
                RemoveAdsManager.removeAdsDeferred()
                break
            
        
    


最后,这是用户可以按下以购买应用内的按钮:

@IBAction func didTapBuy() 
        RemoveAdsManager.removeAds()
    

非常感谢您的所有帮助。我正在努力学习,所以欢迎任何回应。

【问题讨论】:

let iap = IAPManager(productID: IAPManager.removeAdsID) 听起来很奇怪。这是一种保留周期。 @ElTomato 你知道我该如何改变它吗? 为产品及其 AppleID 创建一个简单的结构。并摆脱 RemoveAdsManager。相反,通过视图控制器使用 IAPManager。 【参考方案1】:

removeAds() 似乎调用 beginPurchase() 在这一点上你应该调用类似的东西

let payment = SKMutablePayment(product: product)
SKPaymentQueue.default().add(payment)

【讨论】:

以上是关于productsRequest 从未在应用程序购买中调用 swift 5的主要内容,如果未能解决你的问题,请参考以下文章

应用内购买适用于 iOS 3 和 5,但不适用于 iOS 4

StoreKit SKProducts请求崩溃

通过 productsRequest:didReceiveResponse 返回的 SKProduct 在生产中没有 .introductoryPrice,但沙盒符合预期

onIabPurchaseFinished 从未调用过。

iOS - 应用内购买 - 恢复按钮?

在应用程序钥匙串中存储应用内购买收据