首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用iOS SDK在facebook上分享视频

使用iOS SDK在facebook上分享视频
EN

Stack Overflow用户
提问于 2017-05-11 23:37:21
回答 2查看 849关注 0票数 0

我需要在脸书上分享一个视频,并有这个视频的fileURL,但要分享我需要这个视频的资产网址。如何在不使用UIImagePickerViewController的情况下从本地视频中获取资产地址?

EN

回答 2

Stack Overflow用户

发布于 2017-11-02 07:02:35

Facebook SDK需要资产URL,因此您必须将视频文件传递到资源库并从中获取新的url。完整的代码如下所示:

代码语言:javascript
运行
复制
NSURL *_videoURL = [URL to local video file];
//initilize the asset library object and define the completion block
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);
        //share to facebook when we have new asset URL
        FBSDKShareVideo *shareVideo = [[FBSDKShareVideo alloc]init];
        shareVideo.videoURL = newURL;
        FBSDKShareVideoContent *shareContent = [[FBSDKShareVideoContent alloc] init];
        shareContent.video = shareVideo;
        [FBSDKShareDialog showFromViewController:self withContent:shareContent delegate:nil];
    }
};

//write video file and fire up facebook sharing diaglog when complete
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:_videoURL])
{
    [library writeVideoAtPathToSavedPhotosAlbum:_videoURL
                                completionBlock:videoWriteCompletionBlock];
}

更新(适用于iOS SDK 9.0+):

代码语言:javascript
运行
复制
 NSURL *_videoURL = [URL to local video file];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    PHAssetChangeRequest *changeRequest= [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:_videoURL];
    _videoPlaceHolder= [changeRequest placeholderForCreatedAsset];

} completionHandler:^(BOOL success, NSError * _Nullable error) {
    if (error) {
        NSLog( @"Error writing image with metadata to Photo Library: %@", error );
    } else {
        NSLog( @"Wrote image with metadata to Photo Library %@", [_videoPlaceHolder localIdentifier]);
        //example for localidentifier:2BC8A8A8-E974-42D9-AD0F-2F463B353914/L0/001
        NSArray *id=[_videoPlaceHolder.localIdentifier componentsSeparatedByString:@"/"];
        NSString *path= [NSString stringWithFormat:@"assets-library://asset/asset.MOV?id=%@&ext=MOV",id[0]];
        _videoAssetURL = [NSURL URLWithString:path];

        dispatch_async(dispatch_get_main_queue(), ^{
            //share to facebook
            FBSDKShareVideo *shareVideo = [[FBSDKShareVideo alloc]init];
            shareVideo.videoURL = _videoAssetURL;
            FBSDKShareVideoContent *shareContent = [[FBSDKShareVideoContent alloc] init];
            shareContent.video = shareVideo;
            [FBSDKShareDialog showFromViewController:self withContent:shareContent delegate:nil];
        });
    }
}];
票数 2
EN

Stack Overflow用户

发布于 2017-05-23 15:23:02

代码语言:javascript
运行
复制
import FacebookShare

let shareDialog = ShareDialog(content: myContent)
shareDialog.mode = .Native
shareDialog.failsOnInvalidData = true
shareDialog.completion = { result in 
    // Handle share results
}

try shareDialog.show()

有关更多详细信息,请参阅Facebook开发人员API

https://developers.facebook.com/docs/swift/sharing/share-dialog

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43919947

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档