Showing tweeter timeline but is empty

72 views Asked by At

I was practicing swift with social media and I was following a tutorial of How to add Twitter timeline to you App. Everything seems to be ok but the only problem is that the table that shows the tweets is completely empty. The code is the following...

import UIKit
import Social
import Accounts

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {


    @IBOutlet weak var tweetTableView: UITableView!

    var dataSource = [AnyObject]()


    func getTimeLine() {

        let account = ACAccountStore()
        let accountType = account.accountType(
        withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter)

        account.requestAccessToAccounts(with: accountType, options: nil, completion: {(success, error) in

            if success {
                let arrayOfAccounts =
                account.accounts(with: accountType)

                if (arrayOfAccounts?.count)! > 0 {
                    let twitterAccount = arrayOfAccounts?.last as! ACAccount

                    let requestURL = URL(string:
                    "https://api.twitter.com/1.1/statuses/user_timeline.json")

                    let parameters = ["screen_name" : "@techotopia",
                                  "include_rts" : "0",
                                  "trim_user" : "1",
                                  "count" : "20"]

                    let postRequest = SLRequest(forServiceType:
                    SLServiceTypeTwitter,
                                            requestMethod: SLRequestMethod.GET,
                                            url: requestURL,
                                            parameters: parameters)

                    postRequest?.account = twitterAccount

                    postRequest?.perform(handler: {(responseData, urlResponse, error) in

                        do {
                            try self.dataSource = JSONSerialization.jsonObject(with: responseData!, options: JSONSerialization.ReadingOptions.mutableLeaves) as! [AnyObject]

                            if self.dataSource.count != 0 {
                                DispatchQueue.main.async() {
                                    self.tweetTableView.reloadData()
                                }
                            }
                        } catch let error as NSError {
                        print("Data serialization error: \(error.localizedDescription)")
                        }
                    })
                }
            } else {
                print("Failed to access account")
            }
        })
    }




    override func viewDidLoad() {
        super.viewDidLoad()

        tweetTableView.register(UITableViewCell.self,
                            forCellReuseIdentifier: "Cell")
        getTimeLine()
        tweetTableView.estimatedRowHeight = 50

        }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }


    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell =
        tableView.dequeueReusableCell(withIdentifier: "Cell")
        let row = indexPath.row
        let tweet = self.dataSource[row]
        cell!.textLabel!.text = tweet.object(forKey: "text") as? String
        cell!.textLabel!.numberOfLines = 0
        return cell!

    }

}

The result is the following... enter image description here

I appreciate your answers! thank you!

0

There are 0 answers