如何使用requestReview(SKStore Review Controller)在随机时间段之后在当前viewController中显示审查popup窗口

我已经阅读了iOS 10.3中提供的这个新function,并认为它将更加灵活和开箱即用。 但是在阅读文档之后,我发现你需要决定显示它的时间以及调用它的viewController。 有没有什么办法让我们可以在任何viewController显示在那一刻随机的一段时间后触发?

在你的AppDelegate中:

迅速:

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) } 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]; } 

上面的代码将要求苹果提示用户在应用程序启动后的50到500秒之间的随机时间评价应用程序。 请记住,根据苹果的文档,当requestReview被调用时,不能保证评级提示。

对于Objective – C:

添加StoreKit.framework

然后在你的viewController.h中

 #import <StoreKit/StoreKit.h> 

然后在你的函数调用中:

  [SKStoreReviewController requestReview]; 

对于Swift

添加StoreKit.framework

在你的ViewController.swift中

 import StoreKit 

然后在你的函数调用中:

 SKStoreReviewController.requestReview() 

而已 ! 苹果会照顾什么时候会显示评级(随机)。 在开发过程中,每次调用它时都会被调用。

编辑:无需检查操作系统版本,如果操作系统低于10.3,则不会popupStoreKit,感谢Zakaria。

随意popup是不是一个好的方法来使用这个例程,不仅违反了苹果的build议,而且会给你带来不甚好的结果。

用户随意popup来烦恼用户,就不会像在适当的时候提示用户那样成功 – 比如他们刚刚完成一个级别或者创build了一个文档,并且具有这种温暖的模糊的成就感。

以Peter Johnson的build议为例,我创build了一个简单的类,您可以将代码粘贴到代码中的所需位置,并在用户刚获得成功的位置popup。

 struct DefaultKeys { static let uses = "uses" } class ReviewUtility { // Default Keys stored in Structs.swift static let sharedInstance = ReviewUtility() private init() {} func recordLaunch() { let defaults = UserDefaults.standard // if there's no value set when the app launches, create one guard defaults.value(forKey: DefaultKeys.uses) != nil else { defaults.set(1, forKey: DefaultKeys.uses); return } // read the value var totalLaunches: Int = defaults.value(forKey: DefaultKeys.uses) as! Int // increment it totalLaunches += 1 // write the new value UserDefaults.standard.set(totalLaunches, forKey: DefaultKeys.uses) // pick whatever interval you want if totalLaunches % 20 == 0 { // not sure if necessary, but being neurotic if #available(iOS 10.3, *) { // do storekit review here SKStoreReviewController.requestReview() } } } } 

要使用它,把它贴在你想要的地方,希望你不会随意勾选用户。

 ReviewUtility.sharedInstance.recordLaunch() 

我不能添加评论,但如果你正在使用Appirater你可能想检查版本,看看它是否低于10.3,所以其他Appirater审查消息框popup。