1

下午好,

我有一个应用程序要求用户登录,这要归功于在那里找到的 twitter 文档:使用 twitter 登录。然后,我就可以成功加载用户的时间线了。此时间线加载到具有接口TWTRTimelineViewController的NavigationController中。现在,我希望当一个用户将手指放在一条推文上,或者只是点击一条推文,而不是打开 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

任何帮助都是极好的!

干杯,西奥。

4

1 回答 1

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);
    }
于 2017-11-09T15:39:59.317 回答