swift StoreKit 2:如何验证活动订阅?

r7s23pms  于 2023-08-02  发布在  Swift
关注(0)|答案(2)|浏览(332)

StoreKit 2周围的许多tutorials,甚至Apple's own Sample Code都引用了“Xcode中的StoreKit测试”,这样你就可以构建和运行示例应用程序,而无需在App Store Connect中完成任何设置。该项目在Products.storekit文件中为StoreKit测试服务器定义应用内产品。”我有一个应用程序,已在App Store Connect中设置了自动续订订阅(从SwiftyStoreKit迁移到StoreKit 2)...我如何设置此StoreKitManager类来检查活动订阅,而无需创建单独的Products.plist文件?下面这段代码主要基于Apple的示例代码,结果是Error Domain=ASDErrorDomain Code=509 "No active account",这很明显,因为我不知道如何将我的产品连接到StoreKit 2逻辑上?🤔
编辑:这是我的代码的gist

import Foundation
import StoreKit

typealias Transaction = StoreKit.Transaction

public enum StoreError: Error {
    case failedVerification
}

@available(watchOSApplicationExtension 8.0, *)
class WatchStoreManager: ObservableObject {
    
    var updateListenerTask: Task<Void, Error>? = nil
    
    init() {
        print("Init called in WatchStoreManager")
        //Start a transaction listener as close to app launch as possible so you don't miss any transactions.
        updateListenerTask = listenForTransactions()
    }
    
    deinit {
        updateListenerTask?.cancel()
    }
    
    func listenForTransactions() -> Task<Void, Error> {
        return Task.detached {
            //Iterate through any transactions that don't come from a direct call to `purchase()`.
            for await result in Transaction.updates {
                do {
                    let transaction = try self.checkVerified(result)
                    
                    print("we have a verified transacction")

                    //Deliver products to the user.
                    //TODO:
                    //await self.updateCustomerProductStatus()

                    //Always finish a transaction.
                    await transaction.finish()
                } catch {
                    //StoreKit has a transaction that fails verification. Don't deliver content to the user.
                    print("Transaction failed verification")
                }
            }
        }
    }
    
    func checkVerified<T>(_ result: VerificationResult<T>) throws -> T {
        //Check whether the JWS passes StoreKit verification.
        switch result {
        case .unverified:
            //StoreKit parses the JWS, but it fails verification.
            throw StoreError.failedVerification
        case .verified(let safe):
            //The result is verified. Return the unwrapped value.
            return safe
        }
    }
    
}

字符串

bvn4nwqk

bvn4nwqk1#

第509章:没有活动帐户
表示用户未登录应用商店。转到Settings -> Sign in to Your iPhone并使用有效的App Store或沙盒帐户登录
编辑:我看到你在你的设备上登录-这很奇怪。你提到你没有链接你的产品。您需要从App Store中获取产品,类似于以下内容。但我不希望你看到那个特定的错误信息...

enum AppProduct: String, CaseIterable, Identifiable {
        case noAds = "MYUNIQUEIDENTIFIER_ESTABLISHEDIN_APPSTORECONNECT"

        var id: String {
            UUID().uuidString
        } // to make it Identifiable for use in a List

        static var allProductIds: [String] {
            return Self.allCases.map { $0.rawValue }
        } // convenience
    }

    @MainActor
    @discardableResult func fetchProducts() async throws -> [Product] {
        let products = try await Product.products(for: AppProduct.allProductIds) // this is the call that fetches products
        guard products.first != nil else { throw PurchaseError.unknown } // custom error
        self._storeProducts = products
        let fetchedProducts: [AppProduct] = products.compactMap {
            let appProduct = AppProduct(rawValue: $0.id)
            return appProduct
        }
        self.fetchedProducts = fetchedProducts
        try await checkPurchased()
        return products
    }

    private func checkPurchased() async throws {
        for product in _storeProducts {
            guard let state = await product.currentEntitlement else { continue }
            let transaction = try self.checkVerified(state)
            //Always finish a transaction.
            await transaction.finish()
        }
    }

字符串
我正在将购买状态设置为checkVerified,当验证通过时...

vlju58qv

vlju58qv2#

对我来说,我的Xcode项目的Bundle id与appstoreConnect的Bundle id不完全匹配。我在Xcode上更改了它,在appstoreconnect上没有更新。
奇怪的是,我的本地测试配置存储文件同步得很好,一切正常。
一般来说(对于StoreKit 2的沙盒测试),我当然会检查:- 您已经在appstoreconnect上创建了应用和应用内购买。- 您已同意AppstoreConnect上的所有协议-协议、税收等。- 产品标识符和捆绑包ID匹配。
这就是我试图解决这个问题所得到的,看到其他人的问题和对他们有效的东西。希望有人能得到这个有用的!

相关问题