6

For the new Facebook SDK 4.x (4.1) for iOS had new changes with the frameworks . It includes 3 different framework as like

  • Core
  • Sharing
  • Login.

To share any image I used FBSDKSharePhoto Class. But it has method

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

I want to add caption with the image as string and text. can Anyone help me for this so I can post caption with the image using FB's new sdk.

Thanks in Advance.

Esha
  • 1,328
  • 13
  • 34

3 Answers3

7

As @NANNAV posted it will work, but if you want to share silently with out any FB Dialog screen then use "shareWithContent" as below, but you need to submit your Facebook app for review.

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)
Shiva Kumar
  • 389
  • 3
  • 22
  • Thanks for the answer but this need extended permission as well the permission from the fb app. – Esha May 19 '15 at 12:58
  • @Esha yes you need publishPermissions : "publish_actions" in order to make it work. – Shiva Kumar May 19 '15 at 13:09
  • And for this new SDK I had one more problem as unable to get Publish_action permission. It always goes into canceled block. although openUrl, active app and launch option methods are set. – Esha May 19 '15 at 13:11
  • You can get the publish and other type of permissions as you create your facebook app and try to review your permissions on that app once your permissions are Granted by facebook team you can you that app id in your ios app..keep coding – Ghanshyam Tomar May 26 '15 at 11:22
  • I am using QA version of app, but still its asking like [FBSDKAccessToken currentAccessToken] is missing publish_actions permissions – Shreesh Garg Aug 12 '15 at 13:40
  • How to attach location(placeID) with content in swift? – Jay Patel Sep 27 '17 at 06:57
5

Assign your Caption String to caption Key value

@property (nonatomic, copy) NSString *caption;

Try This :

 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];
NANNAV
  • 4,875
  • 4
  • 32
  • 50
2

Add the below code to share text to 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 below code to share photo.

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];