Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 545
AngryDuck Avatar asked Feb 24 '16 19:02

AngryDuck


1 Answers

converted into swift 3 and 4 :- try this

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

    }
like image 194
Arun sharma Avatar answered Sep 22 '22 15:09

Arun sharma