将图片附加到Twitter帖子

4sl*_*ces 4 iphone twitter file-sharing ios

如何将图片附加到Twitter内容,就像照片应用程序中内置的iPhone一样?如果任何身体有一些样本代码将是一个很大的帮助.谢谢.

Dan*_*iel 5

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

请看这里:在IOS6中不推荐使用TWTweetComposeViewController

来自Apple自己,WWDC 2012,会议306演示文稿PDF:

Twitter框架

•Twitter框架已弃用

•不要使用TWTweetComposeViewController

现在要使用Twitter,你应该使用框架的SLComposeViewControllerSocial,它的用法几乎相同TWTweetComposeViewController.

您可能需要支持iOS 5,在这种情况下您没有其他选择来使用TWTweetComposeViewController该类,但您应该努力检查SLComposeViewController并使用它,如果它可用,只是因为这将节省您在附近的时间和精力未来当iOS 5的支持被删除时,该TWTweetComposeViewController课程可能会消失.如果你现在为了简单而依赖Twitter框架,因为它可以在iOS 5和6上运行,那么你是短视的,以后你遇到问题,只需要几行就可以了,这意味着你赢了'需要担心未来的iOS SDK版本.

您应该导入Twitter.framework并将Social.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...
}
Run Code Online (Sandbox Code Playgroud)