【问题标题】:Attach a picture to twitter post将图片附加到推特帖子
【发布时间】:2012-10-06 03:16:12
【问题描述】:

如何像 iPhone 内置的照片应用程序那样将图片附加到 twitter 帖子? 如果任何机构有一些示例代码,那将是一个很大的帮助。 谢谢。

【问题讨论】:

  • :[tweetViewController addImage:img];

标签: iphone ios twitter file-sharing


【解决方案1】:

其他答案建议TWTweetComposeViewController,但是如果你可以避免使用这个类,它现在在 iOS 6 中已被弃用,

请看这里:TWTweetComposeViewController deprecated in IOS6

来自 Apple 自己,WWDC 2012, session 306 presentation PDF

推特框架

• Twitter 框架已弃用

• 不要使用 TWTweetComposeViewController

现在要使用 Twitter,你应该使用 Social 框架的 SLComposeViewController 类,它的用法几乎与 TWTweetComposeViewController 相同。

您可能需要支持 iOS 5,在这种情况下,您别无选择,只能使用 TWTweetComposeViewController 类,但您应该努力检查 SLComposeViewController 并在可用时使用它,仅仅是因为这个在不久的将来,当对 iOS 5 的支持被取消时,TWTweetComposeViewController 类真的可能会消失,这将为您节省时间和精力。如果您现在为了简单起见而依赖 Twitter 框架,因为它确实适用于 iOS 5 和 6,那么您是目光短浅的,并且您在稍后的某个时间遇到问题,只需多几行代码就可以做到这一点这意味着您无需担心未来的 iOS SDK 版本。

您应该导入Twitter.frameworkSocial.framework,将它们都标记为可选 导入(非必需)。

示例代码:

UIImage *myImage = [...]; // an image

if( NSClassFromString(@"SLComposeViewController") ){
    // We have the Social framework in our iOS system
    // iOS 6 and later will use this

    if([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter]){
        SLComposeViewController *twitterCompose = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

        [twitterCompose addImage:myImage]; // Adding your UIImage

        twitterCompose.completionHandler = ^(SLComposeViewControllerResult result){
            // Handle result, dismiss view controller
            [self dismissViewControllerAnimated:YES 
                                     completion:nil];
        };

        [self presentViewController:twitterCompose
                           animated:YES
                         completion:nil];
    }else{
        // the user does not have Twitter set up
    }
}else if( NSClassFromString(@"TWTweetComposeViewController") ){
    // We don't have the Social framework, work with the Twitter framework
    // iOS 5 only will use this

    if( [TWTweetComposeViewController canSendTweet] ){
        TWTweetComposeViewController *twitterCompose = [[TWTweetComposeViewController alloc] init];

        [twitterCompose addImage:myImage];

        twitterCompose.completionHandler = ^(TWTweetComposeViewControllerResult result){
            // Handle result, dismiss view controller
            [self dismissViewControllerAnimated:YES 
                                     completion:nil];
        };
        [self presentViewController:twitterCompose
                           animated:YES
                         completion:nil];
    }else{
        // the user hasn't go Twitter set up on their device.
    }
}else{
    // Wow you're going retro with this app, 
    // you must be on iOS 4 if you ever get here...
}

【讨论】:

  • 那么我需要将哪些框架添加到我的项目中以及在哪里:在头文件或实现文件中?我是否需要全部添加 twitter、社交、帐户和安全框架,或者 twitter 是否足够好?谢谢。
  • 只需Social.framework,进入您的项目,进入目标,然后构建阶段,打开Link Binary With Libraries,点击+按钮并搜索Social.framework。然后在您的视图控制器中确保导入:#import <Social/Social.h>,您可以在标头或实现中执行此操作。不需要 Account 框架,如果需要支持 iOS 5 可以添加 Twitter 框架
  • 但我收到一条错误消息(twitterCompose)说:没有可见的@interface for'SLComposerViewController'声明选择器'setImage:'
  • 我的错...这是addImage: 不是setImage:,抱歉:)
  • 现在它可以工作了,但是为什么你认为取消按钮不起作用,除非我点击它两次?
