简体   繁体   English

Facebook iOS SDK 4.0上的显式共享

[英]Explicit Sharing on Facebook iOS SDK 4.0

I want to explicitly share an open graph action on Facebook using the new iOS 4.0 SDK. 我想使用新的iOS 4.0 SDK在Facebook上明确分享一个开放的图形动作。 The following does not work. 以下不起作用。 It shows up on my Activity Log, but not on my Timeline. 它显示在我的活动日志中,但不显示在我的时间轴上。

// Construct an FBSDKSharePhoto
FBSDKSharePhoto *photo = [[FBSDKSharePhoto alloc] init];
photo.image = img;
photo.userGenerated = YES;

// Create an object
NSDictionary *properties = @{
                             @"og:type": @"theprose:post",
                             @"og:title": post.title,
                             @"og:description": post.text,
                             @"og:caption": @"A fresh picked Prose for you.",
                             @"og:url": post.url,
                             @"fb:explicitly_shared": @"true"
                             };
FBSDKShareOpenGraphObject *object = [FBSDKShareOpenGraphObject objectWithProperties:properties];

// Create an action
FBSDKShareOpenGraphAction *act = [[FBSDKShareOpenGraphAction alloc] init];
act.actionType = @"theprose:share";
[act setObject:object forKey:@"theprose:post"];
[act setPhoto:photo forKey:@"image"];

// Create the content
FBSDKShareOpenGraphContent *content = [[FBSDKShareOpenGraphContent alloc] init];
content.action = act;
content.previewPropertyName = @"theprose:post";

[[FBSDKShareAPI shareWithContent:content delegate:nil] share];

Piecing together the answers from this page, this is what worked for me: 拼凑这个页面的答案,这对我有用:

1. Check the Explicitly Shared box 1.选中“明确共享”框

Go to your open graph settings, action types and then choose your custom action. 转到打开的图表设置,操作类型,然后选择自定义操作。 Scroll down to capabilities section and make sure that "explicitly share" box is checked. 向下滚动到功能部分,确保选中“显式共享”框。

功能设置

2. Set fb:explicitely_shared On Action 2.设置fb:explicitely_shared On Action

[action setString:@"true" forKey:@"fb:explicitly_shared"];

3. Set the Relationship Between Action & Object 3.设置动作和对象之间的关系

Make sure that you know the correct to connect your action to your object. 确保您知道将操作连接到对象的正确方法。 In this case the key is "article": 在这种情况下,关键是“文章”:

[FBSDKShareOpenGraphAction actionWithType:@"mynamespace:share" object:object key:@"article"]

You can find that key by looking at the Action Type you created in your FB app's Open Graph settings. 您可以通过查看在FB应用程序的Open Graph设置中创建的Action Type来找到该键。 When you connected the Action with the Object via a Story, a new key for that relationship appears on the Action's page under the Property Name column. 当您通过Story将Action与Ob​​ject连接时,该关系的新键将显示在Property Name列的Action页面上。

The original poster is using the namespace:property_name format when what you really need is just the property_name for that key. 原始海报使用namespace:property_name格式,而您真正需要的只是该键的property_name。 This could be the fix for the OP. 这可能是OP的修复。

4. Set the Delegate, Look For Errors 4.设置代理,查找错误

The OP is not taking advantage of the FBSDKSharingDelegate features. OP没有利用FBSDKSharingDelegate功能。 It reports errors and will help you fix the problem: 它会报告错误并帮助您解决问题:

[[FBSDKShareAPI shareWithContent:content delegate:self] share];

And implement the delegate methods in self: 并在self中实现委托方法:

#pragma mark - FBSDKSharingDelegate

- (void) sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results {

    NSLog(@"Facebook sharing completed: %@", results);
}

- (void) sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error {

    NSLog(@"Facebook sharing failed: %@", error);
}

- (void) sharerDidCancel:(id<FBSDKSharing>)sharer {

    NSLog(@"Facebook sharing cancelled.");
}

For Reference, Here's My Working Code 供参考,这是我的工作代码

    // Create the object
NSMutableDictionary *properties = [NSMutableDictionary dictionaryWithDictionary: @{
                                                                                   @"og:type": @"mynamespace:article",
                                                                                   @"og:title": myModel.title,
                                                                                   @"og:description": @"myModel.description",
                                                                                   @"og:url": @"http://mywebsite.com"
                                                                                   }];


NSURL *imageURL = [myModel getImageURL];
if (imageURL) {

    FBSDKSharePhoto *photo = [FBSDKSharePhoto photoWithImageURL:imageURL userGenerated:NO];
    [properties setObject:@[photo] forKey:@"og:image"];
}

FBSDKShareOpenGraphObject *object = [FBSDKShareOpenGraphObject objectWithProperties:properties];

// Create the action
FBSDKShareOpenGraphAction *action = [FBSDKShareOpenGraphAction actionWithType:@"mynamespace:share" object:object key:@"article"];
[action setString:@"true" forKey:@"fb:explicitly_shared"];

// Create the content
FBSDKShareOpenGraphContent *content = [[FBSDKShareOpenGraphContent alloc] init];
content.action = action;
content.previewPropertyName = @"article";

// Share the content
FBSDKShareAPI *shareAPI = [[FBSDKShareAPI alloc] init];
shareAPI.shareContent = content;
shareAPI.delegate = self;

[shareAPI share];

The same problem but works fine with FBSDKShareDialog under the same account. 同样的问题但在同一帐户下使用FBSDKShareDialog可以正常工作。 So the problem is in something else. 所以问题出在其他方面。

I got the same problem. 我遇到了同样的问题。 I mean the reason in your code is, that you set the fb:explicitly_shared property in the object not in the concerning action. 我的意思是你的代码中的原因是你在对象中设置fb:explicit_shared属性而不是在关注动作中。

The following code should solve the issue. 以下代码应该可以解决问题。

[action setString:@"true" forKey:@"fb:explicitly_shared"];
  1. If your Facebook app is not public, make sure you have the test account added as a Test account, in the Roles section. 如果您的Facebook应用程序不公开,请确保在“角色”部分中将测试帐户添加为测试帐户。

  2. Make sure you have requested the publish_actions permission: https://developers.facebook.com/docs/facebook-login/ios/permissions 确保您已请求publish_actions权限: https//developers.facebook.com/docs/facebook-login/ios/permissions

  3. You have no delegate assigned to your call, there is a high chance you are not getting the error returned by Facebook 您没有为您的电话分配代表,很有可能您没有收到Facebook返回的错误

Go to your open graph settings, action types and then choose your custom action. 转到打开的图表设置,操作类型,然后选择自定义操作。 Scroll down to capabilities section and make sure that "explicitly share" box is checked. 向下滚动到功能部分,确保选中“显式共享”框。

在此输入图像描述

它应该不起作用,因为在4.1版本之前的FB版本中存在一个错误: https//developers.facebook.com/bugs/943100005734949/ https://developers.facebook.com/bugs/1563738347248670但是SDK版本4.1这个错误是修复的,但与FSDKShareDialog相比,自定义故事/动作/对象的内容以非常不同的方式显示(((

One more check to the checklist: FBSDKShareApi only works on the main thread. 再检查清单:FBSDKShareApi仅适用于主线程。 So make sure you are sharing from there. 因此,请确保您从那里分享。

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

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