2012-12-21 35 views
0

我有一个应用程序,我在一个表视图中的用户的时间表发展。这将是每个应用程序中的相同用户时间表。我已经编码,它完美的作品。我的问题是,当我在调用中将API版本从1更改为1.1时,它停止工作。所有的推文都会通过推特表,所以我不需要认证。我是否需要添加代码,而不仅仅是在调用中更改API版本,或者设置授权来显示新API的时间表?除了仅显示单个用户时间表和使用推特表进行回复外,我不添加任何功能。我附上我的代码。任何帮助都会很棒。谢谢。新的Twitter API集成到现有应用程序

- (void)fetchTweets 
{ 
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
     NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://api.twitter.com/1/statuses/user_timeline.json?include_rts=false&screen_name=johnnelm9r&count=100"]]; 

     if (data == nil) 
     { 

     } 

     else 
     { 
      NSError *error; 

      tweets = [NSJSONSerialization JSONObjectWithData:data 
                options:kNilOptions 
                 error:&error]; 
     } 

     dispatch_async(dispatch_get_main_queue(), ^{ 
      [self.twitterTableView reloadData]; 
     }); 

    }); 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(reachabilityChanged:) 
               name:kReachabilityChangedNotification 
               object:nil]; 
    self.reachability = [Reachability reachabilityWithHostName:@"www.apple.com"]; 
    [self.reachability startNotifier]; 
} 

回答

0

这就是我最终想出来的。对于我需要做的事情来说,这很好。希望它可以帮助别人。

- (void)fetchTweets 
{ 
twitterLoader.animationImages = [NSArray arrayWithObjects: 
           [UIImage imageNamed:@"StatusIndicator.png"], 
           [UIImage imageNamed:@"StatusIndicator1.png"], 
           [UIImage imageNamed:@"StatusIndicator2.png"], 
           [UIImage imageNamed:@"StatusIndicator3.png"], 
           [UIImage imageNamed:@"StatusIndicator4.png"], 
           [UIImage imageNamed:@"StatusIndicator5.png"], 
           [UIImage imageNamed:@"StatusIndicator6.png"], 
           [UIImage imageNamed:@"StatusIndicator7.png"], 
           [UIImage imageNamed:@"StatusIndicator8.png"], 
           [UIImage imageNamed:@"StatusIndicator9.png"], 
           [UIImage imageNamed:@"StatusIndicator10.png"], 
           [UIImage imageNamed:@"StatusIndicator9.png"], 
           [UIImage imageNamed:@"StatusIndicator8.png"], 
           [UIImage imageNamed:@"StatusIndicator7.png"], 
           [UIImage imageNamed:@"StatusIndicator6.png"], 
           [UIImage imageNamed:@"StatusIndicator5.png"], 
           [UIImage imageNamed:@"StatusIndicator4.png"], 
           [UIImage imageNamed:@"StatusIndicator3.png"], 
           [UIImage imageNamed:@"StatusIndicator2.png"], 
           [UIImage imageNamed:@"StatusIndicator1.png"], 
           nil]; 

twitterLoader.animationDuration = 0.8; 
[twitterLoader startAnimating]; 

ACAccountStore *store = [[ACAccountStore alloc] init]; 
ACAccountType *twitterAccountType = 
[store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter]; 

    // Request permission from the user to access the available Twitter accounts 
[store requestAccessToAccountsWithType:twitterAccountType 
           options:nil 
          completion:^(BOOL granted, NSError *error) 
{ 
if (!granted) 
    { 
    NSLog(@"User rejected access to the account."); 
    } 

else 
    { 
    [twitterLoader startAnimating]; 

    NSArray *twitterAccounts = 
    [store accountsWithAccountType:twitterAccountType]; 

    if ([twitterAccounts count] > 0) 
     { 
     ACAccount *account = [twitterAccounts objectAtIndex:0]; 
     [twitterLoader startAnimating]; 

     NSMutableDictionary *params = [[NSMutableDictionary alloc] init]; 
     [params setObject:@"johnnelm9r" forKey:@"screen_name"]; 
     [params setObject:@"100" forKey:@"count"]; 
     [params setObject:@"0" forKey:@"include_entities"]; 
     [params setObject:@"0" forKey:@"include_rts"]; 

      // The endpoint that we wish to call 
     NSURL *url = 
     [NSURL 
      URLWithString:@"http://api.twitter.com/1.1/statuses/user_timeline.json"]; 

     SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter 
               requestMethod:SLRequestMethodGET 
                  URL:url 
                parameters:params]; 


      // Attach the account object to this request 
     [request setAccount:account]; 

     [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) 
      { 
      if (!responseData) 
       { 
       NSLog(@"%@", error); 
       } 

      else 
       { 
       NSError *jsonError; 
       tweets = [NSJSONSerialization JSONObjectWithData:responseData 
                 options:NSJSONReadingMutableLeaves 
                 error:&jsonError]; 
       if (tweets) 
        { 
        NSLog(@"%@", tweets); 
        [self.twitterTableView reloadData]; 

        [twitterLoader stopAnimating]; 
        } 

       else 
        { 
        NSLog(@"%@", jsonError); 
        } 
       } 
      }]; 
     [self.twitterTableView reloadData]; 
     [twitterLoader stopAnimating]; 
     } 
    } 
}]; 

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(reachabilityChanged:) 
              name:kReachabilityChangedNotification 
              object:nil]; 
self.reachability = [Reachability reachabilityWithHostName:@"www.apple.com"]; 
[self.reachability startNotifier]; 
} 

我还添加了一个简单的装载机和可达性。如果你不需要它,只需删除底部的NSNotification。

,并确保添加

帐户和社会框架

和他们的头文件导入到.m文件,你使用它英寸

祝你好运!

相关问题