【问题标题】:Sending Tweet with no account setted in "Iphone Settings"在“Iphone 设置”中发送没有帐户设置的推文
【发布时间】:2013-09-07 00:44:58
【问题描述】:

我正在使用此代码从我的应用程序向用户 Twitter 帐户发送推文。在 Twitter 设备设置中至少选择一个帐户之前,我没有问题。如果我从设置中删除所有帐户,我的推文将失败。

 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];

当 Twitter 设备设置中没有帐户时,有一种方法可以发送推文吗?也许完美的解决方案应该是调用一个api并使用用户名和密码作为参数进行登录请求。有办法做到这一点吗?还是有更好的方法?

【问题讨论】:

    标签: iphone ios twitter acaccount acaccountstore


    【解决方案1】:

    您要做的是尝试仅使用帐户框架发送推文。您还没有编写代码来使用普通的 twitter API 发送推文。

    如果您可以使用帐户框架发送推文,请先做一件事验证。如果不是,则继续进行正常的 OAuth 身份验证

    您可以使用

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

    如果此条件失败,则使用 OAuth API 发布甜蜜。最简单的方法是使用这个控件

    https://github.com/malcommac/DMTwitterOAuth

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-03
      • 2014-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多