Skip to content

Instantly share code, notes, and snippets.

@josephchang10
Created August 12, 2017 05:12
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephchang10/0d57a5492ba9be8cacc1f92947359954 to your computer and use it in GitHub Desktop.
Save josephchang10/0d57a5492ba9be8cacc1f92947359954 to your computer and use it in GitHub Desktop.
从 iOS 10.3 开始请求应用商店评分的正确姿势:SKStoreReviewController
//
// ReviewRequest.swift
//
// Created by 张嘉夫 on 12/8/17.
// Copyright © 2017 张嘉夫. All rights reserved.
import Foundation
import StoreKit
let runIncrementerSetting = "numberOfRuns" // 用于存储运行次数的 UserDefauls 字典键
let minimumRunCount = 5 // 询问评分的最少运行次数
func incrementAppRuns() { // app 运行次数计数器。可以在 App Delegate 中调用此方法
let usD = UserDefaults()
let runs = getRunCounts() + 1
usD.setValuesForKeys([runIncrementerSetting: runs])
usD.synchronize()
}
func getRunCounts () -> Int { // 从 UserDefaults 里读取运行次数并返回。
let usD = UserDefaults()
let savedRuns = usD.value(forKey: runIncrementerSetting)
var runs = 0
if (savedRuns != nil) {
runs = savedRuns as! Int
}
print("已运行\(runs)次")
return runs
}
func showReview() {
let runs = getRunCounts()
print("显示评分")
if (runs > minimumRunCount) {
if #available(iOS 10.3, *) {
print("已请求评分")
SKStoreReviewController.requestReview()
} else {
// 回到老版本
}
} else {
print("请求评分所需的运行次数不足!")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment