Simple Migration Guide

Here's the simplest way to switch from StoreKit to KeepIAP:

1

1. Current StoreKit Code

This is your current StoreKit code:

// Your current StoreKit code
class StoreManager: ObservableObject {
    @Published var products: [Product] = []
    
    func loadProducts() async {
        let products = try await Product.products(for: ["premium.monthly", "premium.yearly"])
        self.products = products
    }
    
    func purchase(_ product: Product) async throws {
        let result = try await product.purchase()
        switch result {
        case .success(let verification):
            let transaction = try checkVerified(verification)
            await transaction.finish()
        case .userCancelled:
            throw PaymentError.userCancelled
        default:
            throw PaymentError.unknown
        }
    }
}
2

2. Simple KeepIAP Implementation

Replace StoreKit with KeepIAP using the same simple pattern:

// Simple KeepIAP implementation
class KeepIAPManager: ObservableObject {
    @Published var products: [Product] = []
    private let keepiap = KeepIAP.shared
    
    func loadProducts() async {
        // Load products from your backend
        let products = [
            Product(id: "premium.monthly", price: 9.99, title: "Premium Monthly"),
            Product(id: "premium.yearly", price: 99.99, title: "Premium Yearly")
        ]
        self.products = products
    }
    
    func purchase(_ product: Product) async throws {
        // Process payment through KeepIAP
        let result = try await keepiap.presentPayment(
            productId: product.id,
            amount: Int(product.price * 100),
            currency: "usd",
            description: product.title
        )
        
        switch result {
        case .success:
            // Payment successful
            break
        case .userCancelled:
            throw PaymentError.userCancelled
        case .failed(let error):
            throw PaymentError.paymentFailed(error)
        }
    }
}
3

3. Usage in Your App

Use it in your app just like StoreKit:

struct PurchaseView: View {
    @StateObject private var manager = WebIAPManager()
    
    var body: some View {
        List(manager.products) { product in
            Button(action: {
                Task {
                    try? await manager.purchase(product)
                }
            }) {
                VStack(alignment: .leading) {
                    Text(product.title)
                        .font(.headline)
                    Text("$(product.price, specifier: "%.2f")")
                        .font(.subheadline)
                }
            }
        }
        .task {
            await manager.loadProducts()
        }
    }
}