Twitter在异步调用中共享错误

时间:2014-01-20 06:19:08

标签: ios objective-c twitter

我正在尝试在Twitter上分享推文,我正在使用异步调用。这是我正在使用的代码,我认为它安静通用且简单。

 ACAccountType *twitterType = [self.accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    NSDictionary *message = @{@"status": tweetString};
    NSURL *requestURL = [NSURL URLWithString:@"http://api.twitter.com/1.1/statuses/update.json"];
    SLRequest *postRequest = [SLRequest
                              requestForServiceType:SLServiceTypeTwitter
                              requestMethod:SLRequestMethodPOST
                              URL:requestURL parameters:message];
    NSArray *accounts = [self.accountStore accountsWithAccountType:twitterType];
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    for (int i = 0; i<[accounts count]; i++) {
        ACAccount *currentAccount = [accounts objectAtIndex:i];
        if ([currentAccount.username isEqualToString:[delegate.twitterAccount username]]) {
            postRequest.account = currentAccount;
            break;
        }
    }

    [postRequest performRequestWithHandler:^(NSData *responseData,
                                             NSHTTPURLResponse *urlResponse, NSError *error)
     {
         NSLog(@"URL Response: %@",statusCode);
         if ([urlResponse statusCode] == 200) {
             [self.delegate twitterPostCompleted:YES];
         } else {
             [self.delegate twitterPostCompleted:NO];
         }
     }];

我不知道为什么,但我甚至无法打印urlResponse。我试图调试此代码,但它永远不会到达行:NSLog(@"URL Response: %@",statusCode);

有什么建议吗?

编辑: twitterPostCompleted是我打印最终输出的方法。我总是收到错误消息,这意味着我没有收到codeStatus = 200

2 个答案:

答案 0 :(得分:2)

除了一个,我的代码中的一切都很好。我正在使用

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

我想要使用这个:

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

刚刚用http替换了https,它运行正常。

答案 1 :(得分:1)

首先,您需要询问用户权限,然后才能继续执行以下操作


    ACAccountStore *account = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [account    accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    // NSDictionary *message = @{@"status": tweetString};//i am using dictionary directly 

    // Request access from the user to access their Twitter account

   [account requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error) {
    {
        if (granted == YES)
        {
          //proceed further 

           NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
            if ([arrayOfAccounts count] > 0)
            {
                //use the first account available
                if(selectedIndex == -1) selectedIndex = 0;
                NSLog(@"selected Account at index -> %d",selectedIndex);
                ACAccount *acct = [arrayOfAccounts objectAtIndex:selectedIndex]; //hear u can get the selected account in your case from app delegate.

              //Build a twitter request
                 SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] parameters:[NSDictionary dictionaryWithObject:message forKey:@"status"]];

             //set account for post 
                [postRequest setAccount:acct]; 

            //manage the response
                [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
                 {
                     if(error)
                     {
                         NSLog(@"error");
                         NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]);
                     }
                     else
                     {
                         // on successful posting the tweet
                       NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]);
                     }

                    }];
                 }
            else
            {
              NSLog(@"else part of account less than zero");
            }
        }
        else
        {
           NSLog(@"user does not allowed to access account");
        }

    }]; //completion of block