【问题标题】:How to use iOS TWRequest for iOS 5 for getting Twitter User Details如何使用 iOS TWRequest for iOS 5 获取 Twitter 用户详细信息
【发布时间】:2013-07-31 14:59:47
【问题描述】:

如何在 iOS 5 中使用 TWRequest 来获取 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 可能会被弃用......

我在 iOS 6 上使用 SLRequest,它运行良好,但我想知道如何在 iOS 5 中获取 Twitter 用户详细信息

【问题讨论】:

    标签: iphone ios twitter twrequest


    【解决方案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];
                                 }                             
                             });
                         }];
                     });
                 }
             }];
        }
    }
    

    【讨论】:

    • 谢谢毛利克!但有时它会在这个地方崩溃,可能是线程问题.. [request setAccount:[twitterAccounts lastObject]];我们可以解决这个问题吗?
    • 你的崩溃日志是什么?
    • 我在 ios 5 中也遇到了同样的问题,它给出了 EXC_BAD_ACCESS,
    • 我有解决方案..!您必须保留 ACAccountStore:@property (nonatomic, strong) ACAccountStore *accountStore;
    猜你喜欢
    • 2016-04-17
    • 1970-01-01
    • 2015-10-01
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    • 2014-09-06
    相关资源
    最近更新 更多