Load twitter posts from different users

早过忘川 提交于 2020-07-22 21:52:10

问题


Hi need to load twitter posts from different users. I know how to load post from single user. Perform request with search query FROM:<username>. Do you know the way how to load posts from different users? I tried something like this FROM:<username1>&&<username2> - but it doesn't work. Any ideas?


回答1:


You can make use of TwitterKit framework which is available in cocoapods GitHub - https://github.com/twitter/twitter-kit-ios/wiki/Access-Twitter's-REST-API Cocoapods - https://cocoapods.org/pods/TwitterKit Its twitter's own library, so you can always rely on that. To load multiple tweets you need to use the below code

let client = TWTRAPIClient()
let tweetIDs = ["510908888888487103", "510908133777777104"]
client.loadTweets(withIDs: tweetIDs) { (tweets, error) -> Void in
  // handle the response or error
}

If you want to to access other Twitter API endpoints, you can construct a request manually also like below

let client = TWTRAPIClient()
let statusesShowEndpoint = "https://api.twitter.com/1.1/statuses/show.json"
let params = ["id": "20"]
var clientError : NSError?

let request = client.urlRequest(withMethod: "GET", url: statusesShowEndpoint, parameters: params, error: &clientError)

client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
    if connectionError != nil {
        print("Error: \(connectionError)")
    }

    do {
        let json = try JSONSerialization.jsonObject(with: data!, options: [])
        print("json: \(json)")
    } catch let jsonError as NSError {
        print("json error: \(jsonError.localizedDescription)")
    }
}

Hope this one helps.




回答2:


I found an approach. I have to use Twitter iOS SDK and TWTRAPIClient for performing the request. https://api.twitter.com/1.1/statuses/user_timeline.json is endpoint for getting tweets of certain user with some username.

  NSURLRequest *r = [[TWTRAPIClient new] URLRequestWithMethod:@"GET"
                                                                URL:@"https://api.twitter.com/1.1/statuses/user_timeline.json"
                                                         parameters:@{@"screen_name":name}
                                                              error:nil];


        [[TWTRAPIClient new] sendTwitterRequest:r completion:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
            if(data){

                NSArray *tweets = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
                [TWTRTweet tweetsWithJSONArray:tweets]


            }
        }];


来源:https://stackoverflow.com/questions/50519377/load-twitter-posts-from-different-users

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!