Вопрос

I'm using this code to send a tweet from my app to user twitter account. I have no problem until at least one account is selected in the twitter device settings. If i delete all accounts from settings my tweet fail.

 ACAccountStore *accountStore=[[ACAccountStore alloc] init];
   ACAccountType *twitterType =
   [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

   SLRequestHandler requestHandler =
   ^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
      if (responseData) {
         NSInteger statusCode = urlResponse.statusCode;
         if (statusCode >= 200 && statusCode < 300) {
            NSDictionary *postResponseData =
            [NSJSONSerialization JSONObjectWithData:responseData
                                            options:NSJSONReadingMutableContainers
                                              error:NULL];
            NSLog(@"[SUCCESS!] Created Tweet with ID: %@", postResponseData[@"id_str"]);
         }
         else {
            NSLog(@"[ERROR] Server responded: status code %d %@", statusCode,
                  [NSHTTPURLResponse localizedStringForStatusCode:statusCode]);
         }
      }
      else {
         NSLog(@"[ERROR] An error occurred while posting: %@", [error localizedDescription]);
      }
   };

   ACAccountStoreRequestAccessCompletionHandler accountStoreHandler =
   ^(BOOL granted, NSError *error) {
      if (granted) {
         NSArray *accounts = [accountStore accountsWithAccountType:twitterType];
         NSURL *url = [NSURL URLWithString:@"https://api.twitter.com"
                       @"/1.1/statuses/update_with_media.json"];
         NSDictionary *params = @{@"status" : @"status"};
         SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter
                                                 requestMethod:SLRequestMethodPOST
                                                           URL:url
                                                    parameters:params];
         NSData *imageData = UIImageJPEGRepresentation(Photo, 1.f);
         [request addMultipartData:imageData
                          withName:@"media[]"
                              type:@"image/jpeg"
                          filename:@"image.jpg"];
         [request setAccount:[accounts lastObject]];
         [request performRequestWithHandler:requestHandler];
      }
      else {
         NSLog(@"[ERROR] An error occurred while asking for user authorization: %@",
               [error localizedDescription]);
      }
   };

   [accountStore requestAccessToAccountsWithType:twitterType
                                              options:NULL
                                           completion:accountStoreHandler];

There's a way to send a tweet when there isn't accounts in the twitter device settings? Maybe the perfect solution should be to call an api and made a login request with username and password as parameters. There's a way to do this? Or there is a better method?

Это было полезно?

Решение

What you are trying to do is that you are trying to send a tweet using accounts framework only. You haven't written code to send tweet using normal twitter API.

do one thing validate first if you can send tweet using accounts framework. if not then proceed to normal OAuth authentication

you can validate twitter account using

if ([TWTweetComposeViewController canSendTweet])
{
    TWTweetComposeViewController *tweetSheet = 
        [[TWTweetComposeViewController alloc] init];
    [tweetSheet setInitialText:@"Initial Tweet Text!"];
    [self presentModalViewController:tweetSheet animated:YES];
}

if this condition fails then post sweet using OAuth API. The simplest possible way is to use this control

https://github.com/malcommac/DMTwitterOAuth

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top