应用内购买成功,但没有调用方法

In-App Purchase is successful, but methods are not called

我正在测试推广应用内购买,如here所述。

工作原理如下:

  1. 我构建系统URL。
  2. 我将系统 URL link 发送到我的设备。
  3. 我点击 link。
  4. 我的应用自动打开,出现应用内购买支付sheet。
  5. 我点击“购买”按钮(或其他名称),然后输入我的密码以完成交易。
  6. 交易似乎是成功的,并且会出现一条提示,“您已准备就绪。您的购买已成功”。

问题是,某些代码应该 运行 取决于事务的状态,但似乎没有。我不确定这怎么可能,但就是这样。

因此,deferredTransactionHandlerhandlePurchasehandleFailedPurchasehandleRestoredPurchase 方法似乎未被调用。

class StoreObserver: NSObject, SKPaymentTransactionObserver, SKProductsRequestDelegate { 
    var productRequest: SKProductsRequest?
    var isAuthorizedForPayments: Bool {
        return SKPaymentQueue.canMakePayments()
    }
    var products = [SKProduct]()


    func paymentQueue(_ queue: SKPaymentQueue, shouldAddStorePayment payment: SKPayment, for product: SKProduct) -> Bool {
        //The user has initiated a promoted In-App Purchase directly from the app's product page.

        //Show the UI:
        SceneDelegate.mainData.showAppStorePromotionUI = true
        
        //"Return true to continue the transaction in your app. Return false to defer or cancel the transaction."
        return true
    }

    func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
        for transaction in transactions {
            switch transaction.transactionState {
                case .purchasing: break
                case .deferred: deferredTransactionHandler(transaction)
                case .purchased: handlePurchase(transaction)
                case .failed: handleFailedPurchase(transaction)
                case .restored: handleRestoredPurchase(transaction)
                @unknown default: fatalError("unknownPaymentTransaction")
             }
          }
    }
}

我怎么知道这些方法没有被调用? 1) 在这些方法中,我更新了 UI 以反映事务已完成(不过 UI 从未更新过),以及 2) 在这些方法中,我尝试显示一个警报(但是警报永远不会出现)。我是这样做的:

func handlePurchase(_ transaction: SKPaymentTransaction) {
    //Update the UI
    SceneDelegate.mainData.showAppStorePromotionUI = false

    //Present an alert so you know this function ran
    let alert = UIAlertController(title: "Alert", message: "handlePurchase()", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
            
    }))
    let topViewController = UIApplication
            .shared
            .connectedScenes
            .flatMap { ([=11=] as? UIWindowScene)?.windows ?? [] }
            .first { [=11=].isKeyWindow }?.rootViewController
        
    topViewController?.present(alert, animated: true, completion: nil)

    //Complete the transaction  
    SKPaymentQueue.default().finishTransaction(transaction)
}

为什么这些函数好像没有被调用?我错过了什么?

谢谢!

唉。这是我的一个愚蠢错误。

我一直在测试一些东西,忘记删除我 SceneDelegatesceneWillResignActive(_:) 中删除观察者的一行。因此,每次支付 sheet 时,观察者都会被移除,这会把一切都搞砸。

func sceneWillResignActive(_ scene: UIScene) {
    //Removing this line solved the problem
    //SKPaymentQueue.default().remove(SceneDelegate.inAppPurchases)
}