mikemeli
mikemeli

Reputation: 725

how to share video in twitter with UIActivityViewController

When i share a video using the UIActivityViewController, the twitter post does not show the video. However, if it take the same video and post it using the twitter app, the video shows and plays embedded in the post.

I am using this code:

 MyActivityItemProvider * videoItem = [[MyActivityItemProvider alloc] initWithPlaceholderItem:@""];


    NSArray * activityItems = [NSArray arrayWithObjects:
                               videoItem,
                               nil];

    UIActivityViewController *activityViewController = [[UIActivityViewController alloc]
                                                        initWithActivityItems:activityItems
                                                        applicationActivities:nil];

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

And in MyActivityItemProvider, I have this:

@implementation MyActivityItemProvider

- (MyActivityItemProvider * )initWithPlaceholderItem:(NSString *)placeholderItem
{
    self = [super initWithPlaceholderItem:placeholderItem];

    return self;
}
- (id)item
{
    NSString * path = [[NSBundle mainBundle] pathForResource:@"video_name" ofType:@"mp4"];

    NSURL * fileURL = [NSURL fileURLWithPath:path];

    return fileURL;
}

@end

Is it possible for a twitter video post to have the video embedded in it (as if I posted it using the twitter app) when posting using the UIActivityViewController?? Any suggestions on how to achieve this (what it looks) rather simple task?

The video is .mp4 and it is 3.1 MB (like I said, appears to post just fine using the Twitter app and I can send the video via txt message fine).

Upvotes: 1

Views: 1160

Answers (2)

Rain
Rain

Reputation: 320

Video cannot be shared with UIActivityViewController.

Thanks to the uploading media new API, you can share video to twitter by it. And I have tried it, it works.

Please check this: https://github.com/liu044100/SocialVideoHelper

You just need to call this class method.

+(void)uploadTwitterVideo:(NSData*)videoData comment:(NSString*)comment account:(ACAccount*)account withCompletion:(VideoUploadCompletion)completion;

Upvotes: 1

NSPratik
NSPratik

Reputation: 4848

Try this :

+ (void)shareOnTwiiterFromView:(UIViewController *)vc userTitle:(NSString *)title shareURL:(NSString *)urlString
{
    if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        SLComposeViewController *tweetSheet = [SLComposeViewController
                                               composeViewControllerForServiceType:
                                               SLServiceTypeTwitter];

        tweetSheet.completionHandler = ^(SLComposeViewControllerResult result)
        {
            switch(result)
            {
                case SLComposeViewControllerResultCancelled:
                    break;
                case SLComposeViewControllerResultDone:
                    break;
            }
        };

        NSString *twTitle = [NSString stringWithFormat:@"%@ %@",title,@"Your App Name"];
        [tweetSheet setInitialText:twTitle];

        if (![tweetSheet addURL:[NSURL URLWithString:urlString]])
        {
            NSLog(@"Unable to add the URL!");
        }

        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
        {
            UIPresentationController *control = tweetSheet.presentationController;
            [tweetSheet setModalPresentationStyle:UIModalPresentationFormSheet];

             [vc presentViewController:control.presentedViewController animated:YES completion:^
             {
                 NSLog(@"Tweet sheet has been presented.");
             }];
        }
        else
        {
             [vc presentViewController:tweetSheet animated:NO completion:^
             {
                 NSLog(@"Tweet sheet has been presented.");
             }];
        }
    }
    else
    {
        [APP_DEL showErrorAlert:@"No Twitter Account" description:@"There are no twitter accounts configured. You can add or create a Twitter account in Settings."];
    }
}

Upvotes: 0

Related Questions