从TWTRTimelineViewController中的时间轴中读取推文信息(文本和图像) - 目标c

时间:2017-11-07 12:36:56

标签: ios objective-c api twitter twitterkit

下午好,

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

#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

任何帮助都会很棒!

干杯, 西奥。

1 个答案:

答案 0 :(得分:0)

自己找到答案。对于那些在同一个问题上苦苦挣扎的人,这里是我写的代码。有点乱,但它有效。

-(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);
    }