iOS Life

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

Tutorial: Force Upgrade in Swift

Sometimes you need all users to use the same version of your App. There are many reasons for this but the most popular is to ensure all users have a specific new feature that’s only available in the latest version. Another popular reason is you changed a data source or functionality that is no longer supported in older versions of your app. This will force the user to upgrade the app on their device and will not allow them to use their current version anymore.

It’s important that this feature is built into your very first release of your application (v1.0) so you can force all users to upgrade when you need to.

Below is a tutorial to help you add a forced upgrade alert in Swift.

Web Server API

  1. Create “appVersion.php” on your Server. This can be created using any kind of Web API (Perl, PHP, Ruby, etc..).
  2. Write this code into your file. This code will give out your latest App Version.
    
    

Get App Version using NSURLConnection

  1. You’ll need the following method to read in the latest App Version from the Web Server API.
    func getAppVersion() {
        let url = NSURL(string:"http://www.brianjcoleman.com/code/appVersion.php")
        let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
        var request = NSMutableURLRequest(URL: url!, cachePolicy: cachePolicy, timeoutInterval: 2.0)
        request.HTTPMethod = "POST"
        
        // set Content-Type in HTTP header
        let boundaryConstant = "----------V2ymHFg03esomerandomstuffhbqgZCaKO6jy";
        let contentType = "multipart/form-data; boundary=" + boundaryConstant
        NSURLProtocol.setProperty(contentType, forKey: "Content-Type", inRequest: request)
        
        // set data
        var dataString = ""
        let requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
        request.HTTPBody = requestBodyData
        //println("Data String: \(dataString)")
        // set content length
        //NSURLProtocol.setProperty(requestBodyData.length, forKey: "Content-Length", inRequest: request)
        
        var response: NSURLResponse? = nil
        var error: NSError? = nil
        let reply = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&error)
        
        let results = NSString(data:reply!, encoding:NSUTF8StringEncoding)
        println("API Response: \(results)")
        self.serverAppVersion = results!
    }
    

Display UIAlertController in Swift

  1. Then you need to compare the minimum app version from the web server against the app version the user has on their device.
  2. If the minimum app version from the server is greater than the users app version it will display an alert view and force the user to upgrade.
  3. Add the method below into your first view controller in your app.
    func checkForAppUpdate() {
        
        getAppVersion()
        
        var minAppVersion = self.serverAppVersion
        var appVersion = NSBundle.mainBundle().objectForInfoDictionaryKey("CFBundleShortVersionString") as String
        
        println("Current Version: \(appVersion) -- Minimum Version: \(minAppVersion)")
        
        //Compare App Versions
        var minAppVersionComponents : NSArray = minAppVersion.componentsSeparatedByString(".")
        var appVersionComponents : NSArray = appVersion.componentsSeparatedByString(".")
        
        var needToUpdate = false
        
        for i in 0..
    
  4. Lastly you'll need to call the checkForAppUpdate() from viewDidLoad() so it will only get called the when the app is initially loaded.

You can grab the full source code for this tutorial. Note: Built using XCode6.1
If you need it built using Xcode 6.3 (Swift 1.2).

Jul 31, 2014 Brian Coleman
Tutorial: Rate Me using UIAlertController in SwiftTutorial: Collection View using Swift
You Might Also Like
 
Tutorial: Setting up a Jenkins Automated Build Server for iOS
 
Tutorial: In-App Purchases in Swift
8 years ago Swift, Tutorialsios8, swift, tutorial13,609
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
163,489 views
Tutorial: How to test your app for IPv6 compatibility
102,074 views
Tutorial: How to use Auto Layout in Xcode 6
89,234 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