Search code examples
iosswifttwittertweetstwitter-fabric

iOS: Get Latest Tweet Text Only From Specific User


I am wanting to get the latest tweet from a specific user that will not change. So if the specific user was Joe Smith, I would always get the latest tweet from him. I've looked into Fabric which seems to only get specific tweets by ID. The Twitter API looks fairly involved when all I am wanting is the latest tweet from a user. What is the best way to do this?


Solution

  • you could achive this by searching for a user and only using th first element

    Using from:@tweethandle for example: from:StackOverflow and using only the first item!

    You can search using TWTRAPIClient's URLRequestWithMethod

    Ex: (using SwiftyJSON for parsing JSON) this should print the first tweet

    let twError:NSErrorPointer = nil
    let userID = Twitter.sharedInstance().sessionStore.session()?.userID
    let client = TWTRAPIClient(userID: userID)
    let request = client.URLRequestWithMethod("GET", URL: "https://api.twitter.com/1.1/search/tweets.json", parameters: ["q":"from:StackOverflow","result_type":"recent"], error: twError)
    
    
    if twError == nil {
        client.sendTwitterRequest(request, completion: { (response, data, error) -> Void in
        let json = JSON(data: data!)
        print(json["statuses"][0])
        })
    }
    

    More info in the docs