5

I noticed you can share NSData video to facebook messenger simply:

NSData *videoData = [NSData dataWithContentsOfURL:localVideoUrl];
[FBSDKMessengerSharer shareVideo:videoData withOptions:options];

But I’m having difficulties doing the same when sharing to facebook feed using local video file or phasset.

FBSDKShareVideo *video = [FBSDKShareVideo videoWithVideoURL:localVideoUrl];
FBSDKShareVideoContent *content = [[FBSDKShareVideoContent alloc] init];
[content setVideo: video];
[FBSDKShareDialog showFromViewController:nil withContent:content delegate:self];

com.facebook.sdk:FBSDKErrorDeveloperMessageKey=Only asset file URLs are allowed for the native dialog

How would I go about having similar nice app-switching behavior using phasset video?

Thanks!

Code cracker
  • 3,105
  • 6
  • 37
  • 67
Fabe
  • 51
  • 1
  • 3

2 Answers2

13

With the new Facebook SDK 4.0, videos must be passed as assets URL. You have to copy your local video path to Assets Library and use that generated URL to share on Facebook.

Step 1:

NSURL *videoURL=[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"IMG_1007" ofType:@"mp4"]];
[self saveToCameraRoll:videoURL];

Step 2:

- (void)saveToCameraRoll:(NSURL *)srcURL
{
    NSLog(@"srcURL: %@", srcURL);

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    ALAssetsLibraryWriteVideoCompletionBlock videoWriteCompletionBlock =
    ^(NSURL *newURL, NSError *error) {
        if (error) {
            NSLog( @"Error writing image with metadata to Photo Library: %@", error );
        } else {
            NSLog( @"Wrote image with metadata to Photo Library %@", newURL.absoluteString);
            url_new  = newURL;
        }
    };

    if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:srcURL])
    {
        [library writeVideoAtPathToSavedPhotosAlbum:srcURL
                                    completionBlock:videoWriteCompletionBlock];
    }
}

Step 3:

FBSDKShareDialog *shareDialog = [[FBSDKShareDialog alloc] init];    
NSURL *videoURL = url_new;
FBSDKShareVideo *video = [[FBSDKShareVideo alloc] init];
video.videoURL = videoURL;
FBSDKShareVideoContent *content = [[FBSDKShareVideoContent alloc] init];   
content.video = video;
shareDialog.shareContent = content;
shareDialog.delegate = self;
[shareDialog show];

If you have any other query please let me know.

Thanks!

Velociround
  • 611
  • 7
  • 18
Eliza
  • 212
  • 1
  • 6
  • 3
    Thanks for the suggestion! That would work, but the video that I’d like to share already exists in photo library since its an PHAsset. I wouldn’t want to create another copy. I can save PHAsset video locally temporarily (how I solved few other related sharing problems) or use its data. But there’s not really a way to bridge PHAsset to ALAsset to my knowledge? There’s no way to share PhotoKit videos using Facebook SDK 4.0 FBSDKShareDialog? – Fabe Apr 04 '15 at 09:50
  • This is the new url generated!! `assets-library://asset/asset.mp4?id=E121FF88-B0AB-4EE9-BD60-E676617BDC54&ext=mp4` – jose920405 Nov 20 '15 at 22:18
  • It is possible to do this below without showing any modal and put description and title to the video? – jose920405 Dec 02 '15 at 13:41
  • I don't think you can override modal presenting style and attach any text or link using Sharing Dialogs. – Dmitry Kurilo Dec 09 '15 at 20:12
  • 1
    if i want share video from my own server url like- http://www.myownserverurl.com/myvide.mp4 what to do? – amit gupta Jan 06 '16 at 07:05
  • sharing video from `PHAsset` is working if you use the url generated from this post: http://stackoverflow.com/questions/28887638/how-to-get-an-alasset-url-from-a-phasset – medvedNick Mar 05 '17 at 19:50
0

Please check: 1)The videos must be less than 12MB in size. 2)People who share should have Facebook for iOS client installed, version 26.0 or higher.

You should use the below line:

NSURL *movieUrl = [info objectForKey:UIImagePickerControllerReferenceURL];

instead of

NSURL *movieUrl = [info objectForKey:UIImagePickerControllerMediaURL];
Dani Pralea
  • 4,545
  • 2
  • 31
  • 49
Ritu
  • 661
  • 3
  • 8