Marcos Griselli
Marcos Griselli

Reputation: 1346

Retrieve Twitter user images and names in swift using Swifter Framework

I'm using Swifter Framework to get tweets using a specific hashtag, but I also need the user image and name.

With this method I'm able to get the tweet text and I can read from the JSON response the profile image and username but I'm not able to get it into a variable.

PS: Don't mind about the for index 0...19 its just testing

        let swifter = Swifter(consumerKey: "MyConsumerKey", consumerSecret: "MyConsumerSecret", appOnly: true)

        swifter.authorizeAppOnlyWithSuccess({ (accessToken, response) -> Void in
        swifter.getSearchTweetsWithQuery("%23realmadrid", geocode: nil, lang: nil, locale: nil, resultType: nil, count: 20, until: nil, sinceID: nil, maxID: nil, includeEntities: true, callback: nil, success: { (statuses, searchMetadata) -> Void in


            for index in 0...19 {
                if let statusText = statuses?[index]["text"].string {
                    self.tweetsArray.addObject(statusText)
                }
                if let statusName = statuses?[index]["screen_name"].string {
                    println("@%@", statusName)
                }
                if let statusImage = statuses?[index]["profile_image_url"].string {
                    println("@%@", statusImage)
                }

            }

            self.tableView.reloadData()


        }, failure: { (error) -> Void in
        })
    }, failure: { (error) -> Void in
        println("error")
    })

Upvotes: 1

Views: 1056

Answers (1)

Marcos Griselli
Marcos Griselli

Reputation: 1346

To anyone who finds the same problem:

  if let statusName = statuses?[index]["user"]["screen_name"].string {
                    tweetsDictionary.setObject(statusName, forKey: "name")
                }

  if let statusImage = statuses?[index]["user"]["profile_image_url"].string {
                    var string = statusImage.stringByReplacingOccurrencesOfString("_normal", withString: "", options: NSStringCompareOptions.BackwardsSearch, range: nil)
                    tweetsDictionary.setObject(string, forKey: "image")
                }

This retrieves twitter username and profile image in the quality the user uploaded it.

Upvotes: 3

Related Questions