Bepul sinov muddatini taklif qilish foydalanuvchilarni ilovangizning premium xususiyatlarini sinab ko‘rishga undashning ajoyib usuli bo‘lib, ularni pullik obunachilarga aylantirish imkoniyatini oshiradi. StoreKit 2 bilan Apple foydalanuvchining kirish takliflariga muvofiqligini tekshirishga yordam beradigan takomillashtirilgan vositalarni taqdim etdi. Ushbu maqolada biz sizga ilovangizda foydalanuvchining muvofiqligini tekshirish uchun kod parchalari bilan toʻliq bepul sinovlarni amalga oshirish boʻyicha koʻrsatma beramiz. Shuningdek, biz mos va nomaqbul foydalanuvchilar uchun sinov stsenariylarini ham ko‘rib chiqamiz. Maqola bo'ylab tarqalgan " Pro Maslahatlar " ni diqqat bilan kuzatib boring, u erda men o'z tajribamdan ba'zi shaxsiy fikrlarim bilan o'rtoqlashaman!
Kodlashni boshlashdan oldin, App Store Connect-da ilova ichidagi xarid sozlamalarini sozlaganingizga ishonch hosil qiling:
Men sizga foydalanuvchi uchun sinov muddatini qanday hisoblashni xohlayotganingiz uchun kod parchasini ko'rsatib, buni soddalashtiraman.
Bepul sinov taklifini ko'rsatish va foydalanuvchi o'zaro munosabatlarini boshqarish uchun SwiftUI ko'rinishini yarating. Sizni o'rganish uchun kod parchasida ko'plab sharhlar qoldiraman.
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 Xcode-da StoreKit testidan foydalangan holda turli xil foydalanuvchi holatlarini (masalan, bepul sinov uchun mos yoki mos emas) sinab ko'rish uchun mustahkam vositalarni taqdim etadi:
Xcode-da Fayl > Yangi > Fayl... > StoreKit konfiguratsiya fayliga o‘ting.
Obuna mahsulotlarini, jumladan, sinov muddatlari va muvofiqlik holatlarini sozlang.
Maslahat : App Store Connect-dan konfiguratsiya faylini sinxronlash uchun yangi konfiguratsiya faylini ham yaratishingiz mumkin va bunda barcha mahsulotlarni sozlash shart emas.
Turli stsenariylarni simulyatsiya qilish:
Bepul sinov uchun mos:
Bepul sinov foydalanuvchisini taqlid qilish uchun tranzaksiya menejerida hech qanday tranzaksiya yo'qligiga ishonch hosil qiling.
Tranzaksiya menejerini ko'rish uchun. Debug → StoreKit → Tranzaksiyalarni boshqarish- ga o'ting
Bepul sinov uchun mos emas:
Bepul sinovdan foydalanish huquqiga ega bo'lmagan foydalanuvchini simulyatsiya qilish uchun. Obunani tranzaksiya menejeridagi tranzaksiya menejeridan qo'lda qo'shishingiz mumkin. Siz tranzaksiya menejeri ekranidagi qo'shish tugmasini bosishingiz va keyin qo'shmoqchi bo'lgan tranzaksiyani tanlashingiz mumkin. Bu erda men foydalanuvchi uchun oylik obunani sozlashga harakat qilaman. Ushbu tranzaksiyani qo'shgandan va ilovani qayta ishga tushirgandan so'ng, sinov muddati noto'g'ri deb belgilanganligini ko'rishingiz kerak.
Maslahat: Siz shuningdek, foydalanuvchi identifikatorini saqlash uchun ushbu maydondan foydalanib, xaridga UUID qo'shishingiz mumkin. Shunday qilib, qaysi foydalanuvchi ilovangizda xarid qilganiga ishonch hosil qilishingiz mumkin. Keyinchalik bu ma'lumotni foydalanuvchining tranzaksiya tarixidan olish mumkin.
Sandbox testi ilovangizning ilova ichidagi xaridlari va obunalarini App Store ishlab chiqarish muhitiga taqlid qiladigan muhitda sinab ko‘rish imkonini beradi, shu bilan birga sizga xaridlarni to‘xtatib turish, oila bilan bo‘lishish va tashqarida qilingan xaridlarni taqlid qilish kabi bir nechta chekli holatlarni masxara qilish erkinligini beradi. ilovada yoki boshqa qurilmada. Bu sizga ham imkon beradi
Biroq, avvalo, sinov muhiti sinovini qanday sozlash va undan foydalanish kerak:
Sandbox tester hisobini yarating:
Sandbox Tester hisobi bilan tizimga kiring:
Ilovangizni Sandbox rejimida ishga tushiring:
Turli stsenariylarni sinab ko'ring:
Maslahat: Apple hisob qaydnomasida bir nechta faol obuna bo'lishi mumkin emas, shuning uchun ikki xil foydalanuvchi bir xil Apple ID yordamida ilovada alohida xaridlarni amalga oshira olmaydi. Ushbu foydalanuvchi ishi uchun ilovangizni tekshirishni unutmang.
https://developer.apple.com/documentation/storekit
https://developer.apple.com/documentation/xcode/setting-up-storekit-testing-in-xcode/
https://developer.apple.com/app-store-connect/