From b0f1a8855762778857f92796540d5f7daaa8df36 Mon Sep 17 00:00:00 2001 From: Pavel Tikhonenko Date: Mon, 11 Oct 2021 03:11:01 +0300 Subject: [PATCH] Retrieve product ids with active subscription --- Sources/Mercato/Mercato.swift | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Sources/Mercato/Mercato.swift b/Sources/Mercato/Mercato.swift index 8bc2633..e4f0a08 100644 --- a/Sources/Mercato/Mercato.swift +++ b/Sources/Mercato/Mercato.swift @@ -135,8 +135,31 @@ extension Mercato { try await AppStore.showManageSubscriptions(in: scene) } + + public static func activeSubscriptions(onlyRenewable: Bool = true) async throws -> [String] + { + var productIds: Set = [] + + for await result in Transaction.currentEntitlements + { + do { + let transaction = try checkVerified(result) + + if transaction.productType == .autoRenewable || + (!onlyRenewable && transaction.productType == .nonRenewable) + { + productIds.insert(transaction.productID) + } + } catch { + throw error + } + } + + return Array(productIds) + } } + func checkVerified(_ result: VerificationResult) throws -> T { switch result