iOS Life

  • Home
  • ABOUT
  • SERVICES
  • PORTFOLIO
  • BLOG
  • Contact

Tutorial: Rate Me using UIAlertController in Swift

We all know that good reviews for our apps will influence new users to download the app. It doesn’t help that there is no standard way to ask users for a review. The only way to get your app reviewed is on your app page within the App Store. How many people after downloading your app will again go back to AppStore, search for your app, go to rate screen and give you a rating? Not many outside of your own family and friends!

If you prompt the customer at some point while the app is running you’re always going to disrupt their workflow to some degree. You can alleviate this by trying to pick a moment that’s the least disruptive. If possible choose a moment when something positive or rewarding has just happened.

Below is a tutorial to help you add a Rate Me alert view in Swift.

Setup Rate Me

The method below will control the logic of when the Rate Me alert view is displayed to the user. Enter in the minimum number of sessions the app should be opened before showing the request to rate the app the first time and how often it should try again if the user selects “Maybe Later”. The numLaunches and neverRate values are stored using NSUserDefaults.

var iMinSessions = 3
var iTryAgainSessions = 6

func rateMe() {
    var neverRate = NSUserDefaults.standardUserDefaults().boolForKey("neverRate")
    var numLaunches = NSUserDefaults.standardUserDefaults().integerForKey("numLaunches") + 1
    
    if (!neverRate && (numLaunches == iMinSessions || numLaunches >= (iMinSessions + iTryAgainSessions + 1)))
    {
        showRateMe()
        numLaunches = iMinSessions + 1
    }
    NSUserDefaults.standardUserDefaults().setInteger(numLaunches, forKey: "numLaunches")
}

Using UIAlertController in Swift

UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead. Now UIAlertController is a single class for creating and interacting with what we knew as UIAlertViews and UIActionSheets on iOS 8.

If you would like to use UIAlertView with multiple actions, the method below is the best way to handle each button tapped. The advantage of using completion handlers is you can easily handle multiple alert views within a single view controller.

func showRateMe() {
    var alert = UIAlertController(title: "Rate Us", message: "Thanks for using ", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Rate ", style: UIAlertActionStyle.Default, handler: { alertAction in
        UIApplication.sharedApplication().openURL(NSURL(string : "itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id="))
        alert.dismissViewControllerAnimated(true, completion: nil)
        }))
    alert.addAction(UIAlertAction(title: "No Thanks", style: UIAlertActionStyle.Default, handler: { alertAction in
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "neverRate")
        alert.dismissViewControllerAnimated(true, completion: nil)
        }))
    alert.addAction(UIAlertAction(title: "Maybe Later", style: UIAlertActionStyle.Default, handler: { alertAction in
        alert.dismissViewControllerAnimated(true, completion: nil)
        }))
    self.presentViewController(alert, animated: true, completion: nil)
}

If you just need an alert view with a single action “Ok”, the following code will do.

let alert = UIAlertView()
    alert.title = "Alert"
    alert.message = "Here's a message"
    alert.addButtonWithTitle("Understod")
    alert.show()
Jul 30, 2014 Brian Coleman
Tutorial: Get Facebook Friends using the Facebook SDK in SwiftTutorial: Force Upgrade in Swift
You Might Also Like
 
iOS7, We’ve Got A Problem
 
Tutorial: How to use Auto Layout in Xcode 6
6 years ago Swift, Tutorialsios, swift, tutorial12,991
Follow Me
    
Categories
  • About Me
  • Frameworks
  • My Apps
  • News
  • Strategy
  • Swift
  • Tools
  • Tutorials
  • tvOS
  • Uncategorized
  • Videos
  • Watch
Archives
  • May 2016
  • January 2016
  • October 2015
  • July 2015
  • May 2015
  • April 2015
  • March 2015
  • November 2014
  • October 2014
  • September 2014
  • July 2014
  • June 2014
  • September 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
brianjcoleman on Twitter
  • Classix is still holding in the top charts on Apple TV in Canada. #55 Free, #23 Top Grossing. #tvos #appletv #app https://t.co/xuEJiT4rro, Jul 14
  • New Blog Post: "Classix for iPhone, iPad & Apple TV” #iOSDev #ios #swift #swiftlang #SwiftDevs #AppleTV Read here: https://t.co/uF6w3gYOot, May 20
  • New Blog Post: "How to test your app for IPv6 compatibility” #iOSDev #ios #swift #swiftlang #SwiftDevs Read here: https://t.co/SveichSUep, May 6

Recent Posts
  • Classix for iPhone, iPad & Apple TV
  • Tutorial: How to test your app for IPv6 compatibility
  • Tutorial: Testing SSL using Charles Proxy on an iOS Device
  • Tutorial: 3D Touch – Quick Actions in Swift
  • tvOS Tutorial: Top Shelf in Swift
Featured Apps
Classix
Sportsnet
TAGS
tutorialswiftios8iosobjective-cvideostrategygamesframeworknewsappsmonitizeios7applefacebookwatchtoolstvosios9bookdesignsocialapiprovisiontutorialsbooksiapiTunes ConnectIPv6
Search
ABOUT
Brian is a Lead iOS/tvOS Developer from Toronto with over 18 years of multifaceted experience including development, design, business analysis and project management.
MOST VIEWED
Tutorial: How To Use Login in Facebook SDK 4.1.x for Swift
162,608 views
Tutorial: How to test your app for IPv6 compatibility
99,880 views
Tutorial: How to use Auto Layout in Xcode 6
88,713 views
FOLLOW ME
    
Email Subscription
Sign up for my newsletter to receive the latest news and tutorials posted.

Enter your email address:

2013-2017 © Brian Coleman