从 TWTRTimeline ViewController 中的时间线检索推文信息(文本图像)- Objective c

Retreive tweet information (text&image) from timeline in TWTRTimelineViewController - Objective c

下午好,

我有一个要求用户登录的应用程序,感谢在那里找到的 Twitter 文档:Log in with twitter。然后,我可以成功加载用户的时间线。此时间线加载在 NavigationController 中,它具有接口 TWTRTimelineViewController。现在,我希望当一个用户将他的手指放在一条推文上,或者只是点击一条推文时,而不是打开 Safari 来显示它,它会弹出一个按钮,我可以在点击后处理推文。我将需要访问推文的文本和图像。 根据我的理解,我需要将所有推文委托给某种 TWTRTweetView 控制器来处理它们,但我不太确定如何处理,因为我对此完全陌生。我确实尝试阅读文档但无法真正理解它,并且大部分示例都是用 Swift 编写的。我也不确定我应该如何访问推文的属性。我试过 STTwitter,在那里我玩了一些 JSON 格式的文本,我在哪里能够得到文本和图像 URL 但我无法弄清楚如何直接使用 TwitterKit 执行此操作。这是我显示时间线的控制器的实际代码:

#import "TwitterTimelineViewController.h"
#import <TwitterKit/TwitterKit.h>
#import "AppDelegate.h"

@interface TwitterTimelineViewController ()
@property (strong, nonatomic) IBOutlet UITableView *TwitterTableView;
@end

@implementation TwitterTimelineViewController

TWTRUserTimelineDataSource *userTimelineDataSource;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    AppDelegate *delegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    [[Twitter sharedInstance] startWithConsumerKey:@"myConsumerKey" consumerSecret:@"myConsumerSecretKey"];
    
    TWTRAPIClient *APIClient = [[TWTRAPIClient alloc] init];
    userTimelineDataSource = [[TWTRUserTimelineDataSource alloc] initWithScreenName:delegate.twitterUsername APIClient:APIClient];
    self.dataSource = userTimelineDataSource;
}

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options {
    return [[Twitter sharedInstance] application:app openURL:url options:options];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

任何帮助都会很棒!

干杯, 西奥.

自己找到了答案。对于遇到同样问题的任何人,这里是我编写的代码。有点乱,但很管用。

-(IBAction)ShowTweets: (id) sender{

UIButton *clicked = (UIButton *) sender;

NSString *tweetToDecryptIndex = [NSString stringWithFormat: @"%ld", (long)clicked.tag];

//gets all tweets from current timeline
NSArray *allTweets = self.snapshotTweets;

//look the tweets, get the URL and removes it to get the text only
NSDataDetector *detect = [[NSDataDetector alloc] initWithTypes:NSTextCheckingTypeLink error:nil];

    //gets the single tweet from clicked button
    TWTRTweet *tweet = [allTweets objectAtIndex:(long)clicked.tag];
    NSString *content = tweet.text; //gets the text
    NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil];
    NSArray *matches = [linkDetector matchesInString:content options:0 range:NSMakeRange(0, [content length])]; //find the URL

    NSURL *url; //contains the url from the text of the tweet
    NSString *ciph; //text from tweet without the url

    for (NSTextCheckingResult *match in matches) {
        if ([match resultType] == NSTextCheckingTypeLink) {
            url = [match URL];
            ciph = [content substringToIndex:content.length-url.absoluteString.length];
        }
    }

    //Now, ask a JSON answer from twitter of the specific tweet using its ID
    TWTRAPIClient *client = [[TWTRAPIClient alloc] init];
    NSString *statusesShowEndpoint = @"https://api.twitter.com/1.1/statuses/show.json";
    NSDictionary *params = @{@"id" : tweet.tweetID};
    NSError *clientError;

    NSURLRequest *request = [client URLRequestWithMethod:@"GET" URL:statusesShowEndpoint parameters:params error:&clientError];

    if (request) {
        [client sendTwitterRequest:request completion:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            if (data) {
                NSError *jsonError;
                NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&jsonError];

                //NSLog(@"%@", json);
                //looking for the media_url
                NSString *media = [json valueForKeyPath:@"entities.media"];
                NSArray *urlArray = [media valueForKey:@"media_url_https"];

                //finally getting the url as a string
                NSMutableString * urlOfImageString = [[NSMutableString alloc] init];
                for (NSObject * obj in urlArray)
                {
                    [urlOfImageString appendString:[obj description]];
                }

                //NSLog(@"%@",urlOfImageString);

                //constructing name for image to write in path
                NSString *tweetID = tweet.tweetID;
                NSString *imgName = [tweetID stringByAppendingString:@".jpg"];
                NSString *tmp = [@"/your/path" stringByAppendingString:imgName];

                //NSLog(@"%@", tmp);

                //Now writting the image
                NSURL *urlOfImageUrl = [NSURL URLWithString:urlOfImageString];
                NSData *imageData = [NSData dataWithContentsOfURL:urlOfImageUrl];   
            }
            else {
                NSLog(@"Error: %@", connectionError);
            }
        }];
    }
    else {
        NSLog(@"Error: %@", clientError);
    }