Post facebook 上的图像使用 FBSDK 4.x for iOS

Post image on facebook using FBSDK 4.x for iOS

对于新的Facebook SDK 4.x (4.1) for iOS,框架有新的变化。它包括 3 个不同的框架

分享我使用的任何图像 FBSDKSharePhoto Class。但是它有方法

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

我想用图片添加标题作为字符串和文本。谁能帮我解决这个问题,这样我就可以 post 使用 FB 的新 sdk 为图像添加标题。

提前致谢。

将您的字幕字符串分配给字幕键值

@属性(非原子,复制)NSString *caption;

试试这个:

 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 发布的那样,它会起作用,但如果您想在没有任何 FB 对话屏幕的情况下静默分享,请使用 "shareWithContent",如下所示,但您需要提交您的 Facebook 应用程序以供审核。

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

Swift

中的代码
var sharePhoto = FBSDKSharePhoto()
sharePhoto.caption = "Test"
sharePhoto.image = UIImage(named: "BGI.jpg")

var content = FBSDKSharePhotoContent()
content.photos = [sharePhoto]

FBSDKShareAPI.shareWithContent(content, delegate: self)

添加以下代码以使用 Facebook SDK 将文本分享到 facebook 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");

        }

}];

*添加以下代码分享照片。

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