Как использовать iOS TWRequest для iOS 5 для получения сведений о пользователе Twitter

Как использовать TWRequest для iOS 5 для получения сведений о пользователе Twitter,

TWRequest работал раньше, и я использовал таким образом

 NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1/users/show.json"];
 NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:twittername,@"screen_name",nil];
                                 request = [[TWRequest alloc] initWithURL:url
                                                                      parameters:params
                                                                   requestMethod:TWRequestMethodGET];

Но недавно Twitter закрыл API версии 1 и внедрил версию 1.1, и в настоящее время вышеприведенная логика не работает в iOS 5, из-за устаревания API может быть...

Я использую SLRequest для iOS 6, он работает отлично, но я хотел бы знать, как мы можем получить данные пользователя Twitter в iOS 5.


person Ganesh    schedule 31.07.2013    source источник


Ответы (1)


Попробуй это :

   NSURL *url = [NSURL URLWithString:@"https://api.twitter.com/1.1/users/show.json"];
   NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:twittername,@"screen_name",nil];
   request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET];                                                                 

    ACAccountStore *accountStore = [[ACAccountStore alloc] init];
    ACAccountType *twitterAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    NSArray *twitterAccounts = [accountStore accountsWithAccountType:twitterAccountType];

    // Runing on iOS 6
    if (NSClassFromString(@"SLComposeViewController") && [SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        [accountStore requestAccessToAccountsWithType:twitterAccountType options:NULL completion:^(BOOL granted, NSError *error)
         {
             if (granted)
             {
                 SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodGET URL:url                                      parameters:parameters];

                 [request setAccount:[twitterAccounts lastObject]];

                 dispatch_async(dispatch_get_main_queue(), ^                                
                 {

                     [NSURLConnection sendAsynchronousRequest:request.preparedURLRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response1, NSData *data, NSError *error)                      
                     {                         
                         dispatch_async(dispatch_get_main_queue(), ^                                        
                         {                             
                             if (data)                                 
                             {                                 
                                 [self loadData:data];                                 
                             }                             
                         });                                         
                     }];                     
                 });
             }
         }];
    }
    else if (NSClassFromString(@"TWTweetComposeViewController") && [TWTweetComposeViewController canSendTweet]) // Runing on iOS 5
    {
        [accountStore requestAccessToAccountsWithType:twitterAccountType withCompletionHandler:^(BOOL granted, NSError *error)
         {
             if (granted)
             {
                 TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:parameters requestMethod:TWRequestMethodGET];
                 [request setAccount:[twitterAccounts lastObject]];

                 dispatch_async(dispatch_get_main_queue(), ^                                
                 {                     
                     [NSURLConnection sendAsynchronousRequest:request.signedURLRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response1, NSData *data, NSError *error)                      
                     {                         
                         dispatch_async(dispatch_get_main_queue(), ^                                        
                         {                             
                             if (data)                                 
                             {                                 
                                 [self loadData:data];
                             }                             
                         });
                     }];
                 });
             }
         }];
    }
}
person Maulik    schedule 31.07.2013
comment
Спасибо Маулик! Но иногда происходит сбой в этом месте, может быть проблема с потоками.. [request setAccount:[twitterAccounts lastObject]]; мы можем решить это? - person Ganesh; 31.07.2013
comment
какой у тебя краш-лог? - person Maulik; 31.07.2013
comment
я также получаю ту же проблему в ios 5, это дает EXC_BAD_ACCESS, - person Toseef Khilji; 27.08.2013
comment
я получил решение ..! Вы должны сохранить ACAccountStore: @property (nonatomic, strong) ACAccountStore *accountStore; - person Toseef Khilji; 28.08.2013