【问题标题】:Is automated sharing possible for Twitter in the background on iOS?Twitter 是否可以在 iOS 的后台自动分享?
【发布时间】:2012-12-05 10:56:51
【问题描述】:

我想在 Twitter 墙上分享一个 Twitter 提要,其中包含一张图片和一些文字。我想要从 iOS 4.3 到 iOS 6.0.1 的支持。是否可以在没有发送/共享按钮的情况下在后台共享?如何实现?

【问题讨论】:

    标签: iphone ios twitter


    【解决方案1】:

    您需要发送的 API 调用是:

    https://dev.twitter.com/docs/api/1.1/post/statuses/update_with_media

    当然,在进行该调用之前,您需要通过 xAuth/OAuth 向 Twitter 进行身份验证。除非你得到 Twitter 的特别许可,否则你似乎需要使用 OAuth,

    https://dev.twitter.com/docs/oauth/xauth

    为请求后台处理,使用 Grand Central Dispatch 可能很有意义——除非您有很多不同的 Twitter 请求要发送。在这种情况下,我会选择NSOperationQueue,其中maxConcurrentOperationCount = 1。请参阅以下内容:

    http://www.fieryrobot.com/blog/2010/06/27/a-simple-job-queue-with-grand-central-dispatch/

    http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues

    尽管如此,由于 OAuth 非常痛苦,因此使用第三方库可能是有意义的。我以前从未使用过它,但这里有一个使用 MGTwitterEngine 的示例:

    Twitter's statuses/update_with_media on iOS returns 500 error

    如果您能够限制使用 iOS 5+,那么我强烈建议您使用 SLRequest 对象。这种方法的优点是您可以直接与 iOS 用户帐户集成,因此他们不必通过 UIWebView 或一些俗气的东西进行身份验证。

    为此,您只需将适当的 Twitter API url 插入以下函数 requestForServiceType:requestMethod:URL:parameters: 并获取您的 SLRequest 对象。然后使用requestAccessToAccountsWithType:options:completion: 分配从ACAccountStore 获得的适当Twitter ACAccount。最后拨打您的电话到performRequestWithHandler,然后它会异步执行您的请求。

    【讨论】:

      【解决方案2】:

      以下代码不会在后台发布,但可以跨 ios 版本发布... 您可以对 ios 版本使用条件,如下面的代码。这是我已经实现的工作代码,它适用于 ios 5 和 6。请检查 ios 4 以确认。我认为它应该可以工作。

      #import "Twitter/Twitter.h"
      #import "Social/Social.h"
      
      -(IBAction)tweetPost:(id)sender
      {
       if ([self  isSocialAvailable])
       {
        SLComposeViewController *tweetComposer=[SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
      
        if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]) {
         SLComposeViewControllerCompletionHandler __block completionHandler=
         ^(SLComposeViewControllerResult result){
      
          [tweetComposer dismissViewControllerAnimated:YES completion:nil];
      
          switch(result){
           case SLComposeViewControllerResultCancelled:
           default:
           {
            NSLog(@"Cancelled.....");
           }
            break;
           case SLComposeViewControllerResultDone:
           {
            NSLog(@"Posted....");
            UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Sent"
                                                             message:nil
                                                            delegate:nil
                                                   cancelButtonTitle:@"Dismiss"
                                                   otherButtonTitles: nil];
            [alert show];
           }
            break;
          }};
         NSString*message = @"posting to twitter test ios 6";
         [tweetComposer setInitialText:message];
         [tweetComposer addImage:[UIImage imageNamed:@"2.jpg"]];
         [tweetComposer addURL:[NSURL URLWithString:@"http://www.youtube.com/watch?v=GoZ2Be2zLq8"]];
      
         [tweetComposer setCompletionHandler:completionHandler];
         [self presentViewController:tweetComposer animated:YES completion:nil];
        }
       }
       else
       {
        TWTweetComposeViewController *twitter= [[TWTweetComposeViewController alloc] init];
      
        [twitter addImage:[UIImage imageNamed:@"2.jpg"]];
        [twitter addURL:[NSURL URLWithString:@"http://www.youtube.com/watch?v=GoZ2Be2zLq8"]];
        [twitter setInitialText:@"Tweet from iOS 5 app using the Twitter framework."];
      
        [self presentModalViewController:twitter animated:YES];
      
        twitter.completionHandler = ^(TWTweetComposeViewControllerResult result)
        {
         NSString *title = @"Tweet Status";
         NSString *msg;
      
         if (result == TWTweetComposeViewControllerResultCancelled)
          msg = @"Tweet compostion was canceled.";
         else if (result == TWTweetComposeViewControllerResultDone)
          msg = @"Tweet composition completed.";
      
         // Show alert to see how things went...
         UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil];
         [alertView show];
        };
       }
      }
      
      
      -(BOOL)isSocialAvailable {
       return NSClassFromString(@"SLComposeViewController") != nil;
      }
      

      您需要包含三个名为 social、adSupport 和 Accounts 的框架。在 twitter 提要帖子中检查哪个不需要。 希望对你有所帮助。

      【讨论】:

        【解决方案3】:

        是的,但是您需要为您和授权者找到一些 1.1 API 包装器(生成 API 请求、发送它们等的东西)(MGWTitter 引擎工作正常)。我有一个working solution for sharing (text only) and getting user info for iOS 4+

        关于后台部分 - 这取决于您如何实现(即通知或连续后台执行或 gps callbacs 等...)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-06-02
          • 2013-03-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-07
          • 2018-01-14
          相关资源
          最近更新 更多