首页 > 解决方案 > 应用内购买仅在首次应用启动时失败

问题描述

我的应用中有应用内购买。点击按钮后我的购买被激活。但是,如果我第一次打开应用程序并单击按钮,我会得到以下结果:No products found with identifier

从这个函数:

func purchaseProduct(identifier:String) {
    guard let product = self.product(identifier: identifier) else {
        print("No products found with identifier: \(identifier)")
            
        // fire purchase status: failed notification
        delegate?.purchaseStatusDidUpdate(PurchaseStatus.init(state: .failed, error: PurchaseError.productNotFound, transaction: nil, message:"An error occured"))
        return
    }
    //.etc
}

但如果我再次按下按钮,一切正常。此外,如果我关闭应用程序,再次打开并按下按钮 mu 购买工作正常。为什么这只发生在第一次应用启动时?以及如何解决?

我将此代码用于应用内购买https://stackoverflow.com/a/51688015

更新

有以下课程,我认为重点在于:

class MyAppProductStore: Store {
    static let shared = MyAppProductStore()
    
    // purchase processor
    var itunes: iTunesStore = iTunesStore()
    
    init() {
        // register for purchase status update callbacks
        itunes.delegate = self
        
        validateProducts(MyProductIds.allIdentifiers())
    }
    
    /// This function is called during the purchase/restore purchase process with each status change in the flow. If the status is complete then access to the product should be granted at this point.
    ///
    /// - Parameter status: The current status of the transaction.
    internal func processPurchaseStatus(_ status: PurchaseStatus) {
        switch status.state {
        case .initiated:
            // TODO: show alert that purchase is in progress...
            break
        case .complete:
            if let productID = status.transaction?.payment.productIdentifier {
                // Store product id in UserDefaults or some other method of tracking purchases
                UserDefaults.standard.set(true , forKey: productID)
                UserDefaults.standard.synchronize()
            }
        case .cancelled:
            break
        case .failed:
            // TODO: notify user with alert...
            break
        }
    }
}

extension MyAppProductStore: iTunesPurchaseStatusReceiver, iTunesProductStatusReceiver {
    func purchaseStatusDidUpdate(_ status: PurchaseStatus) {
        // process based on received status
        processPurchaseStatus(status)
    }
    
    func restoreStatusDidUpdate(_ status: PurchaseStatus) {
        // pass this into the same flow as purchasing for unlocking products
        processPurchaseStatus(status)
    }
    
    func didValidateProducts(_ products: [SKProduct]) {
        print("Product identifier validation complete with products: \(products)")
        // TODO: if you have a local representation of your products you could
        // sync them up with the itc version here
    }
    
    func didReceiveInvalidProductIdentifiers(_ identifiers: [String]) {
        // TODO: filter out invalid products? maybe... by default isActive is false
    }
}

如果我在调用它后单击按钮,一切都会正常工作。

要调用它,我在其中使用以下行var store = MyAppProductStore()AppDelegate如下所示:

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var store = MyAppProductStore()
.etc

这是最快的上课方式吗?

标签: iosswiftin-app-purchase

解决方案


查看您链接到的代码,您必须先调用fetchStoreProducts,然后在调用“self.product”之前确保产品已加载。可能应该更改原始代码以包含某种在产品加载请求完成时激活的回调。


推荐阅读