我有一个应用程序要求用户登录,感谢在那里找到的 Twitter 文档:使用 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);
}