Embedded single user timeline in iOS


#1

Hi
I am new to both Swift development and Fabric so please bear with me!
I am trying to use Twitter Kit in Fabric to display an embedded single user’s timeline within a simple table view in my app. Login to Twitter will be by guest view only to begin. I appreciate there is plenty of documentation on this online but I can’t seem to piece the various bits of code together to make things work.
I have tried within Xcode to implement the guest login method and then the display in table view cells example shown in the documentation without success. The best I have achieved is to get 2 cells to populate based on the example with grey image placeholders and the Twitter logo but no tweet.
I also don’t understand the differences between a Twitter user handle and their Twitter ID?
Would someone kindly paste an example code of how the swift file for such a request should look or point me in the right direction for more help.
Many thanks


#2

Hey benthomas1982,

There is a pretty thorough Swift example on how to display Tweets with TwitterKit here: https://dev.twitter.com/twitter-kit/ios/show-tweets. Look for the section “Show a UITableView of Tweets”. Let us know if you need further help on this.

As for a user handle and their ID, both are simply identifiers for a Twitter User. As you might suspect, the handle is often easier to refer to because the ID is some very long 64-bit integer. The difference lies that a Twitter User can change their handle at any time to another that isn’t already taken. The ID is fixed after the user creates their account.


#3

Hi Kang
Thank you so much for your help. I have tracked down the user ID I require but for some reason I am still unable to populate the table view using the sample code provided in the Twitter Kit documentation (I just get an empty table view like the cell is not connected). I have tried to access Twitter as a guest only in my code and then simply embed the timeline of a single user. I am very sorry for pasting large amounts of code but am unable to explain my problem properly without doing so. Any advice would be greatly appreciated. Thanks again.

**Edited code below as per my last comment on this post

import UIKit
import TwitterKit

class TwitterViewController: UITableViewController, TWTRTweetViewDelegate {
    
    
    let tweetTableReuseIdentifier = "TweetCell"
    // Hold all the loaded Tweets
    var tweets: [TWTRTweet] = [] {
        didSet {
            tableView.reloadData()
        }
    }
    let tweetIDs = [
        "184701590"] // our favorite bike Tweet
    
    override func viewDidLoad() {
    
        
        Twitter.sharedInstance().logInGuestWithCompletion { guestSession, error in
            if (guestSession != nil) {
                Twitter.sharedInstance().APIClient.loadTweetsWithIDs(self.tweetIDs) { tweets, error in
                    if let ts = tweets as? [TWTRTweet] {
                        self.tweets = ts
                    } else {
                        println("Failed to load tweets: \(error.localizedDescription)")
                    }
                }
            }
        }
        // Setup the table view
        tableView.estimatedRowHeight = 150
        tableView.rowHeight = UITableViewAutomaticDimension // Explicitly set on iOS 8 if using automatic row height calculation
        tableView.allowsSelection = false
        tableView.registerClass(TWTRTweetTableViewCell.self, forCellReuseIdentifier: tweetTableReuseIdentifier)
        
                }
    
    // MARK: UITableViewDelegate Methods
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.tweets.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let tweet = tweets[indexPath.row]
        let cell = tableView.dequeueReusableCellWithIdentifier(tweetTableReuseIdentifier, forIndexPath: indexPath) as TWTRTweetTableViewCell
        cell.tweetView.delegate = self
        cell.configureWithTweet(tweet)
        return cell
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        let tweet = tweets[indexPath.row]
        return TWTRTweetTableViewCell.heightForTweet(tweet, width: CGRectGetWidth(self.view.bounds))
    }

}


#4

I think the problem lies in that loadTweetsWithIDs is called outside of the logInGuestWithCompletion, which means you might not have a valid session before you call the API and will get unauthenticated errors

Try doing this instead:

Twitter.sharedInstance().logInGuestWithCompletion { guestSession, error in
  if (guestSession != nil) {
    Twitter.sharedInstance().APIClient.loadTweetsWithIDs(tweetIDs) { tweets, error in
      if let ts = tweets as? [TWTRTweet] {
        self.tweets = ts
      } else {
        println("Failed to load tweets: \(error.localizedDescription)")
      }
    }
  } else {
    println("error: \(error.localizedDescription)");
  }
}

#5

Thanks Kang. I have amended as above in the edited code. The API call is now within the logInGuestWithCompletion method. I do not get any authorisation or other errors in the console but the tableview is still unpopulated. I simply have blank cells. Note I added cell.configureWithTweet(tweet) in the cellForRowAtIndexPath method also which someone else advised me was needed. Any ideas please…this is driving me crazy !


#6

Nearly there! It works if I use the ids of particular tweets as opposed to the twitter id for a user. How do I amend the syntax to load a timeline based on a single user please? loadUserWithId doesn’t seem to work? Thanks


#7

Hi Ben, did you ever get this figured out (in Swift)? I am still trying to find out how to get this working. Any chance you could share the source code?


#8

I am having the same issue here. Can anyone help please? Any help will be very much appreciated. I get empty cells when i use my twitter ID. If that doesn’t work, can someone point us in the right direction? Thanks


#9