// // CPDFOfferSubscriptionStoreView.swift // PDFReaderPro // // Created by zenghong on 1/3/25. // import SwiftUI import StoreKit @available(iOS 18.0, macOS 15.0, tvOS 18.0, watchOS 11.0, visionOS 2.0, *) public struct CPDFOfferSubscriptionStoreView: View { @State private var products: [Product] = [] @State private var tProduct: Product? @State private var showLoading: Bool = true @State private var winBack : Product.SubscriptionOffer? var eligibleOffersCallback: (([String])->Void)? public var body: some View { VStack { if showLoading { // ProgressView("加载订阅信息中...") } else { // SubscriptionStoreView(groupID: "21572797") SubscriptionStoreView(subscriptions: self.products).preferredSubscriptionOffer { product, subscription, eligibleOffers in // print("winBackOffers:\(product.subscription?.winBackOffers)") var string: [String] = [] for offer in eligibleOffers { if let data = offer.id { string.append(data) } } eligibleOffersCallback?(string) return eligibleOffers.first } // .onAppear { // fetchProducts() // } // .frame(maxWidth: .infinity, maxHeight: .infinity) } } .onAppear { fetchProducts() } } public func purchaseWinBackOffer() async { let purchaseOption = Product.PurchaseOption.winBackOffer(winBack!) do { let purchaseResult = try await tProduct!.purchase(options: [purchaseOption]) switch purchaseResult { case .success(let verificationResult): switch verificationResult { case .verified(let transaction): print("购买成功:\(transaction)") await transaction.finish() case .unverified(_, let error): print("验证失败:\(error)") } case .userCancelled: print("用户取消了购买") case .pending: print("购买中") @unknown default: print("未知错误") } } catch { print("发生错误:\(error)") } } public func fetchProducts() { Task { do { // 加载订阅组的产品 let identifiers = Set(["com.pdfreaderpro.mac_free.member.all_access_pack_advanced_6months.001"]) let fetchedProducts = try await Product.products(for:identifiers) // 更新产品信息 DispatchQueue.main.async { self.products = fetchedProducts self.tProduct = fetchedProducts.first self.showLoading = false } } catch { print("无法加载产品: \(error)") self.showLoading = false } } } }