分享视频到Facebook

时间:2016-02-24 19:27:09

标签: ios swift swift2 facebook-sdk-4.0

我正在编写一个简单的测试应用程序,用于将视频从iOS上传到Facebook。

由于FacebookSDK的所有文档都在Objective-C中,因此我发现很难在线查找有关如何使用Swift执行此操作的示例/教程。

到目前为止,我有这个在我的用户界面上放置一个分享按钮,但它看起来已禁用,从我读到的内容是因为没有内容设置,但我看不出这是怎么回事。

    let video_content : FBSDKShareVideoContent = FBSDKShareVideoContent()

    video_content.video = FBSDKShareVideo(videoURL: getVideoURL())

    let button : FBSDKShareButton = FBSDKShareButton()
    button.shareContent = video_content

    button.frame = CGRectMake((UIScreen.mainScreen().bounds.width - 100) * 0.5, 50, 100, 25)
    self.view.addSubview(button)

我的getVideoURL()函数返回一个NSURL,它肯定包含视频的url(我已将其打印到控制台并检查它指向我之前存储在文件夹中的视频)。

我查看了社交框架,遗憾的是我无法使用它,因为它似乎只用于分享照片而无法分享视频。

我正在使用最新版本的Xcode,Swift和Facebook SDK。

5 个答案:

答案 0 :(得分:9)

转换为swift 3和4: - 试试这个

func uploadVideoOnFacebook() {
        var pathURL: URL
        var videoData: Data
        pathURL = URL(string: "Your video url string")!
        do {
            videoData = try Data(contentsOf: URL(string: "Your video url string")!)
            print(videoData)
            var strDesc : String
            strDesc = ""
            let videoObject: [String : Any] = ["title": "application Name", "description": strDesc, pathURL.absoluteString: videoData]
            let uploadRequest: FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me/videos", parameters: videoObject, httpMethod: "POST")
            self.view!.isUserInteractionEnabled = false
            uploadRequest.start(completionHandler: {(connection: FBSDKGraphRequestConnection, result: AnyObject, error: NSError) -> Void in
                if error == error {
                    NSLog("Error")

                }
                else {
                    NSLog("Success")
                }
            } as! FBSDKGraphRequestHandler)
        } catch {
            print(error)
        }

    }

答案 1 :(得分:3)

@AngryDuck

尝试使用此代码

func uploadVideoOnFacebook() {
    var pathURL: NSURL
    var videoData: NSData
    pathURL = NSURL(string: self.strUploadVideoURL)!
    videoData = NSData.dataWithContentsOfURL(NSURL(string: self.strUploadVideoURL)!)
    NSString * strDesc
    strDesc = txtCaption.text!
    var videoObject: [NSObject : AnyObject] = ["title": "application Name", "description": strDesc, pathURL.absoluteString(): videoData]
    var uploadRequest: FBRequest = FBRequest(graphPath: "me/videos", parameters: videoObject, HTTPMethod: "POST")
    self.view!.userInteractionEnabled = false
    uploadRequest.startWithCompletionHandler({(connection: FBRequestConnection, result: AnyObject, error: NSError) -> Void in
        if !error {
            NSLog("Success")
        }
        else {
            NSLog("Error")
        }
    })
}

答案 2 :(得分:1)

我在目标c中完成了这个,我遇到了类似的问题。不知道这是否适用于swift。

问题在于,如果出于某种原因,视频必须存储在NSDocumentDirectoryNSTemporaryDirectory中,那么她就可以工作了。不是,如果是你的情况。

尽管您可以在最糟糕的情况下{/ 1}}使用/ me / Videos

我已经看到这种分享您尝试使用的视频的方式,直接从照片库中获取链接。正如您在this example

中看到的那样

目前只有FBSDKGraphRequest中的示例我希望这可以帮助您

objective-c

您可以使用Xcode自动完成功能更改为swift sintax。

您可以使用this

的语法快速帮助自己

答案 3 :(得分:1)

我在我的应用程序中使用了此代码,这对我来说很好。

此代码适用于 Objective-C ,但您可以在将其转换为swift语法后在swift中使用相同内容。

我希望这会对你有所帮助。

尝试以下代码。

-(void) uploadVideoOnFacebook 
{
    NSURL *pathURL;
    NSData *videoData;


    pathURL = [NSURL URLWithString:self.strUploadVideoURL];
    videoData = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.strUploadVideoURL]];

    NSString *strDesc;
    strDesc = txtCaption.text;

    NSDictionary *videoObject = @{@"title": @"application Name",@"description": strDesc,[pathURL absoluteString]: videoData};
    FBRequest *uploadRequest = [FBRequest requestWithGraphPath:@"me/videos" parameters:videoObject HTTPMethod:@"POST"];
    [self.view setUserInteractionEnabled:NO];

    [uploadRequest startWithCompletionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
           if (!error)
               NSLog("Success");
           else
               NSLog("Error");
    }];
}

答案 4 :(得分:0)

使用SDK Facebook:

https://developers.facebook.com/docs/sharing/ios

视频网址videoURL必须是资产网址。您可以获取视频资产网址,例如来自UIImagePickerController。

import FBSDKCoreKit
import FBSDKShareKit
import MobileCoreServices
import Photos

        func createAssetURL(url: URL, completion: @escaping (String) -> Void) {
            let photoLibrary = PHPhotoLibrary.shared()
            var videoAssetPlaceholder:PHObjectPlaceholder!
            photoLibrary.performChanges({
                let request = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: url)
                videoAssetPlaceholder = request!.placeholderForCreatedAsset
            },
                completionHandler: { success, error in
                    if success {
                        let localID = NSString(string: videoAssetPlaceholder.localIdentifier)
                        let assetID = localID.replacingOccurrences(of: "/.*", with: "", options: NSString.CompareOptions.regularExpression, range: NSRange())
                        let ext = "mp4"
                        let assetURLStr =
                        "assets-library://asset/asset.\(ext)?id=\(assetID)&ext=\(ext)"

                        completion(assetURLStr)
                    }
            })
        }

        func shareFacebbok() {
            let content: FBSDKShareVideoContent = FBSDKShareVideoContent()
            let videoURLs = Bundle.main.url(forResource: "video", withExtension: "mp4")!
            createAssetURL(url: videoURLs) { url in
                let video = FBSDKShareVideo()
                video.videoURL = URL(string: url)
                content.video = video

                let shareDialog = FBSDKShareDialog()
                shareDialog.shareContent = content
                shareDialog.mode = .native
                shareDialog.delegate = self
                shareDialog.show()
            }
        }