Sharing a video from camera roll without image picker, is it possible at all?

ios
video
api

#1

Hi!

How to construct a valid video URL from the camera roll to share on Twitter without involving the image picker? Our application stores the actual file URL and the localIdentifier for creating the associated PHAsset.

The app implements a custom video library (showing the videos from the camera roll as looped videos), the user can choose a video inside our app and share it on social media. We store the file urls locally in this format:

file:///var/mobile/Media/DCIM/100APPLE/IMG_0249.MP4

If I create a videoURL with this format, like:

    NSURL* videoURL = [NSURL fileURLWithPath:@"/var/mobile/Media/DCIM/100APPLE/IMG_0249.MP4"];
    TWTRComposerViewController* composer = [[TWTRComposerViewController alloc] initWithInitialText:@"Share text" image: nil videoURL:videoURL];

The video will be shared as an image, but not as a video (I wonder if it’s actually a bug, since Twitter SDK is able to find it but not able to see that it’s a video but not an image)

In the official documentation, it says:

The videoURL parameter is required to have format of assets-library. Use key UIImagePickerControllerMediaURL from the didFinishPickingMediaWithInfo: info parameter.

However, when printing the actual URL format, it’s not with the format of asset-library://, but in this format:

file:///private/var/mobile/Containers/Data/Application/34F55A5A-91B4-4B54-9530-B69E98EE9D5C/tmp/trim.D525F74B-DB96-442A-9319-B0B797EBE8A0.MOV

Which seems to be only possible to get through the image picker.

How can I create a valid video URL with the actual file URL or the localIdentifier for the PHAsset? Any suggestion would be very appreciated!


#4

Hi Twitter Developers!

As no one seems to know the answer to my initial question, could you please take a look if this behavior is actually a bug? I share a video in my camera roll using a file url. It’s shared successfully, but only as an image.

The file URL is in this format:
@"/var/mobile/Media/DCIM/100APPLE/IMG_0249.MP4"

NSURL* videoURL = [NSURL fileURLWithPath:@“path to a video in camera roll”];
TWTRComposerViewController* composer = [[TWTRComposerViewController alloc] initWithInitialText:@“Share text” image: nil videoURL:videoURL];

The file format @“assets-library://asset/asset.mp4?id=localIdentifier&ext=mp4” doesn’t work for Twitter Kit SDK either (I use this format with Facebook SDK with no issues).

Many thanks!


#5

Sorry for the delayed response.

We plan to add NSData as one of parameters directly. However, I also found this stackoverflow answer, which let’s you get NSURL of the video file.
In TWTRComposerViewController initializer, videoURL is passed through [NSData dataWithContentsOfURL:videoURL].
I believe this would be a workaround for now.


#6

I got the same problem. if the video url has a scheme of file:, the framework shares the video as image. Do you have any update for this please? I tried converting the file url to assets-library but still no good.


#7

Hi @katejaiheelee, any updates on adding the NSData as one of the parameters directly?

Thanks for suggesting a workaround, but I don’t think TWTRComposerViewController can be initialized with an NSData, there are only two initializers found in the header:

  • (instancetype)initWithInitialText:(nullable NSString *)initialText image:(nullable UIImage *)image videoURL:(nullable NSURL *)videoURL;

  • (instancetype)init NS_UNAVAILABLE;

I can’t find any method of TWTRComposerViewController that actually accepts NSData, could you please give a short example to show your workaround? Thanks!


#8

With 3.1.0, updated on August 9, 2017, you can now add a composer with NSData with a preview image.
- (instancetype)initWithInitialText:(nullable NSString *)initialText image:(nullable UIImage *)image videoData:(nullable NSData *)videoData;


#9

Thanks! This approach works well!

Do you know where is the new version announced? I’ve been checking the Announcements board every now and then, but I didn’t see the news about the new update.


#10

https://dev.twitter.com/twitterkit/ios/changelog is the best place to check latest releases.