Filtering StoreKit2 Subscriptions Based on Store Country Without Hardcoding Country Codes

55 Views Asked by At

How can I modify the provided Swift code to filter out the "pro_monthly" subscription for users in the United States without hardcoding any country codes in the code? The goal is to exclude the "pro_monthly" product based on the Store Country of the end user.

Here's the original code for reference:

import SwiftUI
import StoreKit

struct ContentView: View {
    let productIds = ["pro_monthly", "pro_yearly", "pro_lifetime"]

    @State
    private var products: [Product] = []

    var body: some View {
        VStack(spacing: 20) {
            Text("Products")
            ForEach(self.products) { product in
                Button {
                    // Don't do anything yet
                } label: {
                    Text("\(product.displayPrice) - \(product.displayName)")
                }
            }
        }.task {
            do {
                try await self.loadProducts()
            } catch {
                print(error)
            }
        }
    }

    private func loadProducts() async throws {
        self.products = try await Product.products(for: productIds)
    }
}
0

There are 0 best solutions below