Explicit Sharing on Facebook iOS SDK 4.0 - ios

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

If your Facebook app is not public, make sure you have the test account added as a Test account, in the Roles section.
Make sure you have requested the publish_actions permission:
https://developers.facebook.com/docs/facebook-login/ios/permissions
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.

It shouldn't work because there is a bug in FB with SDK versions before 4.1:
https://developers.facebook.com/bugs/943100005734949/
https://developers.facebook.com/bugs/1563738347248670
but with SDK version 4.1 this bug is fixed but the content for custom story/action/object was shown in very different way in compare with FSDKShareDialog (((

One more check to the checklist: FBSDKShareApi only works on the main thread. So make sure you are sharing from there.

Related

Open native iOS app from Facebook feed

I'm sharing a FBSDKShareOpenGraphObject with an FBSDKShareOpenGraphAction of type "video.watches" using the Facebook's SDK. Everything goes fine, the post appears on the Facebook feed but if I tap on the application's name I get an error. This is the code I'm using for sharing:
FBSDKShareOpenGraphObject *object = [FBSDKShareOpenGraphObject objectWithProperties:#{#"og:type": #"video.other",
#"og:title": #"TV Show",
#"og:image": #"http://cdn1.tnwcdn.com/wp-content/blogs.dir/1/files/2013/02/tv-set.jpg",
#"og:url": show_url}];
FBSDKShareOpenGraphAction *action = [[FBSDKShareOpenGraphAction alloc] init];
[action setActionType:#"video.watches"];
[action setObject:object forKey:#"video"];
[action setNumber:#(7200000) forKey:#"expires_in"];
FBSDKShareOpenGraphContent *content = [[FBSDKShareOpenGraphContent alloc] init];
[content setPreviewPropertyName:#"video"];
[content setAction:action];
_shareDialog = [[FBSDKShareDialog alloc] init];
_shareDialog.fromViewController = [self.delegate collectionViewControllerForCell:self];
_shareDialog.shareContent = content;
_shareDialog.delegate = self;
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:#"fbauth2://"]]){
_shareDialog.mode = FBSDKShareDialogModeNative;
} else {
_shareDialog.mode = FBSDKShareDialogModeAutomatic;
}
[_shareDialog show];
The result:
But when I tap on TV Channel (the boxed one) I get this error:
I'd like to open the native application when I tap on that text, in the same way Instagram opens when you tap on it's name in the "X posted a photo on Instagram" on a photo. I configured the Bundle ID and the iPhone Store ID in application's settings, on developer.facebook.com, but no success. What I am missing?
Go to developers.facebook.com and make sure your app is not in test mode.
Go to "App Review" -> Make public?
And mark it as yes.
If you are not ready to make it public, add the facebook user you are using to the testers of the app under "Roles".

FBSDK OpenGraph issues on Object and Action

I have been trying to add an OpenGraph post for quite some time but I cannot figure out the current documentation and broken links to tutorials.
Relevant setup (values are made up):
FB namespace: "spot_app"
Open Graph Object Types: "Spot", inherits from Place as this will include a link to a place in google maps.
I am trying to have the story: "UserX saved a Spot", followed by a user comment and business information.
Relevant code:
NSDictionary *properties = #{#"og:type":#"spot_app:Spot",
#"og:title":#"Spot OpenGraph Title",
#"og:description":#"Spot Description",
#"og:site_name":#"spotAppSite.com",
#"og:url":googleURL};
FBSDKShareOpenGraphObject *object = [FBSDKShareOpenGraphObject objectWithProperties:properties];
FBSDKShareOpenGraphAction *action = [FBSDKShareOpenGraphAction actionWithType:#"save.saves" object:object key:#"spot_app:Spot"];
FBSDKShareOpenGraphContent *content = [FBSDKShareOpenGraphContent new];
content.action = action;
content.previewPropertyName = #"spot_app:Spot";
FBSDKShareAPI *shareAPI = [FBSDKShareAPI new];
shareAPI.shareContent = content;
shareAPI.delegate = self;
[shareAPI share];
The documentation around FBSDKShareOpenGraphAction is quite poor and says several different things in several different places about how to use the "save" action.
Guidance on where I am going wrong? I currently get the error: Failed: The operation couldn’t be completed. (com.facebook.sdk.share error 201.)

SLComposeViewController post both image and url ios9

What I am trying to do and it doesn't work is the following: post an image of my choise and also an url to facebook using the built in facebook sharer, the problem is that it doesn't work to upload both, it's either picture + text and works nice or url + text and works nice, but when I combine them text+picture+url it gets the picture from the url and not my uploaded pic. Any suggestions? I am doing this on iOS9
UIImage *tableViewScreenshot = [tblFeed screenshotOfCellAtIndexPath:idx];
SLComposeViewController *fbSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[fbSheet setInitialText:#"I need your vote"];
[fbSheet addURL:[NSURL URLWithString:str]];
[fbSheet addImage:tableViewScreenshot];
The problem is that Facebook has changed some policies and by this you can't have a text or an image present as a default. That's why you're not seeing the text and photo. Here are 2 scenarios one with Facebook app installed and another one without Facebook app installed. The below one is when the Facebook app is already installed on the device.
And this one is when the Facebook app is not installed on the device
And this is my code:
SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[controller addURL:[NSURL URLWithString:#"https://www.google.com"]];
[controller addImage:[UIImage imageNamed:#"icon_circle"]];
[controller setInitialText:#"Bhavuk you're great!"];
[self presentViewController:controller animated:YES completion:nil];
So If you want to share everything, better use FB SDK.
So it seems at the moment there is no solution for this and we should find a workaround for it, maybe uploading everything first a picture to an url and then use fb sdk to point to a picture from that url + the link as an offline pic doesn't seem to work unfortunately.
Unfortunately, the problem remains even on iOS 10. An easy workaround is simply drop the URL, for example:
SLComposeViewController *fbSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];
[fbSheet setInitialText:#"I need your vote"];
[fbSheet addImage:tableViewScreenshot];
It's not a direct answer to the OP but in case of what you only care is showing text and image while sharing something, here is a possible solution:
If the image and the text that you want to show within your SlComposeView is the same as the information that exist within the web site that you are going to share; you don't need to do anything special at the iOS side at all.
If you are the one who also created the web page that you're sharing URL of, should you have proper Open Graph META Tags at the page, SLComposeView of type Facebook will show image and text which were set at the webpage within your app automagically.
For some information about Facebook Open Graph Markup; see https://developers.facebook.com/docs/sharing/webmasters/#markup
I have had facebook sharing working fine in my ios app when fb not install. and i have installed fb to use the latest api (4.7.x) and now sharing doesnt work at all. I check that I have publish_actions permission (which I do prior to this method being called, I have 'expicitly shared' checked in open graph settings, action types, capabilities. I am validating the content (I dont get an error) and have a delegate, none of its methods get called.
-(void)shareWithFacebook:(NSString *)message
{
if ([[FBSDKAccessToken currentAccessToken] hasGranted:#"publish_actions"])
{
NIDINFO(#"Facebook sharing has publish_actions permission");
}
else
{
FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init];
[loginManager logInWithPublishPermissions:#[#"publish_actions"]
handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
NIDERROR(#"Facebook sharing getting publish_actions permission failed: %#", error);
}
];
}
NSMutableDictionary *properties = [NSMutableDictionary dictionaryWithDictionary: #{
#"og:type": #"article",
#"og:title": #"Bloc",
#"og:description": message,
#"og:url": #"http://getonbloc.com/download"
}];
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;
NSError *error;
if([shareAPI validateWithError:&error] == NO)
{
NIDERROR(#"Facebook sharing content failed: %#", error);
}
[shareAPI share];
}
#pragma mark - FBSDKSharingDelegate
- (void) sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results
{
NIDINFO(#"Facebook sharing completed: %#", results);
}
- (void) sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error
{
NIDERROR(#"Facebook sharing failed: %#", error);
}
- (void) sharerDidCancel:(id<FBSDKSharing>)sharer
{
NIDINFO(#"Facebook sharing cancelled.");
}

Sharing Facebook open graph action succeeds, but post isn't visible

I'm migrating a legacy app from facebook iOS SDK 2.x to 3.x, and I'm having trouble getting my open graph object to share successfully.
Here's the code I have to share the object:
NSMutableDictionary *properties = [#{
#"og:type": #"my-app:my-object",
#"og:title": #"Title",
#"og:description": #"text",
} mutableCopy];
if(photoUrl){
FBSDKSharePhoto *photo = [FBSDKSharePhoto photoWithImageURL:[NSURL URLWithString:photoUrl] userGenerated:NO];
properties[#"og:image"] = #[photo];
}
FBSDKShareOpenGraphObject* graphObject = [FBSDKShareOpenGraphObject objectWithProperties:properties];
FBSDKShareOpenGraphAction *action = [[FBSDKShareOpenGraphAction alloc] init];
action.actionType = #"my-app:my-action";
[action setObject:graphObject forKey:#"my-object"];
FBSDKShareOpenGraphContent *content = [[FBSDKShareOpenGraphContent alloc] init];
content.action = action;
content.previewPropertyName = #"my-object";
FBSDKShareAPI* shareAPI = [FBSDKShareAPI shareWithContent:content delegate:[ShareDelegate theShareDelegate]];
if(![shareAPI share]){
NSLog(#"![shareAPI share]");
onFail(nil);
}else{
NSLog(#"Share succeeded");
onFinish();
}
The share action appears to succeed. The delegate method -(void)sharer:(id<FBSDKSharing>)sharer didCompleteWithResults:(NSDictionary *)results is called with this results dictionary:
{
completionGesture = post;
postId = 1608774589402167;
}
But the post doesn't appear in the test user's news feed. Also, on the open graph -> action types section of the app dashboard at https://developers.facebook.com I see this error message:
Your Open Graph action failed to publish on any of the Platforms you submitted. Make sure the action is working properly by publishing the action with a test user before resubmitting.
This graph action was being shared successfully using the Facebook 2.x SDK for iOS.
What could the point of failure be?
I'm not sure if you still need the answer here, but to get the action to show in the users' timeline you need to set the key on your action:
[action setString:#"true" forKey:#"fb:explicitly_shared"];
This requires configuration for your custom action as well (check boxes at the bottom of the page).

iOS how to publish action on custom object Graph Api - Facebook

I need to publish action
I got success in publishing 'liking the link' from graph API.
e.g 'abc likes www.abc.com'
now I am stuck at 'liking custom object' e.g 'abc likes toy1'
and also to get total number of likes.
This is sample code which is not working
NSDictionary *properties = #{
#"og:type": #"toy",
#"og:title": #"toy1",
#"og:description": #"This is a sample course."};
FBSDKShareOpenGraphObject *object = [FBSDKShareOpenGraphObject objectWithProperties:properties];
FBSDKShareOpenGraphAction *action = [[FBSDKShareOpenGraphAction alloc] init];
action.actionType = #"og.likes";
[action setObject:object forKey:#"toy"];
FBSDKShareOpenGraphContent *content = [[FBSDKShareOpenGraphContent alloc] init];
content.action = action;
content.previewPropertyName = #"toy";
FBSDKShareAPI *shareAPI = [[FBSDKShareAPI alloc] init];
// optionally set the delegate
shareAPI.delegate = self;
shareAPI.shareContent = content;
[shareAPI share];
Edit : My code is working by changing
toy
to
appnamespace:toy
and
[action setObject:object forKey:#"toy"]; // to [action setObject:object forKey:#"object"];
now I want to fetch total number of likes on this custom object
A "og:type" of "toy" is not valid. It should either be a common OG type (like "fitness.course") or a custom one ("yourappnamespace:toy").
If you are simply liking a website or other URL, you can skip the OG object and just create the action like
FBSDKShareOpenGraphAction *action = [FBSDKShareOpenGraphAction actionWithType:#"og.likes"
objectURL:[NSURL URLWithString:#"http://www.example.com"]
key:#"og:object"];
(note that if you try to like the same thing twice it will give you an error).
You should make sure your delegate implements - (void)sharer:(id<FBSDKSharing>)sharer didFailWithError:(NSError *)error to receive error information.
You may also want to use the FBSDKLikeButton or FBSDKLikeControl instead.

Resources