Created
December 3, 2020 16:59
-
-
Save Ilesh/a640fec1b1c93b6d9c4025d69c38c47f to your computer and use it in GitHub Desktop.
InApp Ratings & Custom alert.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// | |
// Created by Ilesh's 2018 on 02/07/19. | |
// Copyright © 2019 Ilesh. All rights reserved. | |
// | |
import Foundation | |
import StoreKit | |
extension UserDefaults { | |
enum Key: String { | |
case reviewWorthyActionCount | |
case lastReviewRequestAppVersion | |
} | |
func integer(forKey key: Key) -> Int { | |
return integer(forKey: key.rawValue) | |
} | |
func string(forKey key: Key) -> String? { | |
return string(forKey: key.rawValue) | |
} | |
func set(_ integer: Int, forKey key: Key) { | |
set(integer, forKey: key.rawValue) | |
} | |
func set(_ object: Any?, forKey key: Key) { | |
set(object, forKey: key.rawValue) | |
} | |
} | |
enum AppStoreReviewManager { | |
static let minimumReviewWorthyActionCount = 3 | |
static func requestReviewIfAppropriate() -> Bool { | |
let defaults = UserDefaults.standard | |
let bundle = Bundle.main | |
var actionCount = defaults.integer(forKey: .reviewWorthyActionCount) | |
actionCount += 1 | |
defaults.set(actionCount, forKey: .reviewWorthyActionCount) | |
guard actionCount >= minimumReviewWorthyActionCount else { | |
return false | |
} | |
let bundleVersionKey = kCFBundleVersionKey as String | |
let currentVersion = bundle.object(forInfoDictionaryKey: bundleVersionKey) as? String | |
let lastVersion = defaults.string(forKey: .lastReviewRequestAppVersion) | |
guard lastVersion == nil || lastVersion != currentVersion else { | |
return false | |
} | |
SKStoreReviewController.requestReview() | |
defaults.set(0, forKey: .reviewWorthyActionCount) | |
defaults.set(currentVersion, forKey: .lastReviewRequestAppVersion) | |
return true | |
} | |
} | |
class IPAppRate { | |
static let shared : IPAppRate = IPAppRate() | |
func askRatingOption(vc:UIViewController){ | |
let alert = UIAlertController.init(title: "Rate our App", message: "If you love our app, please take a moment to rate it in the AppStore", preferredStyle: .alert) | |
alert.addAction(UIAlertAction(title: "Rate", style: .default, handler: { (action) in | |
self.writeReview() | |
})) | |
alert.addAction(UIAlertAction(title: "Feedback", style: .default, handler: { (action) in | |
IPMailComposer.openOptions(controller: vc, strEmail: ["info@kingfisher-consultancy.com"]) | |
})) | |
alert.addAction(UIAlertAction(title: "Close", style: .cancel, handler: { (action) in | |
})) | |
vc.present(alert, animated: true, completion: nil) | |
} | |
private let productURL = URL(string: "https://itunes.apple.com/app/idxxxxxxxx")! | |
private func writeReview() { | |
var components = URLComponents(url: productURL, resolvingAgainstBaseURL: false) | |
components?.queryItems = [ | |
URLQueryItem(name: "action", value: "write-review") | |
] | |
guard let writeReviewURL = components?.url else { | |
return | |
} | |
UIApplication.shared.open(writeReviewURL) | |
} | |
func rateNowAction(){ | |
self.writeReview() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment