How to Add "Write a Review" / "Rate Us" Feature to My App?

Viewed 41312

I wish to add some sort of a "Write a Review" or "Rate Us" feature to my app so my customers can easily rate and review my app.

Best practice I can think of is to have some sort of pop-up or open a UIWebView within my app so the user is not kicked off of my app while opening the App Store application as done in:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms://itunes.com/apps/myAppName"]];

Does anyone knows of a way to do that?

3 Answers

enter image description hereDeclare Variable

let reviewService = ReviewService.shared

In View Did Appear

self.reviewService.rateAlert(from: self)

Import the Class

import UIKit
import StoreKit

class ReviewService: NSObject {
    
    static let shared = ReviewService()
    
    private var lastRequest : Date? {
        
        get {
            
            return UserDefaults.standard.value(forKey: "lastRequest") as? Date
        }
        set {
            
            UserDefaults.standard.set(newValue, forKey: "lastRequest")
        }
    }
    
    private var oneDayAgo : Date {
        
        return Calendar.current.date(byAdding: .day, value: -1, to: Date()) ?? Date()
    }
    
    private var shouldReview : Bool {
        if lastRequest == nil {
            return true
        }
        else if let lastRequest = self.lastRequest , lastRequest < oneDayAgo {
            return true
        }
        else {
            
            return false
        }
    }
    
    func requestReview(isWrittenReview : Bool = false) {
        
        if isWrittenReview {
            let appID = "##########" // App Id
            
            let urlStr = "https://itunes.apple.com/app/id\(appID)?action=write-review"
            
            guard let url = URL(string: urlStr), UIApplication.shared.canOpenURL(url) else { return }
            
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        }
        else {
            SKStoreReviewController.requestReview()
        }
        
    }
    
    //MARK:- Rate App
    func rateAlert(from : UIViewController?) {
        
        guard shouldReview else {
            print("Don't Show")
            return
        }
        
        guard let vc = from else {
            
            return
        }
        
        lastRequest = Date()
        
        let alert = UIAlertController(title: "Rate our App", message: "If you love our app, please take a moment to rate it in the App Store", preferredStyle: .alert)
        
        
        let action1 = UIAlertAction(title: "Rate", style: .default, handler: {(_ action: UIAlertAction?) -> Void in
            
            self.requestReview(isWrittenReview: false)
            
        })
        
        let action2 = UIAlertAction(title: "Send Feedback", style: .default, handler: {(_ action: UIAlertAction?) -> Void in
            
            self.requestReview(isWrittenReview: true)
            
        })
        
        let action3 = UIAlertAction(title: "Close", style: .default, handler: nil)
        
        alert.addAction(action1)
        alert.addAction(action2)
        alert.addAction(action3)
        vc.present(alert,animated: true)
        
    }
    }
Related