
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
- Create “appVersion.php” on your Server. This can be created using any kind of Web API (Perl, PHP, Ruby, etc..).
- Write this code into your file. This code will give out your latest App Version.
Get App Version using NSURLConnection
- 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
- Then you need to compare the minimum app version from the web server against the app version the user has on their device.
- 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.
- 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..
- 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).