├── .gitignore ├── .swiftpm └── xcode │ └── package.xcworkspace │ └── contents.xcworkspacedata ├── LICENSE ├── Package.swift ├── README.md └── Sources └── NSReviewUtility └── NSReviewUtility.swift /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## User settings 6 | xcuserdata/ 7 | 8 | ## compatibility with Xcode 8 and earlier (ignoring not required starting Xcode 9) 9 | *.xcscmblueprint 10 | *.xccheckout 11 | 12 | ## compatibility with Xcode 3 and earlier (ignoring not required starting Xcode 4) 13 | build/ 14 | DerivedData/ 15 | *.moved-aside 16 | *.pbxuser 17 | !default.pbxuser 18 | *.mode1v3 19 | !default.mode1v3 20 | *.mode2v3 21 | !default.mode2v3 22 | *.perspectivev3 23 | !default.perspectivev3 24 | 25 | ## Obj-C/Swift specific 26 | *.hmap 27 | 28 | ## App packaging 29 | *.ipa 30 | *.dSYM.zip 31 | *.dSYM 32 | 33 | ## Playgrounds 34 | timeline.xctimeline 35 | playground.xcworkspace 36 | 37 | # Swift Package Manager 38 | # 39 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 40 | # Packages/ 41 | # Package.pins 42 | # Package.resolved 43 | # *.xcodeproj 44 | # 45 | # Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata 46 | # hence it is not needed unless you have added a package configuration file to your project 47 | # .swiftpm 48 | 49 | .build/ 50 | 51 | # CocoaPods 52 | # 53 | # We recommend against adding the Pods directory to your .gitignore. However 54 | # you should judge for yourself, the pros and cons are mentioned at: 55 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 56 | # 57 | # Pods/ 58 | # 59 | # Add this line if you want to avoid checking in source code from the Xcode workspace 60 | # *.xcworkspace 61 | 62 | # Carthage 63 | # 64 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 65 | # Carthage/Checkouts 66 | 67 | Carthage/Build/ 68 | 69 | # Accio dependency management 70 | Dependencies/ 71 | .accio/ 72 | 73 | # fastlane 74 | # 75 | # It is recommended to not store the screenshots in the git repo. 76 | # Instead, use fastlane to re-generate the screenshots whenever they are needed. 77 | # For more information about the recommended setup visit: 78 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 79 | 80 | fastlane/report.xml 81 | fastlane/Preview.html 82 | fastlane/screenshots/**/*.png 83 | fastlane/test_output 84 | 85 | # Code Injection 86 | # 87 | # After new code Injection tools there's a generated folder /iOSInjectionProject 88 | # https://github.com/johnno1962/injectionforxcode 89 | 90 | iOSInjectionProject/ 91 | -------------------------------------------------------------------------------- /.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Nico 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Package.swift: -------------------------------------------------------------------------------- 1 | // swift-tools-version:5.5 2 | import PackageDescription 3 | 4 | let package = Package( 5 | name: "NSReviewUtility", 6 | platforms: [.iOS(.v14), .macOS(.v11)], 7 | products: [.library(name: "NSReviewUtility", targets: ["NSReviewUtility"])], 8 | targets: [.target(name: "NSReviewUtility")] 9 | ) 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NSReviewUtility 2 | 3 | NSReviewUtility is a package for counting the happiness of a user in your app. It triggers the `SKStoreReviewController` review request when a certain condition happens. You can specify the `happinessIndexCheckCount` and the `daysAfterFirstLaunchCheckCount` to control when the `SKStoreReviewController` should appear at first. The package prevents asking for review when the dialogue already appeared on your current app version or you asked more than three times a year. The ideas for that are from this blog post: [Increase App Ratings by using SKStoreReviewController](https://www.avanderlee.com/swift/skstorereviewcontroller-app-ratings/) 4 | 5 | ## Usage example 6 | 7 | Create a adapter class in your project: 8 | 9 | import NSReviewUtility 10 | 11 | let reviewUtility = ReviewUtilityAdapter().reviewUtility 12 | 13 | class ReviewUtilityAdapter { 14 | 15 | let reviewUtility = NSReviewUtility() 16 | private let reviewUtilityLoggingAdapter = ReviewUtilityLoggerAdapter() 17 | 18 | init() { 19 | reviewUtility.setLoggingAdapter(reviewUtilityLoggingAdapter) 20 | reviewUtility.start() 21 | } 22 | 23 | class ReviewUtilityLoggerAdapter: ReviewUtilityLoggable { 24 | func log(_ message: String) { 25 | //Do your logging here 26 | } 27 | } 28 | } 29 | 30 | When something positive happens in your app: 31 | 32 | func somethingPositiveHappened() { 33 | reviewUtility.incrementHappiness() 34 | } 35 | 36 | When something negative happens in your app: 37 | 38 | func somethingNegativeHappened() { 39 | reviewUtility.decrementHappiness() 40 | } 41 | 42 | When something really bad happened: 43 | 44 | func somethingReallyBadHappened() { 45 | reviewUtility.resetHappiness() 46 | } 47 | 48 | You can also ask for review when possible: 49 | 50 | func manuallyAskForReview() { 51 | reviewUtility.askForReview() 52 | } 53 | 54 | To see this package live in action: 55 | 56 | [NFC・QR Code・Document Scanner on the AppStore](https://apps.apple.com/app/id1249686798) 57 | -------------------------------------------------------------------------------- /Sources/NSReviewUtility/NSReviewUtility.swift: -------------------------------------------------------------------------------- 1 | import StoreKit 2 | 3 | #if os(iOS) 4 | typealias Application = UIApplication 5 | #else 6 | typealias Application = NSApplication 7 | #endif 8 | 9 | public class NSReviewUtility: ObservableObject { 10 | @Published public private(set) var canAskForReview = false 11 | 12 | public var happinessIndexCheckCount = 5 13 | public var daysAfterFirstLaunchCheckCount = 3 14 | 15 | private var isDateDaysAfterFirstLaunchCheckCount: Bool { 16 | if let firstLaunchDate = firstLaunchDate { 17 | let thresholdDate = firstLaunchDate.addingTimeInterval(TimeInterval(daysAfterFirstLaunchCheckCount * 60 * 60 * 24)) 18 | return Date() > thresholdDate 19 | } else { 20 | return false 21 | } 22 | } 23 | 24 | private var currentVersion = Bundle.main.releaseVersionNumber + "-" + Bundle.main.buildVersionNumber 25 | private var didAskForReviewInThisVersion: Bool { versionLastAskedForReview == currentVersion } 26 | private weak var loggingAdapter: ReviewUtilityLoggable? 27 | 28 | public init() {} 29 | 30 | public func start() { 31 | if let firstLaunchDate = firstLaunchDate { 32 | let formatter = DateFormatter() 33 | formatter.dateStyle = .long 34 | loggingAdapter?.log("⭐️ ReviewUtility started. First launched at \(formatter.string(from: firstLaunchDate)), happinessIndexCheckCount: \(happinessIndexCheckCount), daysAfterFirstLaunchCheckCount: \(daysAfterFirstLaunchCheckCount)") 35 | } else { 36 | firstLaunchDate = Date() 37 | loggingAdapter?.log("⭐️ ReviewUtility started for the first time. Setting first launched date to now.") 38 | } 39 | } 40 | 41 | public func setLoggingAdapter(_ loggingAdapter: ReviewUtilityLoggable) { 42 | self.loggingAdapter = loggingAdapter 43 | } 44 | 45 | private func evaluateCanAskForReview() { 46 | DispatchQueue.main.async { 47 | let askedForReviewThisYearCount = self.datesAskedForReview.filter { Calendar.current.isDateInThisYear($0) }.count 48 | let hasLessThanThreeReviewAttemptsThisYear = askedForReviewThisYearCount <= 3 49 | var logString = "⭐️ ReviewUtility asked \(askedForReviewThisYearCount) times this year for a review." 50 | 51 | // max(1, happinessIndexCheckCount) prevents division by zero 52 | let isUserHappy = self.happinessIndex != 0 && (self.happinessIndex % max(1, self.happinessIndexCheckCount) == 0) 53 | 54 | if self.didAskForReviewInThisVersion { 55 | logString += " Asked for review at version: \(self.versionLastAskedForReview), current version is: \(Bundle.main.releaseVersionNumber)." 56 | self.canAskForReview = false 57 | } else { 58 | logString += " currentDate > thresholdDate: \(self.isDateDaysAfterFirstLaunchCheckCount)." 59 | self.canAskForReview = self.isDateDaysAfterFirstLaunchCheckCount && hasLessThanThreeReviewAttemptsThisYear && isUserHappy 60 | } 61 | logString += " Can ask for review: \(self.canAskForReview)" 62 | self.loggingAdapter?.log(logString) 63 | } 64 | } 65 | 66 | public func incrementHappiness() { 67 | happinessIndex += 1 68 | loggingAdapter?.log("⭐️ Incrementing happiness, index is now: \(happinessIndex)") 69 | evaluateCanAskForReview() 70 | } 71 | 72 | public func decrementHappiness() { 73 | if happinessIndex > 0 { 74 | happinessIndex -= 1 75 | loggingAdapter?.log("⭐️ Decrementing happiness, index is now: \(happinessIndex)") 76 | } else { 77 | loggingAdapter?.log("⭐️ Can not decrement happiness because it is already \(happinessIndex)") 78 | } 79 | } 80 | 81 | public func resetHappiness() { 82 | happinessIndex = 0 83 | loggingAdapter?.log("⭐️ Resetting happiness, index is now: \(happinessIndex)") 84 | } 85 | 86 | public func askForReview(force: Bool = false) { 87 | let askForReviewClosure = { [weak self] in 88 | guard let self = self else { return } 89 | self.recordAskForReview() 90 | SKStoreReviewController.askForReview() 91 | self.loggingAdapter?.log("⭐️ Asked for review now") 92 | } 93 | 94 | if force && didAskForReviewInThisVersion { 95 | loggingAdapter?.log("⭐️ Can not force ask for review. Already asked for review in this version.") 96 | } else if force && !didAskForReviewInThisVersion { 97 | askForReviewClosure() 98 | } else if canAskForReview { 99 | askForReviewClosure() 100 | } else { 101 | loggingAdapter?.log("⭐️ Can not ask for review") 102 | } 103 | } 104 | 105 | public func recordAskForReview() { 106 | datesAskedForReview.append(Date()) 107 | versionLastAskedForReview = currentVersion 108 | canAskForReview = false 109 | loggingAdapter?.log("⭐️ Recorded review request") 110 | } 111 | 112 | public func clearAllData() { 113 | datesAskedForReview = [] 114 | firstLaunchDate = Date() 115 | happinessIndex = 0 116 | versionLastAskedForReview = "not asked yet" 117 | loggingAdapter?.log("⭐️ Clearing all data") 118 | } 119 | } 120 | 121 | extension NSReviewUtility { 122 | public private(set) var datesAskedForReview: [Date] { 123 | get { 124 | UserDefaults.standard.value(forKey: "datesAskedForReview") as? [Date] ?? [] 125 | } 126 | set { 127 | UserDefaults.standard.set(newValue, forKey: "datesAskedForReview") 128 | } 129 | } 130 | 131 | public private(set) var firstLaunchDate: Date? { 132 | get { 133 | UserDefaults.standard.value(forKey: "firstLaunchDate") as? Date 134 | } 135 | set { 136 | UserDefaults.standard.set(newValue, forKey: "firstLaunchDate") 137 | } 138 | } 139 | 140 | public private(set) var happinessIndex: Int { 141 | get { 142 | UserDefaults.standard.integer(forKey: "happinessIndex") 143 | } 144 | set { 145 | UserDefaults.standard.set(newValue, forKey: "happinessIndex") 146 | } 147 | } 148 | 149 | public private(set) var versionLastAskedForReview: String { 150 | get { 151 | UserDefaults.standard.string(forKey: "versionLastAskedForReview") ?? "not asked yet" 152 | } 153 | set { 154 | UserDefaults.standard.set(newValue, forKey: "versionLastAskedForReview") 155 | } 156 | } 157 | } 158 | 159 | extension SKStoreReviewController { 160 | public static func askForReview() { 161 | DispatchQueue.main.async { 162 | #if os(iOS) 163 | guard let scene = UIApplication.shared.foregroundActiveScene else { return } 164 | SKStoreReviewController.requestReview(in: scene) 165 | #else 166 | SKStoreReviewController.requestReview() 167 | #endif 168 | } 169 | } 170 | } 171 | 172 | #if os(iOS) 173 | extension UIApplication { 174 | var foregroundActiveScene: UIWindowScene? { 175 | connectedScenes 176 | .first(where: { $0.activationState == .foregroundActive }) as? UIWindowScene 177 | } 178 | } 179 | #endif 180 | 181 | extension Bundle { 182 | var releaseVersionNumber: String { 183 | return infoDictionary?["CFBundleShortVersionString"] as? String ?? "no version number in plist" 184 | } 185 | 186 | var buildVersionNumber: String { 187 | return infoDictionary?["CFBundleVersion"] as? String ?? "no build number in plist" 188 | } 189 | } 190 | 191 | public protocol ReviewUtilityLoggable: AnyObject { 192 | func log(_ message: String) 193 | } 194 | 195 | extension Calendar { 196 | private var currentDate: Date { Date() } 197 | 198 | func isDateInThisYear(_ date: Date) -> Bool { 199 | return isDate(date, equalTo: currentDate, toGranularity: .year) 200 | } 201 | } 202 | --------------------------------------------------------------------------------