Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
like image 488
CaptainStiggz Avatar asked Dec 20 '22 05:12

CaptainStiggz


1 Answers

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.

Capabilities Settings

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];
like image 54
Tyler White Avatar answered Dec 22 '22 00:12

Tyler White