I'm trying to do something that seems to be an order of magnitude more difficult than necessary.
I've got an iOS app that is written against a Parse backend. Our app's users can create items to store and they can sometimes generate alerts for these items.
I want to post a simple alert for a user to Facebook.
I've read quite a bit on the Facebook website about integrating Facebook into iOS, but their documentation is confusing and I've seen more than one person on StackOverflow mention that their iOS code is at least sometimes flawed.
Basically, the custom story will include the following items:
Story Title
Story Description (usually not very much text in here)
One or more pictures (I'd settle for 1, but I'd love to be able to post multiples)
If possible, a user-supplied GPS location.
I will admit up front that I'm a developer who is learning Facebook for work purposes, and I am far from being an expert on Facebook.
When I play around in the Facebook app, I can add a status update that can include:
Text
Picture(s)
Friends
What I'm doing
Where I am (or some location)
However, in the Facebook SDK, there appears to be only the following 2 options for posting a status update:
startForPostStatusUpdate:completionHandler:
startForPostStatusUpdate:place:tags:completionHandler:
If I'm missing something there, please let me know.
Secondly, I'm trying to follow Facebook's documentation and sample code to do the following sequence of events:
Upload and stage an image resource
Create an object (in my app, I've registered an "alert" object)
Create an action (in my app, I've registered a "post" action)
Here's the code that I've pieced together from Facebook's website (comments are mostly from other people's code, not my own):
- (void)postToFacebookWithAlert:(PFObject *)alert image:(UIImage *)image {
// Stage the image
[FBRequestConnection startForUploadStagingResourceWithImage:image completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (error != nil) {
// See: https://developers.facebook.com/docs/ios/errors
} else {
// Package image inside a dictionary, inside an array like we'll need it for the object
NSArray *image = #[#{#"url": [result objectForKey:#"uri"], #"user_generated" : #"true" }];
// Create an object
NSString *caption = // user-generate content
NSMutableDictionary<FBOpenGraphObject> *alert;
alert = [FBGraphObject openGraphObjectForPostWithType:#"<myapp>:alert"
title:caption
image:image
url:nil
description:alert[#"subtitle"]];
alert.provisionedForPost = YES;
// Post custom object
[FBRequestConnection startForPostOpenGraphObject:alert completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if (error != nil) {
// An error occurred
} else {
NSMutableDictionary<FBGraphObject> *post = [FBGraphObject graphObject];
post[#"alert"] = result[#"id"];
[FBRequestConnection startForPostWithGraphPath:#"me/<myapp>:post" graphObject:post completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
NSLog(#"startForPostWithGraphPath:graphObject:completionHandler:");
if (error != nil) {
// An error occurred
NSLog(#"Encountered an error posting to Open Graph: %#", error);
} else {
NSLog(#"OG story posted, story id: %#", result[#"id"]);
}
}];
}
}];
}
}];
}
I've actually gotten this code to work one time. I'm logged in as my own personal Facebook account. If I go to my activity log, I can see the items that I'm trying to post. If I go to my photos, I can see the photos that I'm trying to upload. If I view my profile from my wife's app/account, it doesn't show up.
Does anyone have working code that does what I want?
Maybe you need to set "fb:explicitly_shared" to "true" and it should be reviewed by Facebook.
After your review pass, you can add the option like below.
[post setObject:#"true" forKey:#"fb:explicitly_shared"];
Related
I am attempting to share a generated image through the facebook app with the FBDialog class. The idea is to allow tagging of friends and locations so the SLComposeViewController isn't quite as ideal (though I am falling back to that). The problem is that after it opens the facebook app and the users presses share, it never actually shares. It gets stuck at the loading screen and the bar keeps starting back from 0%, going past 100%, then back to 0.
I'm using the following code. I'm not sure if this is a problem with my implementation or with the facebook app itself.
if ([FBDialogs canPresentShareDialogWithPhotos]) {
FBPhotoParams *params = [[FBPhotoParams alloc] init];
UIImage *img = [self generatePrayerImage];
params.photos = #[img];
[FBDialogs presentShareDialogWithPhotoParams:params
clientState:nil
handler:^(FBAppCall *call,
NSDictionary *results,
NSError *error) {
if (error) {
NSLog(#"Error: %#",
error.description);
} else {
NSLog(#"Success!");
}
}];
}
Thanks.
I was able to fix this by setting the correct facebook app name in my info.plist. For some reason there must have been a small difference between the one I set on developer.facebook.com and my app.
I want to integrate Facebook like Button with the new feature FBLikeControl. On the facebook page they have written it is only for developement and not for publication on the AppStore.
Here is the Tutorial from facebook https://developers.facebook.com/docs/ios/like-button/
When I want to build an archive with my code I get the error message "Use of undeclared identifier FBBetaFeaturesLikeButton".
Is there a way to solve this problem or if there's any other alternative solution, I would appreciate that.
Here is my code:
[FBSettings enableBetaFeature:FBBetaFeaturesLikeButton];
[FBSettings enablePlatformCompatibility:NO];
FBLikeControl *like = [[FBLikeControl alloc] init];
like.frame = CGRectMake((self.frame.size.width-like.frame.size.width)/2, facebookY, 60, 20);
like.objectID = #"https://www.facebook.com/pages/abcdefgh/123456789?ref=tn_tnmn";
[self addSubview: like];
This link says:
FBLikeControl is a preview feature and may not be deployed to the App
Store. Only developers and testers of your Facebook app will be able
to use the Like Button.
is the reason you are unable to built the archive.
There are several ways to implement like, the one which worked for me is as follows:
[FBRequestConnection startWithGraphPath:#"/object-id_to_like/likes"
parameters:nil
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
// Handle error here
}];
Here is the Link
EDIT: It is pretty simple, you just have to replace FBLikeControl code in your project with the above method call replacing object-id_to_like to the actual post-id/ object-id. What the method does is it POSTS a like on corresponding object/post.
In case you have problem to fetch the object id of the object/post you can fetch it using same method with the HTTPMethod parameter set to #"GET". Following code will return posts from your timeline:
[FBRequestConnection startWithGraphPath:#"/me/home"// you can change this to #"/me/feed" if required
parameters:nil
HTTPMethod:#"GET"
completionHandler:^(FBRequestConnection *connection, id result, NSError *error ) {
if (!error){
// Fetch Post-id/Object-id here
}else{
// handle error here
}
}];
I have used FacebookSDK.framework for Facebook integration in my application. I have to like one facebook page from application. I have used following code for liking page.
NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:#"https://www.facebook.com/demoappname"
, #"object",
nil
];
/* make the API call */
[FBRequestConnection startWithGraphPath:#"me/og.likes"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(
FBRequestConnection *connection,
id result,
NSError *error
)
{
NSDictionary *currentResult= [(NSArray *)[result data] objectAtIndex:0];
if(!error)
{
NSLog(#"there is no error");
}
else
{
NSLog(#"There is something wrong at here.");
}
}];
But I am not clear what I have to pass in "object" parameter. Can anybody help to what I am doing wrong at here.
Thanks,
If you read this documentation, it says-
The og.likes action can refer to any open graph object or URL, except for Facebook Pages or Photos.
So, liking a page with Graph API isn't possible.
The only thing you can do- add a Like Button to the page in your app.
I am creating a photo app for iOS. I have a custom action-type pair, "take a photo" defined in my app. I first create the "photo" object (I have a user-generated image, and I'm first uploading the photo and getting the staging URI. this code is after that):
NSMutableDictionary *imageData = [NSMutableDictionary dictionaryWithDictionary:
#{
#"url" : stagingURL,
#"user_generated" : #"true",
}];
NSMutableDictionary<FBOpenGraphObject> *object =
[FBGraphObject openGraphObjectForPostWithType:#"tonerapp:photo"
title:#"photo"
image:imageData
url:#"http://tonerapp.info"
description:title];
//post the object to facebook
[FBRequestConnection startForPostOpenGraphObject:object completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(result != nil){
NSString* resultID = [result objectForKey:#"id"];
NSMutableDictionary<FBGraphObject> *actionDict = [FBGraphObject graphObject];
actionDict[#"photo"] = resultID;
[self postTakeActionToFacebookWithOpenGraphObject:actionDict completion:action];
}else{
action(NO);
}
}];
After posting the object, I publish the "take" action in the completion handler as seen above. Here is the code for posting the "take" action with the "photo" object:
-(void)postTakeActionToFacebookWithOpenGraphObject:(NSMutableDictionary<FBGraphObject>*)ogObject completion:(booleanAction)action{
if(self.selectedPlace != nil){
ogObject[#"place"] = [self.selectedPlace id];
}
if(self.taggedFriends != nil && self.taggedFriends.count > 0){
ogObject[#"tags"] = self.taggedFriends;
}
[FBRequestConnection startForPostWithGraphPath:#"me/tonerapp:take"
graphObject:ogObject
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error) {
if(result != nil){
NSLog(#"oldu");
action(YES);
}else{
action(NO);
}
}];
}
Here is the result:
First a "post" action is created, then the "take" action is applied to the photo. I need to get rid of the "post" action. More weirdly, My Open Graph dashboard looks like this:
I delete the "Object" type and the "Post" action, but every other time, they are added to my actions again. I am suspecting that something that I'm doing wrong would be the cause of both "default post creation action" problem and the default pairs being added to my dashboard. I go to my App's settings and change the subscribe (creation) action of my photo type:
Then I try posting my photo again. Even though my creation action is "take", a "post" action is created:
But I'm not able to create any "take" action this time. It fails with this response:
body = {
error = {
code = 1611231;
message = "A post action for this object already exists.";
type = Exception;
};
};
code = 500;
Shouldn't this be just the opposite? Shouldn't I be messing it up if I don't set my default creation action to my custom action? I've also tried creating the photo object "on-the-fly" (without "post"ing it first and sending the object JSON data directly in the "take" action, which appears to be the natural way of doing it), but it also fails just as in this example: Example Facebook object code.. Am I missing something, or is there something seriously wrong with the Open Graph API?
Thanks,
Can.
UPDATE: I have July 2013 migrations enabled.
If you create an (user-owned) open graph object through the Object API (which is what you're using with startForPostOpenGraphObject, and also through the share dialog), then there will be a "Post" action that's created. The only way around it is to host your own open graph objects.
Think of it this way, what you're really doing is actually 2 separate operations.
You create an open graph object (i.e. you "posted" an object)
You create an open graph action (i.e. your "take" action) which acts on the object
Each of them will result in a separate activity in the activity log. This separation is important because you can reuse the same object in a different action, and it has to show up in the user's activity log because it's a user owned object.
See also https://developers.facebook.com/docs/opengraph/using-object-api/
#can Poyrazoglu
You should you this methods if you don't to create story for each post. If the there are multiple images from the same post, it will group the image and make the album on the whole only instead of creating the separate post.
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:#"your custom message" forKey:#"message"];
[params setObject:UIImagePNGRepresentation(_image) forKey:#"picture"];
_shareToFbBtn.enabled = NO; //for not allowing multiple hits
[FBRequestConnection startWithGraphPath:#"me/photos"
parameters:params
HTTPMethod:#"POST"
completionHandler:^(FBRequestConnection *connection,
id result,
NSError *error)
{
if (error)
{
//showing an alert for failure
[self alertWithTitle:#"Facebook" message:#"Unable to share the photo please try later."];
}
else
{
//showing an alert for success
[UIUtils alertWithTitle:#"Facebook" message:#"Shared the photo successfully"];
}
_shareToFbBtn.enabled = YES;
}];
i'm using facebook ios sdk 3.5, now following the instructions found here https://developers.facebook.com/docs/tutorials/ios-sdk-games/open-graph/ and the code found in the FriendSmasher, i am trying to post a score. However, i'm not seeing any results on my feed.
Thanks
this is my test code for our game:
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys: #"100", #"score", nil];
NSLog(#"Posting new score of 100");
[FBRequestConnection startWithGraphPath:[NSString stringWithFormat:#"%llu/scores", myFBID] parameters:params HTTPMethod:#"POST" completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
if(error){
NSLog(#"AN ERROR HAS OCCURED: %#", error.debugDescription);
}
else
NSLog(#"Score posted");
}];
the request permissions is done using the deprecated function openActiveSessionWithPermissions with email and publish_actions passed
You can check your Activity Log on the desktop for score stories. You'll see stories if you get a high score for example, so you can test that. The fact that a score was posted does not show up in News Feed. The stories that may show up when a score is posted are stories like passing stories (you passed a friend) or high score stories. You can always check these types of stories in your Activity Log.