简体   繁体   中英

iOS : Post on twitter without open dialogue using twitter.framework

I am trying to posting a tweet on twitter using iOS twitter.framework , somehow its not showing me any error when post request gets done, however its not posting anything on twitter!

Here's my code for

-(void)twitPost
{
    ACAccountStore *account = [[ACAccountStore alloc] init];
    ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
    NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
    if (!arrayOfAccounts)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"No Twitter Account" message:@"There are no Twitter accounts configured. You can add or create a Twitter account in the main Settings section of your phone device." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        [alert release];
        return;
    }

    // Request access from the user to access their Twitter account
    [account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
     {
         // Did user allow us access?
         if (granted == YES)
         {                 
             // Populate array with all available Twitter accounts
             NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];

             if ([arrayOfAccounts count] > 0)
             {
                 // Keep it simple, use the first account available
                 ACAccount *acct = [arrayOfAccounts objectAtIndex:0];

                 TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"https://upload.twitter.com/1/statuses/update.json"] parameters:[NSDictionary dictionaryWithObject:@"This is a sample message!" forKey:@"status"] requestMethod:TWRequestMethodPOST];
                 [postRequest setAccount:acct];

                 // Perform the request created above and create a handler block to handle the response.
                 [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
                  {
                      if(error)
                      {
                          NSLog(@"Twitter Request failed with error: %@",[error localizedDescription]);
                      }else{
                          NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]);
                          NSLog(@"Message %@",[NSHTTPURLResponse localizedStringForStatusCode:[urlResponse statusCode]]);
                      }
                  }];
             }
         }
     }];
}

Inside the block, I am not getting any error, but I got to print this,

2013-09-12 19:51:40.103 MyApp[8380:21603] Twitter response, HTTP response: 410

2013-09-12 19:51:44.053 MyApp[8380:21603] Message no longer exists

It's not logged any error message!

Note,

1) I'd already configured a twitter account in the settings.

Update

I tried to link, https://upload.twitter.com/1.1/statuses/update.json in browser and it print the following JSON response!

{
    errors: [
    {
        message: "The Twitter REST API v1 is no longer active. Please migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.",code: 68
    } 
    ]
}

What's wrong? I am missing something? Is there a simple way to migrate to new API?

You're using Twitter API version 1 which is deprecated.

The new one is documented here https://dev.twitter.com/docs/api/1.1

You may want to check third-party libraries such as STTwitter to ease you work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM