The feature described in this section is supported on the following versions and above:
iOS: 3.5.0
Android: 3.5.0
ReactNative: 2.5.0
Cordova: 2.5.0
Flutter: 1.5.0
Purchasely, by default, shows the paywall screen with a loading indicator while fetching the paywall from the network and preparing it for display.
Using Purchasely.fetchPresentation() method, you can pre-fetch the paywall from the network before displaying it. This provides the following benefits:
Display the paywall only after it has been loaded from the network
Handle network errors gracefully
Show a custom loading screen
Pre-load the paywall while users navigate through your app, such as during onboarding screens
// fetch presentation with id
Purchasely.fetchPresentation(with: "presentationId", fetchCompletion: { presentation, error in
})
// fetch presentation for placement
Purchasely.fetchPresentation(
for: "onboarding",
fetchCompletion: { presentation, error in
// closure to get presentation and display it
guard let presentation = presentation, error == nil else {
print("Error while fetching presentation: \(error?.localizedDescription ?? "unknown")")
return
}
if presentation.type == .normal || presentation.type == .fallback {
let paywallController = presentation.controller
// display paywall controller.
} else if presentation.type == .deactivated {
// nothing to display
} else if presentation.type == .client {
let presentationId = presentation.id
let planIds = presentation.plans
// display your own paywall
}
},
completion: { result, plan in
// closure when presentation controller is closed to get result
switch result {
case .purchased:
print("User purchased: \(plan?.name)")
break
case .restored:
print("User restored: \(plan?.name)")
break
case .cancelled:
break
@unknown default:
break
}
})
[Purchasely fetchPresentationFor:@"onboarding" fetchCompletion:^(PLYPresentation * _Nullable presentation, NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Error while fetching presentation: %@", error.localizedDescription);
return;
}
if (presentation.type == PLYPresentationTypeNormal || presentation.type == PLYPresentationTypeFallback) {
PLYPresentationViewController *paywallController = presentation.controller;
// display paywall controller.
} else if (presentation.type == PLYPresentationTypeDeactivated) {
// nothing to display
} else if (presentation.type == PLYPresentationTypeClient) {
NSString *presentationId = presentation.id;
NSArray<NSString *> *plans = presentation.plans;
//display your own paywall
}
} completion:nil];
Purchasely.fetchPresentationForPlacement("onboarding") { presentation, error ->
if(error != null) {
Log.d("Purchasely", "Error fetching paywall", error)
return@fetchPresentationForPlacement
}
when(presentation?.type) {
PLYPresentationType.NORMAL,
PLYPresentationType.FALLBACK -> {
val paywallView = presentation.buildView(
context = this@MainActivity,
viewProperties = PLYPresentationViewProperties(
onClose = {
// TODO remove view from your layout
}
)
) { result, plan ->
// Paywall is closed, check result to know if a purchase happened
when(result) {
PLYProductViewResult.PURCHASED -> Log.d("Purchasely", "User purchased ${plan?.name}")
PLYProductViewResult.CANCELLED -> Log.d("Purchasely", "User cancelled purchased")
PLYProductViewResult.RESTORED -> Log.d("Purchasely", "User restored ${plan?.name}")
}
}
// Display Purchasely paywall by adding paywallView to your layout
}
PLYPresentationType.DEACTIVATED -> {
// Nothing to display
}
PLYPresentationType.CLIENT -> {
val paywallId = presentation.id
val planIds = presentation.plans
// Display your own paywall
}
else -> {
//No presentation, it means an error was triggered
}
}
}