跳出 For 语句并仍然允许完成处理程序成功完成

Breaking out of a For statement and still allowing a completion handler to complete successfully

我有一个正在运行的 RevenueCat 实施,但我不喜欢这个流程,并且正在努力改进它。我知道必须有更完善的方法来做到这一点,所以任何帮助将不胜感激:

@IBAction func btnMnthlyPressed(_ sender: Any) {
    Purchases.shared.offerings { (offerings, error) in
        if let e = error {
            print(e.localizedDescription)
        }
        
        guard let offering = offerings?.current else {
            print("No current offering configured")
            return
        }
        
        for package in offering.availablePackages {
            print(package.identifier)
            if package.identifier == "$rc_monthly" {
                Purchases.shared.purchasePackage(package) { (transaction, info, error, cancelled) in
                    if cancelled {
                        print("User cancelled purchase")
                        return
                    }
                    // Optionally handle specific purchase errors
                     if info?.entitlements.all["FullAccess"]?.isActive == true {
                        print("Unlocked Pro Cats ")
                    }
                }
            }
            print("Product: \(package.product.localizedDescription), price: \(package.localizedPriceString)")
        }
    }
}

也许我在函数中尝试做的太多了...

获取第一个包月套餐可能比 for 循环更好,因为您不想在同一个按钮按下时启动两个购买电话。

@IBAction func btnMnthlyPressed(_ sender: Any) {
    Purchases.shared.offerings { (offerings, error) in
        if let e = error {
            print(e.localizedDescription)
        }
        
        guard let offering = offerings?.current else {
            print("No current offering configured")
            return
        }
        
        guard let package = offering.availablePackages.first(where: { [=10=].packageType == .monthly }) else {
            print("No monthly package type")
            return
        }
        
        Purchases.shared.purchasePackage(package) { (transaction, info, error, cancelled) in
            if cancelled {
                print("User cancelled purchase")
                return
            }
            // Optionally handle specific purchase errors
             if info?.entitlements.all["FullAccess"]?.isActive == true {
                print("Unlocked Pro Cats ")
            }
        }
    }
}

也就是说,您可能希望在加载视图时获取所有包,以确保它们在用户尝试点击购买按钮之前存在。