1

我将 SwiftyStoreKit 与 SwiftUI 和 App 而不是 App Delegate 一起使用,并在控制台中收到此消息:“SwiftyStoreKit.completeTransactions() 应仅在应用启动时调用一次。” 我相信它,因为我在下面的块中调用了 completeTransactions,我可以从 Print 语句中看到,它比以前调用 AppDelegate 的 didFinishLaunching 更频繁。我关注此消息的原因是我在尝试购买时因崩溃而被 App Review 拒绝。我还应该在哪里调用 completeTransactions?

     .onChange(of: scenePhase) { newScenePhase in
                switch newScenePhase {
                case .active:
                    print("App is active")

  SwiftyStoreKit.completeTransactions(atomically: true) { purchases in
            
            for purchase in purchases {
                switch purchase.transaction.transactionState {
                case .purchased, .restored:
                    let downloads = purchase.transaction.downloads
                    if !downloads.isEmpty {
                        SwiftyStoreKit.start(downloads)
                    } else if purchase.needsFinishTransaction {
                        // Deliver content from server, then:
                        SwiftyStoreKit.finishTransaction(purchase.transaction)
                    }
                    print("\(purchase.transaction.transactionState.debugDescription): \(purchase.productId)")
                case .failed, .purchasing, .deferred:
                    break // do nothing
                @unknown default:
                    break // do nothing
                }
            }
        }
                    
4

1 回答 1

3

尝试在 app main init 中执行此操作,例如

@main
struct MyApp: App {

    init() {
        SwiftyStoreKit.completeTransactions(atomically: true) { purchases in
           // ... other code here
        }
    }

    var body: some Scene {
        WindowGroup {
           ContentView()
        }
    }
}

注意:如果为时过早,您可以定义应用程序委托适配器(如这里https://stackoverflow.com/a/62538373/12299030)并调用SwiftyStoreKit确实完成启动回调。

于 2021-01-11T17:07:47.913 回答