简体   繁体   English

使用FB SDK在iOS上共享视频

[英]sharing video on ios using fb sdk

I am very new to programming in iOS and therefore, the question might seem rather stupid but I really do not know what is wrong. 我对iOS编程非常陌生,因此,这个问题似乎很愚蠢,但我真的不知道出什么问题了。

So, I was following sharing in iOS on the https://developers.facebook.com/docs/sharing/ios and I was successful in sharing link, and photo. 因此,我一直在通过https://developers.facebook.com/docs/sharing/ios在iOS上进行共享,并且成功共享了链接和照片。 But I have no idea why I cannot share a video. 但是我不知道为什么不能分享视频。 I am not using UIImagePickerControllerReferenceURL but instead, I am using [[NSBundle mainBundle] URLForResource:@"xyz" withExtension:@"mp4"] for the videoURL . 我没有使用UIImagePickerControllerReferenceURL ,而是为videoURL使用了[[NSBundle mainBundle] URLForResource:@"xyz" withExtension:@"mp4"]
Is this the reason why the sharing button doesn't work? 这是共享按钮不起作用的原因吗?

My aim is to let users share content from the app directly. 我的目的是让用户直接从应用程序共享内容。 So I do not want them to use UIImagePickerControllerReferenceURL . 所以我不希望他们使用UIImagePickerControllerReferenceURL
Here is the code: 这是代码:

NSURL* videoURL = [[NSBundle mainBundle] URLForResource:@"xyz" withExtension:@"mp4"];
FBSDKShareVideo *video = [[FBSDKShareVideo alloc] init];
video.videoURL = videoURL;
FBSDKShareVideoContent *content = [[FBSDKShareVideoContent alloc] init];
content.video = video;

FBSDKShareButton *button = [[FBSDKShareButton alloc] init];
button.center = self.view.center;
button.shareContent = content;
[self.view addSubview:button];

And file xyz.mp4 is in a Media Folder. 文件xyz.mp4在媒体文件夹中。

NSURL* videoURL = [[NSBundle mainBundle] URLForResource:@"xyz" withExtension:@"mp4"];

replace with this 用这个替换

NSURL* videoURL = [[NSBundle mainBundle] URLForResource:@"xyz" ofType:@"mp4"];

[self saveToPhotoAlbum:videoURL];

After that save this video to photo library as Facebook required asset library url. 之后,将该视频保存为照片库,因为Facebook需要资产库URL。

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

    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:url])
    {
        [library writeVideoAtPathToSavedPhotosAlbum:url
                                    completionBlock:videoWriteCompletionBlock];
    }
}

then 然后

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM