首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >StoreKit 2:如何验证活动订阅?

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

Stack Overflow用户
提问于 2022-10-04 20:21:50
回答 1查看 280关注 0票数 1

许多围绕教程 2甚至苹果自己的示例代码苹果自己的示例代码引用了“Xcode中的StoreKit测试,这样您就可以构建和运行示例应用程序,而无需在around中完成任何设置。该项目在Products.storekit文件中为StoreKit测试服务器定义了应用程序中的产品。”我有一个在active (从SwiftyStoreKit迁移到SwiftyStoreKit 2)中已经设置了自动更新订阅的应用程序,我可以设置这个StoreKitManager类来检查活动订阅,而不需要创建一个单独的Products.plist文件吗?下面的代码,主要是基于苹果的示例代码,结果是Error Domain=ASDErrorDomain Code=509 "No active account",这很明显,因为我不知道如何将我的产品连接到StoreKit 2逻辑?

编辑:这是我的代码的要旨

代码语言:javascript
运行
复制
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
        }
    }
    
}
EN

回答 1

Stack Overflow用户

发布于 2022-11-06 17:19:58

509:无活动账户

意味着用户没有登录到应用程序商店。转到Settings -> Sign in to Your iPhone并使用有效的App或沙箱帐户登录

编辑:我看到你在你的设备上登录-这很奇怪。你说过你还没把你的产品联系起来。您需要从App获取类似于以下内容的产品。但我不希望你看到具体的错误信息..。

代码语言:javascript
运行
复制
    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中设置购买状态.

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73953272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档