Search code examples
iosobjective-cstorekit

How to use requestReview (SKStore​Review​Controller) to show review popup in the current viewController after a random period of time


I've read about this new feature available in iOS 10.3 and thought it will be more flexible and out of the box. But after I read the docs I found out that you need to decide the time to show it and the viewController who calls it. Is there any way I can make it trigger after a random period of time in any viewController is showing at that moment?


Solution

  • In your AppDelegate:

    Swift:

    import StoreKit
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        let shortestTime: UInt32 = 50
        let longestTime: UInt32 = 500
        guard let timeInterval = TimeInterval(exactly: arc4random_uniform(longestTime - shortestTime) + shortestTime) else { return true }
    
        Timer.scheduledTimer(timeInterval: timeInterval, target: self, selector: #selector(AppDelegate.requestReview), userInfo: nil, repeats: false)
    
    }
    
    @objc func requestReview() {
        SKStoreReviewController.requestReview()
    }
    

    Objective-C:

    #import <StoreKit/StoreKit.h>
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        int shortestTime = 50;
        int longestTime = 500;
        int timeInterval = arc4random_uniform(longestTime - shortestTime) + shortestTime;
    
        [NSTimer scheduledTimerWithTimeInterval:timeInterval target:self selector:@selector(requestReview) userInfo:nil repeats:NO];
    }
    
    - (void)requestReview {
        [SKStoreReviewController requestReview];
    }
    

    The code above will ask Apple to prompt the user to rate the app at a random time between 50 and 500 seconds after the app finishes launching. Remember that according to Apple's docs, there is no guarantee that the rating prompt will be presented when the requestReview is called.