Gutanga ikigeragezo kubuntu ninzira nziza yo gushishikariza abakoresha kugerageza ibintu byingenzi bya porogaramu yawe, byongera amahirwe yo kubahindura mukwishyura abafatabuguzi. Hamwe na StoreKit 2, Apple yazanye ibikoresho byongerewe imbaraga kugirango bigufashe kugenzura niba umukoresha yujuje ibisabwa kugirango atangire. Muri iyi ngingo, tuzakuyobora mugushira mubikorwa ibigeragezo kubuntu muri porogaramu yawe, byuzuye hamwe na kode ya kode kugirango tumenye neza abakoresha. Tuzareba kandi ibizamini byo kugerageza kubakoresha kandi batujuje ibisabwa. Kandi witondere gukurikiranira hafi " Pro Tips " zinyanyagiye mu ngingo, aho nsangira ubushishozi bwihariye mubyambayeho!
Mbere yo gutangira code, menya neza ko ugena igenamiterere rya porogaramu yo kugura muri porogaramu y'Ububiko bwa App:
Nzakora ibi byoroshye nkwereka kode ya kode, kuburyo ushaka kubara ibyangombwa byikigereranyo kubakoresha
Kora SwiftUI kureba kugirango werekane itangwa ryubusa kandi ukoreshe imikoranire yabakoresha. Nzasiga ibitekerezo byinshi muri kode ya kode kugirango nkunyure.
import StoreKit2 // StoreManager is responsible to communicate with Storekit Framework provided by Apple for monetization class StoreManager: ObservableObject { @Published var message: String = "" // We will use this property to display the right message to the user @Published var products: [Product] = [] // This will be responsible to store the products fetched that we defined // in App Store Connect // Fetch products from the App Store func fetchProducts() { Task { do { // product_id is the id that you would have defined in App Store Connect. let storeProducts = try await Product.products(for: ["product_id"]) products = storeProducts } catch { message = "Failed to fetch products: \(error.localizedDescription)" } } } // Initiate purchase func purchase() { guard let product = products.first else { // There is a possibility of products not being fetched from App Store Connect. // Pro Tip: From experience, even though we defined the products on App Store Connect, it is possible // that the products are not found post attempting to fetch. So, it is important to handle this case. message = "No product available." return } Task { do { let result = try await product.purchase() switch result { case .success(let verification): switch verification { case .verified: message = "Purchase successful!" case .unverified: message = "Could not verify the purchase." } case .userCancelled: message = "Purchase cancelled." case .pending: message = "Purchase is pending." @unknown default: message = "Unknown result." } } catch { message = "Purchase failed: \(error.localizedDescription)" } } } // Check if the user is eligible for a free trial func checkTrialEligibility() async -> Bool { guard let product = products.first else { return false } do { // So when you define a auto renewable subscriptions, there are usually bond in a group. The group can again be // found in App Store Connect let eligibility = try await product.subscription?.isEligibleForIntroOffer(for groupID: 111111) return eligibility ?? false } catch { message = "Error checking trial eligibility: \(error.localizedDescription)" return false } } }
import SwiftUI import StoreKit struct SubscriptionView: View { @StateObject private var storeManager = StoreManager() @State private var isEligibleForFreeTrial = false var body: some View { VStack { Text("Unlock Premium Features") .font(.title) .padding() Text("Get a 7-day free trial of our premium subscription.") .multilineTextAlignment(.center) .padding() Button(action: { storeManager.purchase() }) { // Based on user status, we can display the text Text(isEligibleForFreeTrial ? "Start Free Trial" : "Start Subscription") .bold() .frame(width: 200, height: 50) .background(Color.blue) .foregroundColor(.white) .cornerRadius(10) } Text(storeManager.message) .padding() } .onAppear { storeManager.fetchProducts() checkTrialEligibility() } } private func checkTrialEligibility() { Task { isEligibleForFreeTrial = await storeManager.checkTrialEligibility() } } }
Apple itanga ibikoresho bikomeye byo kugerageza ibihugu bitandukanye byabakoresha (urugero, bujuje ibisabwa cyangwa ntibemerewe kugeragezwa kubuntu) ukoresheje Ikizamini cya StoreKit muri Xcode :
Jya kuri File> Gishya> Idosiye ...> UbubikoKit Iboneza Idosiye muri Xcode.
Shiraho ibicuruzwa byawe byo kwiyandikisha, harimo ibihe byo kugerageza hamwe na leta zujuje ibisabwa.
Impanuro : Urashobora kandi gukora dosiye nshya yimiterere kugirango uhuze dosiye iboneza kuva muri App Store ihuza kandi ubwo buryo ntibukeneye gushiraho ibicuruzwa byose.
Wigane ibintu bitandukanye:
Abemerewe kuburanishwa ku buntu:
Kugereranya umukoresha wikigereranyo kubuntu, menya neza ko udafite ibikorwa mubikorwa byumuyobozi.
Kubona umuyobozi wubucuruzi. Jya kuri Debug → UbubikoKit → Gucunga ibikorwa
Ntibemerewe kuburanishwa kubuntu:
Kugereranya umukoresha utemerewe kugeragezwa kubuntu. Urashobora kongeramo intoki abiyandikishije mubuyobozi bwubucuruzi. Urashobora gukanda kuri bouton yongeyeho kuri ecran ya transaction ya ecran hanyuma ugahitamo transaction ushaka kongeramo. Hano, ndagerageza gushiraho abiyandikisha buri kwezi kubakoresha. Nyuma yo kongeramo ibyo gucuruza no kongera gukoresha porogaramu, ugomba kubona ibyangombwa byo kugerageza byerekanwe nkibinyoma.
Impanuro: Urashobora kandi gushiramo UUID hamwe no kugura, ukoresheje uyu murima kugirango ubike indangamuntu. Ubu buryo urashobora kwemeza umukoresha waguze kuri porogaramu yawe. Aya makuru arashobora gukurwa nyuma mumateka yubucuruzi.
Ikizamini cya Sandbox kigufasha kugerageza kugura porogaramu yawe muri porogaramu no kwiyandikisha mu bidukikije bigana aho ibicuruzwa byinjira mu Ububiko bikaguha kandi umudendezo wo gushinyagurira imanza ebyiri nko guhagarika kugura, kugabana umuryango, no kwigana ibyaguzwe byakorewe hanze porogaramu cyangwa mu kindi gikoresho. Iragufasha kandi
Ariko mbere yikintu cyose, dore uburyo bwo gushiraho no gukoresha ibizamini bya sandbox:
Kora Konti Yipimisha Sandbox:
Injira hamwe na Konti Yipimisha Sandbox:
Koresha Porogaramu yawe muri Sandbox Mode:
Gerageza Ibihe Bitandukanye:
Impanuro: Konti ya Apple ntishobora kugira abiyandikisha benshi bakora, kubwibyo abakoresha babiri batandukanye ntibashobora kugura ibintu bitandukanye kuri porogaramu ukoresheje indangamuntu imwe ya Apple. Wemeze kugenzura porogaramu kuriyi dosiye.
https://developer.apple.com/documentation/storekit
https://developer.apple.com/app-ububiko-huza/