在iOS中使用Twitter API进行简单的GET请求

时间:2022-04-01 07:09:02

Does anyone know how to do a simple GET request of a user's timeline using the Twitter API in iOS?
https://dev.twitter.com/rest/reference/get/statuses/user_timeline

有没有人知道如何使用iOS中的Twitter API对用户的时间线进行简单的GET请求? https://dev.twitter.com/rest/reference/get/statuses/user_timeline

I realize there are multiple frameworks out there but this is all I need and would prefer not to use them just for this. I also don't want to use the Social framework on iOS because it requires the user to have a Twitter account.

我意识到有多个框架,但这就是我所需要的,并且不想仅仅为此使用它们。我也不想在iOS上使用社交框架,因为它要求用户拥有Twitter帐户。

I've implemented this in PHP from this post here but not sure how to do the equivalent thing in iOS. https://*.com/a/12939923/284714

我已经在这篇文章中用PHP实现了这个,但不知道如何在iOS中做同样的事情。 https://*.com/a/12939923/284714

2 个解决方案

#1


2  

Here's how you do it with NSURLSession, which is the current, modern, "future proof" (lol) way of doing it on iOS/macOS:

以下是使用NSURLSession的方法,这是当前的,现代的,“未来证明”(lol)在iOS / macOS上的方式:

NSURLSession *urlSession;
NSURLSessionDataTask *dataTask;
NSURL *twitterURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json"];

urlSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
dataTask = [urlSession
            dataTaskWithURL:twitterURL
            completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

                // add error checking here
                NSLog(@"Got %lu bytes of data from twitter", (unsigned long)[data length]);

            }];
[dataTask resume];

And if swift is your cup of tea:

如果你的茶是快速的:

let defaultSession = URLSession(configuration: URLSessionConfiguration.default)
var dataTask: URLSessionDataTask?
let url: URL! = URL(string: "https://api.twitter.com/1.1/statuses/user_timeline.json");
dataTask = defaultSession.dataTask(with: url) {
    (data, response, error) in
    // check errors and stuff.
    print(data)
}
dataTask?.resume()

#2


0  

One quick way to do this on iOS would be to use the ACAccountStore class to get one of the user's authenticated Twitter accounts, then perform a request using SLRequest. SLRequest has an account property you'd use to include the account.

在iOS上执行此操作的一种快捷方法是使用ACAccountStore类获取用户经过身份验证的Twitter帐户之一,然后使用SLRequest执行请求。 SLRequest具有您用于包含该帐户的帐户属性。

#1


2  

Here's how you do it with NSURLSession, which is the current, modern, "future proof" (lol) way of doing it on iOS/macOS:

以下是使用NSURLSession的方法,这是当前的,现代的,“未来证明”(lol)在iOS / macOS上的方式:

NSURLSession *urlSession;
NSURLSessionDataTask *dataTask;
NSURL *twitterURL = [NSURL URLWithString:@"https://api.twitter.com/1.1/statuses/user_timeline.json"];

urlSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
dataTask = [urlSession
            dataTaskWithURL:twitterURL
            completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

                // add error checking here
                NSLog(@"Got %lu bytes of data from twitter", (unsigned long)[data length]);

            }];
[dataTask resume];

And if swift is your cup of tea:

如果你的茶是快速的:

let defaultSession = URLSession(configuration: URLSessionConfiguration.default)
var dataTask: URLSessionDataTask?
let url: URL! = URL(string: "https://api.twitter.com/1.1/statuses/user_timeline.json");
dataTask = defaultSession.dataTask(with: url) {
    (data, response, error) in
    // check errors and stuff.
    print(data)
}
dataTask?.resume()

#2


0  

One quick way to do this on iOS would be to use the ACAccountStore class to get one of the user's authenticated Twitter accounts, then perform a request using SLRequest. SLRequest has an account property you'd use to include the account.

在iOS上执行此操作的一种快捷方法是使用ACAccountStore类获取用户经过身份验证的Twitter帐户之一,然后使用SLRequest执行请求。 SLRequest具有您用于包含该帐户的帐户属性。