AngryDuck AngryDuck - 2 months ago 28
Swift Question

Share a video to Facebook

I'm writing a simple test app to upload a video to Facebook from iOS.

I'm finding it quite hard to find examples/tutorials online for how to do this with Swift since all the documentation for the FacebookSDK is in Objective-C.

I have this so far which puts a share button on my UI but it looks disabled, from what I have read this is because there is no content set but I can't see how that is possible.

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)


my
getVideoURL()
functions returns a NSURL which definitely contains the url to the video (I have printed it to console and checked it points to a video I have stored in a folder earlier).

I looked at the social framework and its a shame I can't use it because it seems it is only used for sharing photos and not able to share videos.

I'm using latest versions of Xcode, Swift and Facebook SDK.

Answer

@AngryDuck

Try this code it will work

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")
        }
    })
}
Comments