【解决方案2】:

我是如何使用它的:

        NSLog(@"Ready to Tweet."); 
        TWTweetComposeViewController *tweetComposer = [[TWTweetComposeViewController alloc] init];
        [tweetComposer setInitialText:[NSString stringWithFormat:@"My message"]];
        [tweetComposer addImage:[UIImage imageNamed:@"114x114"]];
        [tweetComposer addURL:[NSURL URLWithString:@"http://myPage"]];
        tweetComposer.completionHandler = ^(TWTweetComposeViewControllerResult result){
        if(result == TWTweetComposeViewControllerResultDone){
            NSLog(@"Tweeted.");

        } else if(result == TWTweetComposeViewControllerResultCancelled) {
            NSLog(@"Cancelled.");
        }

        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        };
        [self presentModalViewController:tweetComposer animated:YES];

【讨论】:

  • 您是否需要我将图片上传到网站并发布链接。如果这是您的代码所做的,请告诉我如何附加我的应用程序中存在的图像以在 Twitter 上发布?提前致谢。
  • 您不应再使用TWTweetComposeViewController,它已被弃用。您应该使用SLComposeViewController 类。用法几乎相同。
  • 您有一个UIImage,在您的应用程序中,SLComposeViewControllerTWTweetComposeViewController 处理上传到 Twitter。但是请看我的回答,因为你应该避免使用TWTweetComposeViewController:stackoverflow.com/a/12907850/662605
  • 我不同意....问题更复杂。如果您支持 iOS 4.3 及更高版本(就像我一样),最好使用 TWTweetComposeViewController(请注意,推文只能从 iOS5 开始工作)。如果你只打算使用 iOS 6 SLComposeViewController 会更好
【解决方案3】:

如果你使用的是ios 5.0,那么你可以直接发布图片

添加框架 twitter.framework

导入 Twitter/TWTweetComposeViewController.h

-(void)postToTwittert
{
    Class TWTweetComposeViewControllerClass = NSClassFromString(@"TWTweetComposeViewController");

    if (TWTweetComposeViewControllerClass != nil) {
        if([TWTweetComposeViewControllerClass respondsToSelector:@selector(canSendTweet)]) {
            TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];

            [twitter setInitialText:@"text"];
            [twitter addImage:[UIImage imageNamed:@"imagename"]];
            [twitter addURL:[NSURL URLWithString:@"http://www.google.com"]];

            [self presentViewController:twitter animated:YES completion:nil];

            twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {

                if(res == TWTweetComposeViewControllerResultDone)
                {
                    NSLog(@"success for twitter post");

                }
                else if(res == TWTweetComposeViewControllerResultCancelled)
                {

                    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Canceled" message:@"Your Tweet was not posted" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

                    [alertView show];

                }

                [self dismissModalViewControllerAnimated:YES];

            };
        }
    }
}

在你想发推特的地方调用这个函数

并根据需要进行适当的更改..

祝你好运..

【讨论】:

    【解决方案4】:

    我现在只是使用UIActivityViewController 在 Twitter 上发帖。

    UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:@[@"Default Text", [UIImage imageNamed:@"imageName"]] applicationActivities:nil];
    
    [self presentViewController:controller animated:YES completion:nil];
    

    这将呈现一个控制器,用户可以在其中决定做什么(发布到 Twitter、发布到 Facebook 等...)

    然后它使用系统推文表等...来做。

    您不必提供默认文本。这无论如何都可以被覆盖。

    哦,另外,这不需要任何框架。

    【讨论】:

      猜你喜欢
      • 2015-08-09
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-17
      • 2017-09-20
      • 1970-01-01
      相关资源
      最近更新 更多