Skip to content

Instantly share code, notes, and snippets.

@yccheok
Created March 16, 2025 06:09
Show Gist options
  • Save yccheok/84ded2a99c050f4146e93b9449d02a21 to your computer and use it in GitHub Desktop.
Save yccheok/84ded2a99c050f4146e93b9449d02a21 to your computer and use it in GitHub Desktop.
func isInActiveCanceledTrial() async throws -> Bool {
let currentDate = Date()
for await verificationResult in Transaction.currentEntitlements {
guard case .verified(let transaction) = verificationResult else { continue }
guard let expirationDate = transaction.expirationDate else { continue }
// Check if it's an active trial
if transaction.offer?.type == .introductory {
// Check if it's still active (current date is before expiration)
let isActive = expirationDate > currentDate
if isActive {
if let product = try await StoreKit.Product.products(for: [transaction.productID]).first,
let statuses = try await product.subscription?.status {
var isActiveButCanceled: Bool = false
try statuses.forEach {
let info = try verifyPurchase($0.renewalInfo)
if $0.state == .subscribed && info.willAutoRenew == false {
isActiveButCanceled = true
}
}
return isActiveButCanceled
}
}
return false
}
return false
}
return false
}
func verifyPurchase<T>(_ result: VerificationResult<T>) throws -> T {
switch result {
case .unverified:
throw CaffeinePalStoreFrontError.failedVerification
case .verified(let safe):
return safe
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment