We have merged this example into Matt Donnelley’s Swifter Repo, where it will be more actively maintained along with future releases of Swifter. The code is very much the same, so the tutorial still applies, but you’ll want to pull down from his Swifter repo directly and expect some slight variations to the code here.
Build a simple twitter application that leverages the Swifter library (also built in Swift) and UITableViews to display an user’s
It’s only been a couple weeks, and already developers are diving in and creating cool things with Swift. We’re really excited to be using Swift ourselves, and we’ve been working hard to build applications that showcase some of the new functionality and paradigms that the swift language brings to the app world.
Our first Swift tutorial will be focused on using the Swifter framework to connect to twitter. The Swifter library makes authorization, fetching, and posting data to twitter very easy. In this tutorial, we will focus on authorizing a user and then displaying the latest tweets in a UITableView. If you are not already familiar with the latest apple releases, there are many great resources for learning Xcode 6 and the Swift programming language. We are going to start off this tutorial assuming you know your way around, but even if you don’t know Swift or Xcode 6, you will get enough of a primer from our tutorial that you should be able to build this app from scratch or download the source and understand it.
Create a new iOS Project in Xcode 6
We have some prerequisites for this application. We will be building with Xcode 6 and building for iOS 8 in simulator. This should be zero configuration if you’ve downloaded the beta or newer of Xcode from the Apple Developer Center. Select the Single View Application under the iOS > Application menu on the left side of the window.
Fill in your app details. To keep things simple, we are going to build for iPhone only. You will obviously want to choose swift as your language.
Add the Swifter Framework to your Application
To use the Swifter framework in our application, we need to download the Swifter source and drag the Xcode project into our own. If you are working from our source code, the Swifter framework is already included as a git submodule so you can simply copy and paste this into terminal:
git clone --recursive [email protected]:swiftcast/Swift-UITableView-with-Swifter-Example.git
Select your main (parent) Xcode project and on the General settings page scroll near the bottom and click the + under “Linked Frameworks and Libraries.” You should see SwifteriOS.framework listed at the top of the modal window, which we can select and add.
Status should be set to Required by default, but if not, set it! While we’re on this page, lets lock down our device orientation to portrait, and make sure the deployment target is set to iOS 8.
Create Storyboards Views and use Autolayout
Since we are mainly focused on creating for the iPhone, let’s target the portrait mode by selecting compact width and regular height from our autolayout target guide.
Next we will add the image source for a twitter button that we will create on our main Storyboard view.
Now that we have the image for our button, let’s go add an actual button component to our view. Clear out the default text and set the image field to your twitter button.
Your button should default to the width of the image and you can drag the image to center it on the page. You can also do this with autolayout constraints by selecting the button and selecting the alignment constraints menu and selecting to center horizontally/vertically in container.
Lets now create a custom action for when this button is pressed. Select the assistant editor (tux button in the top right) and ctrl+drag the button onto your ViewController.swift file.
Make sure to select the action in the connection menu. We will call ours doTwitterLogin
Lastly, we added a second view controller with a table view dropped in. To keep things organized, we created a Controllers group in the project, renamed our ViewController.swift to AuthView.swift and dropped it into the controllers group. You should then, also make an additional Swift controller (you can clone the original) and assign each of your two views from the Storyboard to AuthView and RecentTweets respectively.
Done with Storyboards, time for Swift Code!
Rather than fragmenting code with no context of where those snippets really belong, we like to keep our controllers light enough and well commented so you can simply read the code and hopefully understand what is going on:
Build your iOS Twitter App!
- Does it run?
- Did you run into problems?
- What modifications have you tried?
- See any typos in our code or comments?
- Anything you need more clarification on?