访问Twitter帐户

时间:2013-01-11 03:27:11

标签: ios twitter

我使用以下代码在iOS 6中使用SLRequest和ACAccounts发布到Twitter。问题是,当有多个帐户时,我无法控制哪个帐户获得访问权限。允许用户选择要发布的帐户的建议?

-(void)twitteraccess {
    ACAccountStore *account = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:
                                  ACAccountTypeIdentifierTwitter];

    [account requestAccessToAccountsWithType:accountType options:nil
                                  completion:^(BOOL granted, NSError *error)
     {
         if (granted == YES)
         {
             NSArray *arrayOfAccounts = [account
                                         accountsWithAccountType:accountType];

             if ([arrayOfAccounts count] > 0)
             {
                 ACAccount *twitterAccount = [arrayOfAccounts lastObject];

                 NSDictionary *message = @{@"status": @"Message"};

                 NSURL *requestURL = [NSURL
                                      URLWithString:@"http://api.twitter.com/1/statuses/update.json"];

                 SLRequest *postRequest = [SLRequest
                                           requestForServiceType:SLServiceTypeTwitter
                                           requestMethod:SLRequestMethodPOST
                                           URL:requestURL parameters:message];

                 postRequest.account = twitterAccount;

                 [postRequest performRequestWithHandler:^(NSData *responseData,
                                                          NSHTTPURLResponse *urlResponse, NSError *error)
                  {
                      NSLog(@"Twitter HTTP response: %i", [urlResponse
                                                           statusCode]);
                  }];
             }
         }
         else {
             if(error.code == 6){
                 [self performSelectorOnMainThread:@selector(alertmessagetwitter)
                                        withObject:nil
                                     waitUntilDone:YES];                }
         }
     }];
}

0 个答案:

没有答案