Brian Coleman

  • 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()
Tutorial: Get Facebook Friends using the Facebook SDK in SwiftTutorial: Force Upgrade in Swift
Brian Coleman

Manager, Mobile Development at Rogers Communications with over 15 years of multifaceted experience including development, design, business analysis and project management working directly with clients in a consulting capacity throughout the full software life cycle.

July 30, 2014 Swift, Tutorialsios, swift, tutorial
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
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-cvideogamesstrategynewsframeworkappsmonitizefacebookwatchappleios7toolstvosios9apiprovisionsocialtutorialsbooksdesignbookiapIPv6iTunes Connect
Search
TAGS
tutorialswiftios8iosobjective-cvideogamesstrategynewsframeworkappsmonitizefacebookwatchappleios7toolstvosios9apiprovisionsocialtutorialsbooksdesignbookiapIPv6iTunes Connect
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.

FOLLOW ME
    
Email Subscription
Sign up for my newsletter to receive the latest news and tutorials posted.

Enter your email address:

2023 © Brian Coleman