2017-11-07 15 views
1

안녕하세요,Retreive 트윗 정보 TWTRTimelineViewController의 타임 라인에서 (텍스트 및 이미지) - 목적 C

나는 그곳에서 발견 트위터의 문서 덕분에 로그인하도록 요청 응용 프로그램이 있습니다 Log in with twitter. 그런 다음 사용자의 타임 라인을로드 할 수 있습니다. 이 타임 라인은 TWTRTimelineViewController 인터페이스를 포함하는 인 로드됩니다. 이제는 한 사용자가 트위터에 손가락을 올리거나 사파리를 열지 않고 단지 하나의 트윗을 클릭하면 클릭 한 후에 트윗에서 작업 할 수있는 버튼이 팝업됩니다. 나는 트윗의 텍스트와 이미지에 접근 할 필요가있을 것이다. 필자는 모든 트윗을 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 

어떤 도움이 될 것입니다!

건배, Theo.

답변

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); 
    } 
+0

게스트 인증은 어떻게 할 수 있습니까? – Balasubramanian