Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share NSData or phasset video using Facebook iOS SDK 4.0 FBSDKShareDialog

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!

like image 255
Fabe Avatar asked Apr 01 '15 08:04

Fabe


People also ask

What information can I share with Facebook through the SDK?

By enabling Facebook integrations, including through this SDK, you can share information with Facebook, including information about people’s use of your app. Facebook will use information received in accordance with our Data Use Policy, including to provide you with insights about the effectiveness of your ads and the use of your app.

What is F acebook sharing SDK?

Sharing through Facebook is so common… | by Myrick Chow | ITNEXT F acebook Sharing SDK is a user friendly tools for sharing photos, videos and links easily through your app. User can share the media as a news feed or Story to Facebook app or as a direct message to a friend in Messenger app.

What are the requirements to share content on Facebook for iOS?

Note the following: People who share should have Facebook for iOS client installed. Photos must be less than 12MB and video must be less than 50MB in size. People can share a maximum of 1 video plus up to 29 photos or 30 photos. Build your multimedia share content with the ShareMediaContent model.

What kind of content can I share through the SDK?

User can share links, photos and videos through the SDK in form of news feed, story and direct message. Photos cannot be larger than 12MB and videos cannot be larger than 50MB. The maximum number of photo with video in a batch is 30. HashTag can be optionally added to each kind of sharing content.


1 Answers

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!

like image 190
Eliza Avatar answered Oct 19 '22 20:10

Eliza