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

时间:2015-04-01 08:48:24

标签: ios facebook video nsdata

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

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

但是在分享到Facebook Feed时我遇到了同样的困难 使用本地视频文件或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 =本机对话框只允许使用资产文件网址

如何使用phasset视频进行类似的优秀app切换行为?

谢谢!

2 个答案:

答案 0 :(得分:12)

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

第1步:

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

如果您有任何其他疑问,请告诉我。

谢谢!

答案 1 :(得分:0)

请检查:1)视频的大小必须小于12MB。 2)分享的人应安装Facebook for iOS客户端,版本26.0或更高版本。

您应该使用以下行:

NSURL *movieUrl = [info objectForKey:UIImagePickerControllerReferenceURL];

而不是

NSURL *movieUrl = [info objectForKey:UIImagePickerControllerMediaURL];