How can I add a link for a rate button with swift?

Solution 1:

Try This, change appId in your method by your App ID

Swift 5

import StoreKit

func rateApp() {
    if #available(iOS 10.3, *) {
        SKStoreReviewController.requestReview()

    } else if let url = URL(string: "itms-apps://itunes.apple.com/app/" + "appId") {
        if #available(iOS 10, *) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)

        } else {
            UIApplication.shared.openURL(url)
        }
    }
}

Swift 3 \ 4

func rateApp() {
    guard let url = URL(string: "itms-apps://itunes.apple.com/app/" + "appId") else {
        return
    }
    if #available(iOS 10, *) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)

    } else {
        UIApplication.shared.openURL(url)
    }
}

id959379869 This is the id when you go on your Itune page of your app

Example : https://itunes.apple.com/fr/app/hipster-moustache/id959379869?mt=8

How get the ID :

  1. Itunesconnect account
  2. My Apps
  3. Click on "+" Button
  4. New iOS App
  5. Fill require details
  6. After filling all details goto your App
  7. Click on More Button
  8. View on AppStore
  9. It will redirect you to your App URL this will be universal
  10. Look Http URL

Solution 2:

This is working the best for me. Directs the user straight to the 'Write A Review' composer of the application.

Swift 3.1 (Support for iOS10 and below)

Introduces new action=write-review

let appID = "959379869"

if let checkURL = URL(string: "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=\(appID)&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8") {
    open(url: checkURL)
} else {
    print("invalid url")
}

...

func open(url: URL) {
    if #available(iOS 10, *) {
        UIApplication.shared.open(url, options: [:], completionHandler: { (success) in
            print("Open \(url): \(success)")
        })
    } else if UIApplication.shared.openURL(url) {
            print("Open \(url)")
    }
}

Tested and works on Swift 2.2

let appID = "959379869" // Your AppID
if let checkURL = NSURL(string: "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=\(appID)&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8") {
    if UIApplication.sharedApplication().openURL(checkURL) {
        print("url successfully opened")
    }
} else {
    print("invalid url")
}

Solution 3:

Swift 4

let url = URL(string: "itms-apps:itunes.apple.com/us/app/apple-store/id\(YOURAPPID)?mt=8&action=write-review")!
UIApplication.shared.openURL(url)

Solution 4:

Now after iOS 10.3+

The SKStoreReviewController allows users to rate an app directly from within the app through a dialog box. The only downsite is that you can only request StoreKit to display the dialog, but can't be sure if it will.

import StoreKit

func requestToRate() {
    SKStoreReviewController.requestReview()
}

Solution 5:

Swift 5.1: The following function sends your user directly to the review section of ANY store, not just on the American one:

func rateApp(id : String) {
    guard let url = URL(string : "itms-apps://itunes.apple.com/app/id\(id)?mt=8&action=write-review") else { return }
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    } else {
        UIApplication.shared.openURL(url)
    }
}

Usage:

rateApp(id: "1287000522")

Important Note: This doesn't work on simulator! Test it on a real device.