如何使用 Facebook iOS SDK 4.0 FBSDKShareDialog 共享 NSData 或 phasset 视频

问题描述:

我注意到你可以简单地将 NSData 视频分享到 Facebook Messenger:

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

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

但我在分享到 Facebook 提要时遇到了同样的困难使用本地视频文件或相位集.

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=本机对话框只允许使用资产文件 URL

我将如何使用 phasset 视频实现类似的出色应用切换行为?

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

谢谢!

在新的 Facebook SDK 4.0 中,视频必须作为资产 URL 传递.您必须将本地视频路径复制到资产库,并使用生成的 URL 在 Facebook 上分享.

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.

第一步:

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

第 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];
    }
}

第 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.

谢谢!