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

46,386

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.

Share:
46,386
Luis Felipe
Author by

Luis Felipe

I like to make iOS apps :)

Updated on July 05, 2022

Comments

  • Luis Felipe
    Luis Felipe almost 2 years

    First I don't know how to get the link before I submit my app, and if the link is for each country app store or is it universal?

    Also I don't know if the way to do it is just by putting the link there like:

    @IBAction func rate(sender: AnyObject) {
        UIApplication.sharedApplication().openURL(NSURL(string : "webLinkHere")!)
    }
    

    Or should I use another way to do this?

    Thanks

  • Luis Felipe
    Luis Felipe over 9 years
    itunes.apple.com/us/app/tap-dont-tap/… This is my link, it says US so now I don't know if it is universal
  • Krunal Darji
    Krunal Darji over 9 years
    ya it's universal you go ahead with this , so currently you don't have approved binary that's why its showing item not available
  • vkalit
    vkalit about 8 years
    Do APP_ID have format: "id912873918273" or "912873918273" or "com.Companyname.Appname" ?
  • Paul Metas
    Paul Metas about 8 years
    I have a live app on the App Store. The above code works 100%. I have tested it a moment ago. For those looking for a current solution, this it it :-)
  • Tai Le
    Tai Le almost 8 years
    The AppStore opened, but it raise an error: Your request produced an error. [newNullResponse]
  • Mahmud Ahsan
    Mahmud Ahsan almost 8 years
    Now I just use this one: UIApplication.sharedApplication().openURL(NSURL(string : "itunes.apple.com/app/id_APPID")!)
  • MQoder
    MQoder over 7 years
    Thanks complete answer :). you did forget to actualy open the URL on the swift 3.0 example!
  • Simon
    Simon over 7 years
    thanks @MQoder :) Updated it, along with support for the deprecated open function in iOS 10.
  • thibaut noah
    thibaut noah almost 7 years
    The guard on iOS 10 seems unecessary?
  • thibaut noah
    thibaut noah almost 7 years
    your reviewString isn't used anywhere mate
  • MBH
    MBH almost 7 years
    But the code should be tested, there is reviewString is not used. There is also \(success) variable which is used but not declared in else statement
  • Sam Spencer
    Sam Spencer almost 7 years
    This is incorrect. The "openURL" function works on the simulator and devices. However, depending on the URL you supply, the simulator may not be able to handle it.
  • YannSteph
    YannSteph almost 7 years
    @thibautnoah The guard is useful
  • thibaut noah
    thibaut noah almost 7 years
    @YannickSteph if you follow the guidelines it isn't and an if statement should be used instead
  • thibaut noah
    thibaut noah almost 7 years
    @MahmudAhsan there is an auto-completion error at the end of your link, an extra end of parenthese inside the string that shouldn't be here, since it is one character i cannot edit it.
  • Markus
    Markus over 6 years
    PLEASE, THIS SOLUTION WORKS EXCEPT FOR THE NEW APP STORE (iOS 11) I IMPLEMENTED AND TESTED THIS CODE ON iOS 9 AND iOS 10, BUT NOT WORK ON iOS 11.
  • Arvind
    Arvind over 6 years
    any solution that works for both IOS 11 and previous versions?
  • Amr Lotfy
    Amr Lotfy about 6 years
    requestReview is not guaranteed to show up each time in real device.
  • Brewski
    Brewski almost 6 years
    This is the only one that worked for me in this post using Swift4, thanks!
  • Jason Short
    Jason Short over 5 years
    This should be the new approved answer. Since 10.3 should be almost everyones target, this gets rid of the redirect ugliness and sends directly to the store. Very clean.
  • Lucas Chwe
    Lucas Chwe about 5 years
    this is the right answer. this is how apps with heavy traffic get thousands of reviews.
  • Wo_0NDeR ᵀᴹ
    Wo_0NDeR ᵀᴹ almost 5 years
    Awesome answer but why don't use SKStoreReviewController with a logic controlled by the app launch count or something similar??
  • RJB
    RJB almost 5 years
    Dont forget import StoreKit
  • Surendra Kumar
    Surendra Kumar almost 5 years
    I was using this URL in my old app but it is not working with iOS 11.* . What is the new URL now? Thank you!
  • Surendra Kumar
    Surendra Kumar almost 5 years
    @Alfi answer is working with swift4 and iOS 11.* device
  • TheTeacher33
    TheTeacher33 over 4 years
    Apple documentation says exactly this regarding the use of SKStoreReviewController.requestReview. "Although you should call this method when it makes sense in the user experience flow of your app, the actual display of a rating/review request view is governed by App Store policy. Because this method may or may not present an alert, it's not appropriate to call it in response to a button tap or other user action."
  • Bhaumik
    Bhaumik over 3 years
    SKStoreReviewController is a good way to get feedback on your app. However, you should be aware that the prompt will only be displayed to a user a maximum of three times within a 365-day period. More information on this: developer.apple.com/documentation/storekit/…
  • Ahmadreza
    Ahmadreza over 3 years
    Sadly it's depricated on iOS 14
  • Mrugesh Tank
    Mrugesh Tank almost 3 years
    Perfect, this is what I was looking for.
  • ChuckZHB
    ChuckZHB over 2 years
    This works, I just test it on my live app in App Store.