Home > AI > Uncategorized

Swift – app评分

import Foundation
import StoreKit

class Rate{

    static let runCountKey = "numberOfRuns"
    static let minimumRunCount = 5
    
    
    //find suitable time to use this func
    class func popRateAlert(){
        let runs = getSavedRunCounts()
        if runs > minimumRunCount {
            if #available(iOS 10, *){
                SKStoreReviewController.requestReview() //core
            }
        } else {
            print("run count requirement is not satisfied")
        }
        
    }
    
    //This func is used in AppDelegate / applicationDidFinishLaunching
    class func addAppRuns(){
        //let usD = UserDefaults()
        let runs = getSavedRunCounts() + 1
        UserDefaults.standard.setValuesForKeys([runCountKey: runs]) //用单例,避免存取不一致
        UserDefaults.standard.synchronize()
    }
    
    class func getSavedRunCounts() -> Int{
        //let usD = UserDefaults()
        let savedRuns = UserDefaults.standard.value(forKey: runCountKey)
        
        var runs = 0
        if savedRuns != nil {
            runs = savedRuns as! Int
        }
        print("has run \(runs)")
        return runs
    }
}

 

Related posts:

Leave a Reply