Access Twitter timeline via ACAccount & SLRequest in Swift

Okay first things first, I did this way back when Swift first hit the scene and have since been preoccupied with several Objective-C projects. However I’m sure not much has changed so would like to share my findings with others (and I’ve just checked this on the GM release and all works fine).

This function grabs a users timeline from a selected Twitter account in the ACAccountStore.

Start with a simple method where you intend all the magic to happen:

func GetTimeLine() {


Next, add the following constants (see comments for explanation):

  func GetTimeLine() {

// Create an instance of ACAccountStore
var accountStore = ACAccountStore()

// Constant username value
let screenName = "mrchrisbarker"

// Parameters dictionary for addtional options (not covered in this tutorial)
let params = Dictionary()

// Twitter API URL
let url = NSURL.URLWithString("\(screenName)&count=10")

Now we need to setup the “post" request by creating an instance of SLRequest as per below:

    func GetTimeLine() {

// Previous CODE……

// Create a new SLRequest
var postRequest: SLRequest = SLRequest(forServiceType: SLServiceTypeTwitter, requestMethod: SLRequestMethod.GET, URL: url, parameters: params)

// Create a new instance if ACAccount and choose an account from the account store (first in the list chosen for this tutorial).
let account: ACAccount = accountStore.accountsWithAccountType(setAccountType)[0] as ACAccount

// Assign the account to the postRequest
postRequest.account = account


Finally, we call the API via the request handler, in here we grab the response from Twitter and parse the JSON into an NSArray and then access the properties via NSDictionary

    func GetTimeLine() {

// Previous CODE……

postRequest.performRequestWithHandler({responseData, urlResponse, error in

// Variable to catch any errors thrown back by the Twitter API
var error: NSError? = NSError()

// Serialize JSON "responseData" into NSArray
let feedData:NSArray? = NSJSONSerialization.JSONObjectWithData(responseData, options: NSJSONReadingOptions.AllowFragments, error: &error) as? NSArray

// Itterate and parse each tweet into NSDictionary
for item: AnyObject in feedData! {

// Cast 'item' into NSDictionary object
var currentTweet:NSDictionary = item as NSDictionary

// Access the 'user' key withing the 'currentTweet' object
var currentUser:NSDictionary = currentTweet.objectForKey("user") as NSDictionary

// Access the 'name' & 'text' from the 'currentUser' object
var name = currentUser.objectForKey("name") as String
var tweet = currentTweet.objectForKey("text") as String

// Output result / add to object / add to UIListView
println("Name: \(name), Tweet: \(tweet)")




Boom - thats it’s, your done! Once you have your array you can add it to an object or directly into a UIListView / UICollection view.

Code example available:

Hope you’ve found this useful and as always, comments are very welcome.


*Update* - 20/09/14

You’ll need to make sure you have a Twitter ACAccount setup and give the app permission to access the account for this to work (see code below)

    func GetPermission(){

self.accountStore.requestAccessToAccountsWithType(setAccountType, options: nil) {
granted, error in

println("Permission Granted = \(granted)")

if granted {



*Update* - 05/12/14

Don’t forget to include your “import" statements at the top of your class;

import UIKit

import Accounts
import Twitter
import Social