简体   繁体   English

如何在Facebook iOS SDK中发布或共享?

[英]How to post or share in Facebook iOS SDK?

I'm trying to post a message or share a photo in my fb using FacebookSDK. 我正在尝试使用FacebookSDK在我的fb中发布消息或共享照片。 I already follow the steps on how to install it. 我已经按照有关如何安装它的步骤进行了操作。 I'm done applying the login tutorial, but the problem now is how to post a message or share it into my FB using my app. 我已经完成了登录教程的应用,但是现在的问题是如何使用我的应用程序发布消息或将其共享到我的FB中。 There are a lot of tutorial like this one . 有很多像本教程的一个 I think this is already old for FacebookSDK. 我认为这对于FacebookSDK来说已经很老了。 Sorry If I'm so noob for this. 抱歉,如果我对此很陌生。 I know this not the right page to post this, but I don't have any idea now. 我知道这不是发布此信息的正确页面,但是我现在不知道。 If you have a tutorial on how to post or share. 如果您有关于如何发布或共享的教程。 Please give me link. 请给我链接。 If ever, can you provide steps for this. 如果可以的话,您可以提供步骤吗?

I already try this , but It doesn't post or share. 我已经尝试过 ,但是没有发布或分享。

UPdate 更新

this is my code 这是我的代码

import "ShareController.h"
#import <FBSDKShareKit/FBSDKShareKit.h>
#import <FBSDKMessengerShareKit/FBSDKMessengerShareKit.h>


@implementation ShareController

- (void)viewDidLoad {
    [super viewDidLoad];

    FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
    content.contentURL = [NSURL URLWithString:@"https://developers.facebook.com"];

    FBSDKShareButton *button = [[FBSDKShareButton alloc] init];
    button.shareContent = content;
    [self.view addSubview:button];
}

- (IBAction)shareButton:(id)sender {


    UIImage * image = [UIImage imageNamed:@"sample.png"];

    FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc] init];
    photo.image = image;
    photo.userGenerated = YES;
    FBSDKSharePhotoContent *content = [[FBSDKSharePhotoContent alloc] init];
    content.photos = @[photo];

    FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
    dialog.fromViewController = self;
    dialog.shareContent= content;
    dialog.mode = FBSDKShareDialogModeShareSheet;
    [dialog show];
}

here's the code for my login. 这是我的登录代码。 I think this is simple code 我认为这是简单的代码

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
    loginButton.center = self.view.center;
    [self.view addSubview:loginButton];
    // Do any additional setup after loading the view, typically from a nib.
}

You did not set permissions, to set the permissions: 您没有设置权限,而是要设置权限:

FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.center = self.view.center;
[loginButton logInWithPublishPermissions: @[@"publish_actions"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
if (error) {
  NSLog(@"Process error");
} else if (result.isCancelled) {
  NSLog(@"Cancelled");
} else {
  NSLog(@"Logged in");
  }
}];

Also it is a good practice to check if user has granted the permission or not in your IBAction method: 同样好的方法是检查用户是否在您的IBAction方法中授予了权限:

- (IBAction)shareButton:(id)sender {
if ([[FBSDKAccessToken currentAccessToken] hasGranted:@"publish_actions"])
{
    // code to share 
}
}

这些框架应包括在内

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM