4

iOS アプリに Twitter タイムラインを追加しようとしています。Twitter は公式のサンプル コードを提供しています: https://dev.twitter.com/docs/ios/making-api-requests-slrequest
私の問題は: 70 行目です。

if (timelineData) {
    NSLog(@"Timeline Response: %@\n", timelineData);
}

タイムライン データがコンソールに正常に出力されます。追加してみました

self.dict = timelineDate;

return self.dict;

関数の最後にありますが、実際には空の辞書を返します。処理が別スレッドで行われていることに気がついたので、やってみました

dispatch_async(dispatch_get_main_queue(), ^{
    self.dict = timelineDate;
};

しかし、それでも機能しません。これは非常に簡単に解決できるかもしれませんが、Apple からも Twitter からもリソースがまったく見つかりません。誰でも助けることができますか?

4

3 に答える 3

2

この関数を使用してアプリにツイートをロードします (twitter api v1.1 と互換性がありますが、デバイスで同期するには twitter アカウントが必要です)。

//include twitter.framework 
#import <Twitter/Twitter.h>

+ (void)getTweetsFortwitterID:(NSString *)twitterID

{
    if(twitterID.length >0)
    {
    NSString * finalURL = [NSString stringWithFormat:@"https://api.twitter.com/1.1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name=%@&count=10", twitterID];

    TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:finalURL] parameters:nil requestMethod:TWRequestMethodGET];

    ACAccountStore *accountStore = [[ACAccountStore alloc] init] ;

    ACAccountType *accountType =  [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

        // Request access from the user to use their Twitter accounts.
        [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
         {
             if(granted)
             {
                 NSArray *twitterAccounts = [accountStore accountsWithAccountType:accountType];

                 if([twitterAccounts count] >0
                    )
                 {
                 ACAccount *twitterAccount = [twitterAccounts objectAtIndex:0];
                 [postRequest setAccount:twitterAccount];

                 NSLog(@"request.account:%@",postRequest.account);

                 // Perform the request created above and create a handler block to handle the response.
                 NSMutableArray *tweetsArray=[[NSMutableArray alloc]init];

                 [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {

                     // Parse the responseData, which we asked to be in JSON format for this request, into an NSDictionary using NSJSONSerialization.
                     NSArray *publicTimeline = nil;
                     NSError *jsonParsingError = nil;
                     if (responseData)
                     {
                         publicTimeline = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];
                         NSLog(@"publicTimeline : %@", publicTimeline);
                     }

                     if ([publicTimeline isKindOfClass:[NSArray class]])
                     {

                         for (int i =0; i<[publicTimeline count]; i++)
                         {
                             NSMutableDictionary *twitterDict=[[NSMutableDictionary alloc]init];

                             if ([[publicTimeline objectAtIndex:i] objectForKey:@"text"])
                             {
                                 NSLog(@"ID: %@", [[publicTimeline objectAtIndex:i] objectForKey:@"text"]);
                                 [twitterDict setObject:[[publicTimeline objectAtIndex:i] objectForKey:@"text"] forKey:@"text"];
                             }
                             if ([[publicTimeline objectAtIndex:i] objectForKey:@"created_at"])
                             {
                                 NSLog(@"ID: %@", [[publicTimeline objectAtIndex:i] objectForKey:@"created_at"]);
                                 [twitterDict setObject:[[publicTimeline objectAtIndex:i] objectForKey:@"created_at"]
                                                 forKey:@"created_at"];
                             }

                             if ([[publicTimeline objectAtIndex:i] objectForKey:@"user"])
                             {
                                 NSLog(@"ID: %@", [[publicTimeline objectAtIndex:i] objectForKey:@"created_at"]);
                                 [twitterDict setObject:[[[publicTimeline objectAtIndex:i] objectForKey:@"user"]objectForKey:@"profile_image_url"]
                                                 forKey:@"profile_image_url"];
                             }


                             [tweetsArray addObject:twitterDict];
                             NSLog(@"tweets:%@", tweetsArray);

                         }
                     }

                     if([tweetsArray count]>0)
                         [[NSNotificationCenter defaultCenter] postNotificationName:@"tweetsLoaded" object:tweetsArray];


                 }];
                 }

             }

         }];
    }


}

それが役に立つことを願っています。

于 2013-04-19T12:47:15.410 に答える