Skip to content

Instantly share code, notes, and snippets.

@mukaschultze
Created September 13, 2022 18:44
Show Gist options
  • Save mukaschultze/2b3904d20338a8a496b8e3953ff914cc to your computer and use it in GitHub Desktop.
Save mukaschultze/2b3904d20338a8a496b8e3953ff914cc to your computer and use it in GitHub Desktop.
NativeScript prompt user for app review
import { Application, Device, isAndroid, Utils } from '@nativescript/core';
/**
* This method uses native APIs to request an app review from the user. There's
* no guarantee that the user will actually be prompted when this method is
* called and there's no way to know if the left a review or not. This is a
* limitation of the platform.
*
* NOTES
*
* Android: Development builds will never show the popup, production builds have
* a quota of around 1 show per month and internal testing builds don't have any
* quota, but there's a bug on the play store that requires its data to be
* cleared up before prompting again. See resources below for more info and
* troubleshooting guides.
*
* https://developer.android.com/guide/playcore/in-app-review/test
* https://stackoverflow.com/questions/70954925/in-app-review-opens-only-once-when-testing-for-the-same-account
*
* iOS: This method will always show the popup for development builds (on the
* simulator), but never for TestFlight builds. On production builds there's a
* quota of 3 prompts per year for each unique build number (i.e., bundle id).
*
* https://developer.apple.com/documentation/storekit/requesting_app_store_reviews?language=objc
* https://developer.apple.com/documentation/storekit/skstorereviewcontroller/3566727-requestreviewinscene?language=objc
*/
export function requestReview() {
if (isAndroid) {
console.log('Requesting review flow...');
const context = Utils.android.getApplicationContext() as android.content.Context;
const manager = com.google.android.play.core.review.ReviewManagerFactory.create(context);
const request = manager.requestReviewFlow();
request.addOnCompleteListener(
new com.google.android.play.core.tasks.OnCompleteListener({
onComplete: (task) => {
if (!task.isSuccessful()) {
console.log('Failed to get review info');
console.error(task.getException());
return;
}
console.log('Got review info, launching review flow...');
const reviewInfo = task.getResult();
const flow = manager.launchReviewFlow(Application.android.foregroundActivity, reviewInfo);
flow.addOnCompleteListener(
new com.google.android.play.core.tasks.OnCompleteListener({
onComplete: (task) => {
// The flow has finished. The API does not indicate whether the
// user reviewed or not, or even whether the review dialog was
// shown. Thus, no matter the result, we continue our app flow.
if (!task.isSuccessful()) {
console.log('Failed launch review flow');
console.error(task.getException());
} else {
console.log('Review flow completed successfully');
}
}
})
);
}
})
);
} else {
console.log('Requesting review flow...');
if (+Device.sdkVersion < 14.0) {
SKStoreReviewController.requestReview();
} else {
SKStoreReviewController.requestReviewInScene((Application.ios.window as UIWindow).windowScene);
}
console.log('Review flow completed successfully');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment