简体   繁体   中英

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

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

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

3. Set the Relationship Between Action & Object

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

The original poster is using the namespace:property_name format when what you really need is just the property_name for that key. This could be the fix for the OP.

4. Set the Delegate, Look For Errors

The OP is not taking advantage of the FBSDKSharingDelegate features. It reports errors and will help you fix the problem:

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

And implement the delegate methods in 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. 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.

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.

  2. Make sure you have requested the publish_actions permission: 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

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. So make sure you are sharing from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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