Paywall'da etkileşimli bir kilo hedefi grafiği göster
Belirtilen ağırlık değerlerini bir paywall flow’unda grafik olarak göstermek için bu tarifi takip edin. Kullanıcı, onboarding flow’unda mevcut ve hedef kilosunu girer. C# kodunuz her iki değeri toplayıp native kodunuza gönderir. Grafik, uygulamanızla birlikte kullanıcının bir değerden diğerine haftalık ne kadar ilerleme sağlayabileceğini gösterir. Uygulamanız grafiği bir özel element içinde native view olarak render eder. iOS’ta view Swift Charts kullanır. Android’de ise grafik bir grafik kütüphanesi olmadan çizilir.
Bu tarif, iOS tarifi ve Android tarifi içindeki flow kurulumunu ve native view’ları yeniden kullanır. Bu tarifin her adımı, bunlardan ne kopyalanacağını ve Unity projesinde ne farklı yapılacağını açıklar.
Başlamadan önce
- Uygulamanızı Adapty Unity SDK 4.4 veya sonraki bir sürüme güncelleyin. Önceki SDK sürümleri, her özel öğe için yedek içeriği gösterir.
- Uygulamanızı flow’ları getirmek ve görüntülemek için yapılandırın.
- Uygulamanızı özel öğeler için yapılandırın; bunun için Unity SDK ile özel öğeleri görüntüleme kılavuzuna bakın. Bu tarif, söz konusu kılavuzdaki C# ve Java
CustomElementssınıflarına kod ekler. - iOS’ta grafik boyunca sürükleme özelliği iOS 17 veya sonrasını gerektirir. iOS 16’da kullanıcılar çizgiyi görebilir ancak üzerinde sürükleme yapamaz.
İki ayrı flow oluşturun — biri onboarding için, diğeri paywall için. Her birini bir placement’a ekleyin. Bu tarifte yer alan kod, weight_paywall ID’li placement’tan paywall flow’unu getirir.
1. Onboarding flow’unu ve paywall flow’unu ayarlayın
Her iki flow’u da iOS tarifindeki 1. ve 2. adımlarda olduğu gibi ayarlayın: onboarding flow’unda iki Number girişi ve paywall flow’unda grafik için bir özel element.
2. Yerel kodu ekleyin
iOS ve Android tarifler, paywall flow’una bir çözümleyici (resolver) bağlar. Unity SDK’sında tek bir flow için çözümleyici API’si bulunmaz. Bunun yerine, uygulama genelindeki çözümleyiciniz ağırlık planını bir mağazadan okur ve C# kodunuz paywall flow’u açılmadan önce mağazayı doldurur.
iOS
Assets/Plugins/iOS klasöründeki bir Swift dosyasına, iOS tarifi’nin 4. adımındaki ağırlık planını ve grafik görünümünü ekleyin. Ardından ağırlık planı için bir depo, depoyu okuyan bir çözümleyici ve C# kodunuzun çağırdığı iki fonksiyon ekleyin. RegisterCustomElements başlatma sırasında çözümleyiciyi ayarlar, SetWeightPlan ise depoyu doldurur:
// the weight plan that your C# code sends before the paywall flow opens
@MainActor
final class WeightPlanStore {
var plan: WeightPlan?
}
struct CustomElementsResolver: AdaptyUICustomElementsResolver {
let weightPlanStore: WeightPlanStore
func view(
forType type: String,
id: String,
context: AdaptyUICustomElementContext
) -> AnyView? {
switch type {
case "line_chart" where id == "weight_goal":
guard #available(iOS 16.0, *), let plan = weightPlanStore.plan else {
return nil // no weight plan: show the fallback
}
return AnyView(WeightGoalChart(entries: plan.entries))
// the other cases of your resolver
default:
return nil
}
}
}
@MainActor let weightPlanStore = WeightPlanStore()
// Called once from C# at launch, before any flow is created.
@_cdecl("RegisterCustomElements")
public func RegisterCustomElements() {
Task { @MainActor in
AdaptyUI.setCustomElementsResolver(CustomElementsResolver(weightPlanStore: weightPlanStore))
}
}
// Called from C# before the paywall flow opens.
@_cdecl("SetWeightPlan")
public func SetWeightPlan(_ currentWeight: Double, _ goalWeight: Double) {
Task { @MainActor in
weightPlanStore.plan = WeightPlan(currentWeight: currentWeight, goalWeight: goalWeight)
}
}Android
WeightPlan, WeightEntry ve WeightGoalChartView sınıflarını Android tarifi’nin 4. adımının Java sekmesinden kopyalayın. Her sınıfı Assets/Plugins/Android dizininde ayrı bir dosyaya kaydedin; örneğin WeightPlan.java. Her dosyaya package com.example.app; satırı ve ilgili sınıfın kullandığı import’ları ekleyin.
CustomElements.java dosyasında, ağırlık planını okuyan bir resolver tanımlayın ve C# kodunuz için bir setWeightPlan metodu ekleyin:
package com.example.app;
public final class CustomElements {
// the weight plan that your C# code sends before the paywall flow opens
private static volatile WeightPlan weightPlan;
// Called once from C# at launch, before any flow is created.
// This resolver doesn't use the context.
public static void register(Context context) {
AdaptyUI.setCustomElementsResolver((type, id) -> {
WeightPlan plan = weightPlan;
if ("line_chart".equals(type) && "weight_goal".equals(id) && plan != null) {
return AdaptyCustomElementContent.view(
(viewContext, elementContext) -> new WeightGoalChartView(viewContext, plan.entries)
);
}
return null; // no weight plan, or another custom element: show the fallback
});
}
// Called from C# before the paywall flow opens.
public static void setWeightPlan(double currentWeight, double goalWeight) {
weightPlan = new WeightPlan(currentWeight, goalWeight);
}
}C# kodunuz mevcut ve hedef ağırlığı göndermediği sürece, resolver herhangi bir görünüm döndürmez ve kullanıcılar yedek paywall’ı görür.
3. C# kodunuzdan ağırlıkları gönderin
C# CustomElements sınıfında, her iki ağırlığı da native kodunuza geçiren bir SendWeightPlan metodu ekleyin. Vurgulanan satırlar, Unity kılavuzundaki sınıfa eklemeniz gerekenleri göstermektedir:
using UnityEngine;
public static class CustomElements
{
#if UNITY_IOS && !UNITY_EDITOR
[System.Runtime.InteropServices.DllImport("__Internal")]
private static extern void RegisterCustomElements();
[System.Runtime.InteropServices.DllImport("__Internal")]
private static extern void SetWeightPlan(double currentWeight, double goalWeight);
#endif
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
private static void Register()
{
#if UNITY_IOS && !UNITY_EDITOR
RegisterCustomElements();
#elif UNITY_ANDROID && !UNITY_EDITOR
using (var player = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
using (var activity = player.GetStatic<AndroidJavaObject>("currentActivity"))
using (var context = activity.Call<AndroidJavaObject>("getApplicationContext"))
using (var resolver = new AndroidJavaClass("com.example.app.CustomElements"))
{
resolver.CallStatic("register", context);
}
#endif
}
// call before the paywall flow opens
public static void SendWeightPlan(double currentWeight, double goalWeight)
{
#if UNITY_IOS && !UNITY_EDITOR
SetWeightPlan(currentWeight, goalWeight);
#elif UNITY_ANDROID && !UNITY_EDITOR
using (var customElements = new AndroidJavaClass("com.example.app.CustomElements"))
{
customElements.CallStatic("setWeightPlan", currentWeight, goalWeight);
}
#endif
}
}FlowViewDidReceiveAnalyticEvent metodunu kullanarak mevcut ağırlığı ve hedef ağırlığı, Flow’lara veri aktarma sayfasındaki BMI örneğinde olduğu gibi onboarding flow’undan toplayın. FlowViewDidDisappear yalnızca uygulamanız onboarding flow’unu kapattıktan sonra çalışır; bu nedenle flow’u Flow’ları ve paywallları kapatma sayfasında gösterildiği gibi Close flow eylemi üzerinde kapatın. Kullanıcı onboarding flow’unu son ekrandan kapattığında her iki ağırlığı da native kodunuza gönderin ve paywall flow’unu açın:
public void FlowViewDidDisappear(AdaptyUIFlowView view) {
// The listener receives events from every flow. Reset `bodyAnswers` and
// `reachedLastScreen` whenever a flow disappears, so that the paywall
// doesn't open again when the user closes it.
var answers = new Dictionary<string, string>(bodyAnswers);
var finished = reachedLastScreen;
bodyAnswers.Clear();
reachedLastScreen = false;
// Number inputs send their values as strings.
if (!finished) return;
if (!answers.TryGetValue("weight", out var currentText)
|| !double.TryParse(currentText, out var currentWeight)) return;
if (!answers.TryGetValue("goal_weight", out var goalText)
|| !double.TryParse(goalText, out var goalWeight)) return;
// the resolver reads the weights when the paywall flow opens
CustomElements.SendWeightPlan(currentWeight, goalWeight);
ShowWeightPaywall();
}
private void ShowWeightPaywall() {
Adapty.GetFlow("weight_paywall", (flow, flowError) => {
if (flowError != null) {
// handle the error
return;
}
AdaptyUI.CreateFlowView(flow, new AdaptyUICreateFlowViewParameters(), (view, viewError) => {
if (viewError != null) {
// handle the error
return;
}
view.Present((presentError) => {
if (presentError != null) {
// handle the error
}
});
});
});
}4. Cihazda Test Edin
Her iki flow’u da iOS ve Android rehberlerindeki 6. adımda anlatıldığı gibi test edin. Yedek görünümü test etmek için resolver’ın weight_goal ID’si için herhangi bir view döndürmemesini sağlayın ve paywall flow’unu yeniden açın.
Grafik yerine neden yedek görünümün göründüğünü anlamak için Unity rehberinin Cihazda Test Edin bölümündeki talimatları izleyin.