将图片附加到Twitter帖子

时间:2012-10-16 04:42:36

标签: iphone ios twitter file-sharing

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

4 个答案:

答案 0 :(得分:5)

其他答案建议TWTweetComposeViewController,但是如果您可以避免使用此类,那么它现在已在iOS 6中弃用,

请参见此处:TWTweetComposeViewController deprecated in IOS6

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

  

Twitter框架

     

•Twitter框架已弃用

     

•请勿使用TWTweetComposeViewController

现在要使用Twitter,您应该使用SLComposeViewController框架的Social类,它的用法几乎与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...
}

答案 1 :(得分:0)

我在这里如何使用它:

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

答案 2 :(得分:0)

如果您使用的是ios 5.0,则可以直接发布图像

添加Framwork 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];

            };
        }
    }
}

将此功能称为推文帖子

并进行您想要的适当更改..

祝你好运..

答案 3 :(得分:0)

我现在只使用UIActivityViewController发布到Twitter。

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

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

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

然后使用系统推文表等来完成它。

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

哦,此外,没有框架需要。

相关问题