Share Facebook Video

I am writing a simple test application to upload videos to Facebook from iOS.

I find it difficult to find examples / tutorials online on how to do this with Swift, since all the documentation for FacebookSDK is in Objective-C.

I have it so far that puts the sharing button in my user interface, but it looks disabled, from what I read, it’s because there is no content set, but I don’t see how it 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 return an NSURL that definitely contains the URL of the video (I typed it on the console and checked that it points to the video that I saved earlier in the folder).

I looked at the social structure, and I can’t use shame, because it seems that it is used only for sharing photos and does not allow video sharing.

I use the latest versions of Xcode, Swift and the Facebook SDK.

+7
ios swift swift2
source share
4 answers

@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") } }) } 
+3
source share

converts to fast 3 and 4: - try

 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) } } 
+9
source share

I did this in lens c, and I had a similar problem. Not sure if this will work well for quick.

The problem was that if for some reason the video had to be saved in NSDocumentDirectory or NSTemporaryDirectory so that it could work. Not if this is your case.

Although you can use FBSDKGraphRequest in the worst case with / me / Videos

I came to see how this video sharing method that you are trying to use is to get links directly from the photo library. As you can see in this example

Currently there is only an example in objective-c , hope this helps you

 - (void)uploadVideoWithoutSettingsCredentials:(NSURL *)videoUrl withTitle:(NSString *)title withDesc:(NSString *)desc withPrivacy:(NSString *)privacy { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH,0), ^{ NSError * error = nil; NSData * videoData = [NSData dataWithContentsOfURL:videoUrl options:0 error:&error]; NSMutableDictionary *params = [NSMutableDictionary dictionaryWithCapacity:4L]; [params setObject:videoData forKey:[NSString stringWithFormat:@"%@.MOV",title]]; [params setObject:desc forKey:@"description"]; NSString *privacyString = @"EVERYONE"; if (privacy.length > 0) privacyString = privacy; NSDictionary *privacyDict = [[NSDictionary alloc] initWithObjectsAndKeys: privacyString, @"value", nil]; [params setObject:[Utils objectToJsonString:privacyDict] forKey:@"privacy"]; dispatch_async(dispatch_get_main_queue(), ^{ [[[FBSDKGraphRequest alloc] initWithGraphPath:@"/me/videos" parameters:params HTTPMethod:@"POST"] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { NSLog(@"RESPONSE!!! /me/videos"); NSLog(@"result %@",result); NSLog(@"error %@",error); dispatch_async(dispatch_get_main_queue(), ^{ if (!error) { [[[UIAlertView alloc] initWithTitle:@"Success" message:@"Your clip was posted!" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil] show]; } else { [[[UIAlertView alloc] initWithTitle:@"Error!" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil] show]; } }); }]; }); }); } 

You can upgrade to sintax fast using Xcode's autocomplete functionality.

You can help yourself a bit with the swift syntax with this

+1
source share

I used this code in my application and it works fine for me.

This code is for Objective-C , but you can use it in swift after converting it to fast syntax.

Hope this helps you.

Try using the code below.

 -(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"); }]; } 
+1
source share

All Articles