简体   繁体   中英

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

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

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

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")
}

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()
}

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)

You can use the following function and replace the APP_ID with your one. Calling this function will open the app in app store link and user will see the Review page where he can click and write a review easily.

func rateApp(){
   UIApplication.sharedApplication().openURL(NSURL(string : "itms-apps://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=\(APP_ID)&onlyLatestVersion=true&pageNumber=0&sortOrdering=1)")!);
}

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.

For iOS 10.3+ you can use SKStoreReviewController with simple dialog, and choose rating in alert-style window. To use it, you should import StoreKit library. So, universal way to rate your app inside itself is like this:

import StoreKit

func rateApp(){
   if #available(iOS 10.3, *) {
      SKStoreReviewController.requestReview()
   } else {
      guard let url = URL(string: "itms-apps://itunes.apple.com/ru/app/cosmeteria/id1270174484") else {
         return
      }

      if #available(iOS 10.0, *) {
         UIApplication.shared.open(url, options: [:], completionHandler: nil)
      } else {
         UIApplication.shared.openURL(url)
      }
   }
}

And when you try to launch it in simulator, you won't see App Store window, so try it on device and it gonna work. This way covers all iOS versions, using all abilities. And the part of path in you application address "/us/app" means your App Store localisation, for example "us" means USA. You can easily find your app id in address string just by opening app in App Store in any browser.To get the link, just copy address in browser. Changing "https://" for "itms-apps://" lets you to open app in App Store application, while "https" opens web page in Safari

WARNING: If you are running your app on a simulator

 UIApplication.sharedApplication().openURL(NSURL(string : "url")!) 

will not work because there is no app store in the simulator. In order to test this functionality you must run your app on a device.

Swift 3

  func rateApp(){
UIApplication.shared.open(URL(string : "itms-apps://itunes.apple.com/app/id959379869")!, options: [:]) { (done) in
  // Handle results

}}

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

转到您的itunesconnect帐户->我的应用程序->单击“+”按钮->新建iOS应用程序->填写要求详细信息->填写所有详细信息后转到您的应用程序->单击更多按钮->在AppStore上查看->它将将您重定向到您的应用程序 URL,这将是通用的,并且在您的应用程序上线后将保持不变。

All the above answers are not best practices they might be affecting your app store ratings. For best practice use the below code.

func ReviewAppController() {
    let alert = UIAlertController(title: "Feedback", message: "Are you enjoying our App?", preferredStyle: .alert)
    alert.addAction(UIAlertAction(title: "Dismis", style: .cancel, handler: nil))
    alert.addAction(UIAlertAction(title: "Yes, i Love it!", style: .default, handler: {_ in
        SKStoreReviewController.requestReview()
    }))
    alert.addAction(UIAlertAction(title: "No, this sucks!", style: .default, handler: {_ in
        //Collect feedback
    }))
    present(alert, animated: true)
}

This link opens your app page in the App Store and then presents the write review sheet .

itms-apps://itunes.apple.com/app/id[APP_ID]?action=write-review

You can find the APP_ID in the App Store Connect under the details of your app as Apple ID .

In case you want to directly write a review rather than just open an app page:

    if let url = URL(string: "https://itunes.apple.com/in/app/\(yourappname)/id\(yourAppleAppId)?ls=1&mt=8&action=write-review") {
       if #available(iOS 10.0, *) {
           UIApplication.shared.open(url, options: [:], completionHandler: nil)
       } else {
           // Earlier versions
           if UIApplication.shared.canOpenURL(url as URL) {
              UIApplication.shared.openURL(url as URL)
           }
       }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM