From e100bd36f96e57ca29b08f501453465bb107b9a0 Mon Sep 17 00:00:00 2001 From: Toni Rico Date: Thu, 14 Sep 2023 18:19:18 +0200 Subject: [PATCH] Fix runtime crash in SK2TransactionListener in iOS < 15 (#3206) ### Description We found that there was another runtime crash in Capacitor in iOS < 15 similar to https://github.com/RevenueCat/purchases-ios/pull/3139. This applies the same fix to this new crash. --- .../StoreKit2/StoreKit2TransactionListener.swift | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Sources/Purchasing/StoreKit2/StoreKit2TransactionListener.swift b/Sources/Purchasing/StoreKit2/StoreKit2TransactionListener.swift index dada7b4029..7afc46473d 100644 --- a/Sources/Purchasing/StoreKit2/StoreKit2TransactionListener.swift +++ b/Sources/Purchasing/StoreKit2/StoreKit2TransactionListener.swift @@ -58,7 +58,15 @@ actor StoreKit2TransactionListener: StoreKit2TransactionListenerType { private(set) var taskHandle: Task? private weak var delegate: StoreKit2TransactionListenerDelegate? - private let updates: AsyncStream + + // We can't directly store instances of `AsyncStream`, since that causes runtime crashes when + // loading this type in iOS <= 15, even with @available checks correctly in place. + // See https://openradar.appspot.com/radar?id=4970535809187840 / https://github.com/apple/swift/issues/58099 + private let _updates: Box> + + var updates: AsyncStream { + return self._updates.value + } init(delegate: StoreKit2TransactionListenerDelegate? = nil) { self.init(delegate: delegate, updates: StoreKit.Transaction.updates) @@ -71,7 +79,7 @@ actor StoreKit2TransactionListener: StoreKit2TransactionListenerType { updates: S ) where S.Element == TransactionResult { self.delegate = delegate - self.updates = updates.toAsyncStream() + self._updates = .init(updates.toAsyncStream()) } func set(delegate: StoreKit2TransactionListenerDelegate) {