У меня есть следующий код < /p>
if let selectProduct = products.first(where: { $0.id == productId }) {
await PurchaseManager.shared.purchase(selectProduct) { result in
c
switch result {
case .success:
Amplitude.sharedInstance.track(
eventType: "payment_completed",
eventProperties: ["PlanId": productId, "Source": source]
)
self.loadingIndicator.removeFromSuperview()
await self.purchase(
vc: vc,
productId: selectProduct.id,
product:selectProduct.id
)
case .failure(let error):
if (error.localizedDescription == "Purchase was cancelled.") {
Amplitude.sharedInstance.track(
eventType: "payment_cancelled",
eventProperties: ["PlanId": productId, "Source": source]
)
self.loadingIndicator.removeFromSuperview()
} else {
Amplitude.sharedInstance.track(
eventType: "payment_failed",
eventProperties: ["PlanId": productId, "Source": source, "ErrorCode": error.code, "ErrorDescription": error.localizedDescription]
)
self.loadingIndicator.removeFromSuperview()
showMessageWithTitle("Error!", "There was an error processing your request", .error)
}
}
}
}
}
< /code>
bookasemanager < /p>
import StoreKit
@MainActor
class PurchaseManager: NSObject {
static let shared = PurchaseManager()
var products: [Product] = []
var purchasedProductIDs: Set = []
private var transactionListener: Task? = nil
override init() {
super.init()
transactionListener = listenForTransactions()
}
/// Fetch available products
func fetchProducts() async {
do {
let productIDs: Set = ["com.yourapp.product1", "com.yourapp.product2", "com.yourapp.product3"]
products = try await Product.products(for: productIDs)
} catch {
print("Failed to fetch products: \(error.localizedDescription)")
}
}
/// Purchase a product
func purchase(_ product: Product, completion: @escaping (Result) -> Void) async {
do {
let result = try await product.purchase()
switch result {
case .success(let verification):
if case .verified(let transaction) = verification {
print("Purchase successful for \(transaction.productID)")
purchasedProductIDs.insert(transaction.productID)
await transaction.finish()
completion(.success(()))
} else {
completion(.failure(PurchaseError.failed("Transaction verification failed.")))
}
case .userCancelled:
completion(.failure(PurchaseError.failed("Purchase was cancelled.")))
case .pending:
completion(.failure(PurchaseError.failed("Purchase is pending.")))
@unknown default:
completion(.failure(PurchaseError.failed("Unknown error occurred.")))
}
} catch {
completion(.failure(PurchaseError.failed(error.localizedDescription)))
}
}
/// Listen for transactions in the background
private func listenForTransactions() -> Task {
Task {
for await result in Transaction.updates {
if case .verified(let transaction) = result {
purchasedProductIDs.insert(transaction.productID)
await transaction.finish()
}
}
}
}
}
enum PurchaseError: Error {
case failed(String)
}
< /code>
Я получаю ошибку «Не удается пройти функцию типа '@sendable () async -> void' к параметрам, ожидая типа синхронной функции» в строке "dispatchqueue.main.async {" Can Can Кто -то помогает.
Я пытаюсь обработать платежи, используя storekit2.
Подробнее здесь: https://stackoverflow.com/questions/794 ... ction-type
@Sendable () async -> void 'to parameter Ожидая типа синхронной функции [закрыто] ⇐ IOS
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение
-
-
Scrapy + Playwright: вызов синхронной функции parse_single из асинхронной функции анализа
Anonymous » » в форуме Python - 0 Ответы
- 16 Просмотры
-
Последнее сообщение Anonymous
-