xcode 使用 FBSDK 4.x for iOS 在 facebook 上发布图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30317934/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 07:10:10  来源:igfitidea点击:

Post image on facebook using FBSDK 4.x for iOS

iosxcodefacebookfacebook-sdk-4.0

提问by Esha

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

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

  • Core
  • Sharing
  • Login.
  • 分享
  • 登录。

To share any image I used FBSDKSharePhotoClass. But it has method

为了分享我使用的任何图像FBSDKSharePhoto类。但它有方法

[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.

我想用图像作为字符串和文本添加标题。任何人都可以帮助我,以便我可以使用 FB 的新 sdk 发布带有图像的标题。

Thanks in Advance.

提前致谢。

采纳答案by NANNAV

Assign your Caption String to caption Key value

将您的标题字符串分配给标题键值

@property (nonatomic, copy) NSString *caption;

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

回答by Shiva Kumar

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.

正如@NANNAV 发布的那样,它会起作用,但如果您想在没有任何 FB 对话框屏幕的情况下静默分享,请使用如下所示的“shareWithContent”,但您需要提交您的 Facebook 应用程序以供审核。

Code in Obj-c

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

Swift 中的代码

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

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

FBSDKShareAPI.shareWithContent(content, delegate: self)

回答by G. Hazarath Reddy

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

添加以下代码以使用 Facebook SDK 4.x 将文本共享到 Facebook

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