Post image to facebook using FBSDK 4.x for iOS

For the new Facebook SDK 4.x (4.1) for iOS , new changes were made to the framework. It includes 3 different frames, like

  • Core
  • Sharing
  • To come in.

To share any image, I used the FBSDKSharePhoto Class. But he has a method

 [FBSDKSharePhoto photoWithImage:(*UIImage) userGenerated:(BOOL)] 

I want to add a caption with an image as a string and text. Can anyone help me with this, so I can post a caption with the image using FB new sdk.

Thanks at Advance.

+6
source share
3 answers

Assign your signature line to the title. Key value

@property (non-nuclear, copy) NSString * caption;

Try the following:

  FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc] init]; photo.image = image; photo.userGenerated = YES; photo.caption = @"Add Your caption"; FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init]; content.photos = @[photo]; [FBSDKShareDialog showFromViewController:self withContent:content delegate:nil]; 
+5
source

According to @NANNAV, this will work, but if you want to share silence with any FB dialog screen, use "shareWithContent" as shown below, but you need to send your Facebook application to view it.

Code in Obj-c

 FBSDKSharePhoto *sharePhoto = [[FBSDKSharePhoto alloc] init]; sharePhoto.caption = @"Test Caption"; sharePhoto.image = [UIImage imageNamed:@"BGI.jpg"]; FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init]; content.photos = @[sharePhoto]; [FBSDKShareAPI shareWithContent:content delegate:self]; 

Code in Swift

 var sharePhoto = FBSDKSharePhoto() sharePhoto.caption = "Test" sharePhoto.image = UIImage(named: "BGI.jpg") var content = FBSDKSharePhotoContent() content.photos = [sharePhoto] FBSDKShareAPI.shareWithContent(content, delegate: self) 
+5
source

Add the below code to share text on facebook using Facebook SDK 4.x

  FBSDKGraphRequestConnection *connection =[[FBSDKGraphRequestConnection alloc]init]; NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Your_message_here_to_share_FB", @"message", nil]; FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me/feed" parameters:params HTTPMethod:@"POST"]; [connection addRequest:request completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { if(error){ NSLog(@"Failed to share"); } else{ NSLog(@"Updated successfully"); } }]; 

* Add code to share photos below.

 FBSDKGraphRequestConnection *connection =[[FBSDKGraphRequestConnection alloc]init]; NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys: image, @"picture", @"Your_text_here",@"message", nil]; FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me/photos" parameters:params HTTPMethod:@"POST"]; [connection addRequest:request completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) { if(result) { NSLog(@"Posting the image is success, the result is%@",result); } else if(error) { NSLog(@"Error occured while posting image %@",error); } }]; [connection start]; 
+2
source

Source: https://habr.com/ru/post/987474/


All Articles