Swift 40
Tutorial: Collection View using Swift
One of the best features for developers that came in the iOS 6 SDK is UICollectionView. It’s been a popular tool for developers since, so let’s review how to create one in Swift (if you need it in objective-c read Tutorial: Collection View using Flow Layout).
UICollectionView is very similar to UITableView but you can customize it a lot more and it can scroll horizontal, goodbye scroll views! Most recently I used a collection view for a bottom navigation scroller and it worked really well.
A UICollectionView view has three main components:
1. Cells: Display your content in cells that are de-queued as they leave the screen.
2. Supplementary Views: Add labels, section headers and footers to define your content areas.
3. Decoration Views: Decorate the collection view to look like a bookshelf or a background image.
Start by adding the delegates to your class file and define your collection view.
class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource { @IBOutlet var collectionView: UICollectionView?
The most common layout for a UICollectionView is UICollectionViewFlowLayout. Flow layout organizes items into a grid with optional header and footer views for each section. The items in the collection view flow from one row or column (depending on the scrolling direction) to the next, with each row comprising as many cells as will fit. Cells can be the same sizes or different sizes.
Define the UICollectionView & UICollectionViewFlowLayout properties in the viewDidLoad method.
override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() layout.sectionInset = UIEdgeInsets(top: 20, left: 10, bottom: 10, right: 10) layout.itemSize = CGSize(width: 90, height: 90) collectionView = UICollectionView(frame: self.view.frame, collectionViewLayout: layout) collectionView!.dataSource = self collectionView!.delegate = self collectionView!.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier: "CollectionViewCell") collectionView!.backgroundColor = UIColor.whiteColor() self.view.addSubview(collectionView!) }
Most of the properties are self explanatory but the key is to define your cell class if you are using custom cells (just like table views) and setting the size of the cells, above they are set to 90×90. Choose your section insets which is the spacing in between each cell, I have it set to 20 for the top and 10 for the rest. Lastly define the layout, we’re using let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout().
Setting up a UICollectionView is very similar to a UITableView, so you’ll recognized a lot of the methods below.
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { return 1 } func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 20 }
Above we’re defining how many items and sections are in the collection view. If you have more than one section, then you’ll need to define the number of sections, if you only have one you don’t need that method.
Below is the real meat of the UICollectionView, defining the cells with your data. If you choose to use a custom cell you can add images or whatever you need to display.
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) as CollectionViewCell cell.backgroundColor = UIColor.blackColor() cell.textLabel?.text = "\(indexPath.section):\(indexPath.row)" cell.imageView?.image = UIImage(named: "circle") return cell }
Finally you can drag the UICollectionView object into your view within your NIB or storyboard. Move and scale the collection view to where you want to display it and review all of the properties within the inspector. Be sure to hookup the collectionView reference outlet and dataSource and delegates so the collection view can provide all the functionality available. Do the same for the Collection View Flow Layout. (See image on the right).
You can grab the full source code for this tutorial. Note: Built for XCode6-Beta7.
If you need it built using Xcode 6.3 (Swift 1.2).
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
- 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).
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 Swift
Note: Newer tutorial available. If you would like to learn how to implement FBSDKGraphRequest using Facebook SDK 4.0 using Swift, read Tutorial: How To Use Login in Facebook SDK 4.0 for Swift
You’ve probably noticed that most successful games contain a Facebook Leaderboard so users can compete against their friends. You can read more about Game Strategy: Make It Social in one of my previous posts.
Below is a tutorial to help you access the users friends list using the latest Facebook SDK and Graph API in Swift.
Get Friends Using Facebook Graph API v2.0
- Visit the Getting Started with the Facebook iOS SDK documentation to download the Facebook SDK and install it.
- Read and implement my previous post Tutorial: Facebook Login in Swift using a Bridging Header to handle the Facebook Login into your app using Swift.
- In v2.0 of the Graph API, you must request the user_friends permission from each user. user_friends is no longer included by default in every login. Each user must grant the user_friends permission in order to appear in the response to /me/friends.
- Apps are no longer able to retrieve the full list of a user’s friends (only those friends who have specifically authorized your app using the user_friends permission). This has been confirmed by Facebook as ‘by design’.
- Use the following piece of code to get the list of the users friends. This can be used anywhere within your app as long as the user has an active logged in session.
// Get List Of Friends var friendsRequest : FBRequest = FBRequest.requestForMyFriends() friendsRequest.startWithCompletionHandler{(connection:FBRequestConnection!, result:AnyObject!, error:NSError!) -> Void in var resultdict = result as NSDictionary println("Result Dict: \(resultdict)") var data : NSArray = resultdict.objectForKey("data") as NSArray for i in 0..<data.count { let valueDict : NSDictionary = data[i] as NSDictionary let id = valueDict.objectForKey("id") as String println("the id value is \(id)") } var friends = resultdict.objectForKey("data") as NSArray println("Found \(friends.count) friends") }
- If successful you’ll be returned an NSDictionary in JSON with an array of data that contains the basic data for each of the users friends who use your app. Note: Be sure to test using another Facebook account that is friends with your account.
Result Dict: { data = ( { "first_name" = Brian; id =
; "last_name" = Coleman; name = "Brian Coleman"; } ); paging = { next = "https://graph.facebook.com/v2.0/ /friends?fields=id,name,first_name,last_name&format=json&access_token= &offset=5000&__after_id= "; }; summary = { "total_count" = 1; }; } the id value is Found 1 friends - The next step is to use the list of Facebook user id’s returned in the JSON and populate them into an array so you can compare those user id’s with the user scores in your database, then display that subset of scores in a table view leaderboard.
Learn Swift by Video
I don’t know about you, but when I need to learn something I find it easier to watch someone else do it first. That’s why when I wanted to learn Swift I went in search of the best video series I could find. It’s almost like being in a classroom and listening to an instructor teach the content, instead of reading from a book. It also helps to follow along with XCode open so you can complete the steps together, pausing when you need more time to complete what you’re working on.
Below are a bunch of great “getting started” videos for learning Swift.
WWDC 2014 Developer Conference Videos
Introduction to Swift
Swift is Apple’s brand-new programming language for writing great iOS and OS X apps. Learn the basics of the language. See how to declare variables, use the fundamental data types, declare functions, and implement classes. Explore some of the great features that make Swift a safe, modern, and extremely powerful language.
Download: HD | SD | PDF
Intermediate Swift
Explore the modern features of the Swift programming language. Learn about object initialization, closures, and optionals. See how you can perform pattern matching using Swift’s powerful switch statements.
Download: HD | SD | PDF
Advanced Swift
Dive deep into Swift! Explore some of its more powerful features, like operator overloading, string interpolation, and advanced pattern matching. Learn how to take advantage of generics to write terse and expressive code.
Download: HD | SD | PDF