Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook iOS SDK 3.0, implement like action on a url?

I'm trying to implement Like via the facebook open-graph-api with the Facebook iOS SDK 3.0. Everything seems to work except the FbGraphObject and that's because I have no idea how it should look because this clearly does not work.

What I'm trying to do is to like a url posted as an object. A simple Like with via the open-graph.

The error message I get the the code below is:

The action you're trying to publish is invalid because it does not specify any 
reference objects. At least one of the following properties must be specified: object.

The code I use is this:

    FBGraphObject *objectToLike = [[FBGraphObject alloc]initWithContentsOfURL:[NSURL URLWithString:facebookLike.titleLabel.text]];

    FBRequest *requestLike = [[FBRequest alloc]initForPostWithSession:[FBSession activeSession] graphPath:@"me/og.likes" graphObject:objectToLike];

    FBRequestConnection *connection = [[FBRequestConnection alloc] init];
    [connection addRequest:requestLike
         completionHandler:
     ^(FBRequestConnection *connection, id result, NSError *error) {
         if (!error &&
             result) {

             DLog(@"NothingWentWrong");
         }

         DLog(@"MajorError: %@", error);

     }
     ];

    [connection start];

UPDATE:

Checked some more info and my guess it to use this method: https://developers.facebook.com/docs/sdk-reference/iossdk/3.0/class/FBGraphObject/#//api/name/graphObject

To somehow create an object. It's the graphObject method that I probably need to do something with. Any help at all would be appreciated.

like image 381
Joakim Engstrom Avatar asked Aug 24 '12 16:08

Joakim Engstrom


People also ask

How initialize Facebook SDK iOS?

In Xcode, click File > Swift Packages > Add Package Dependency. In the dialog that appears, enter the repository URL: https://github.com/facebook/facebook-ios-sdk. In Version, select Up to Next Major and the default option. Complete the prompts to select the libraries you want to use in your project.

What is Facebook SDK for iOS?

The Facebook SDK is what allows mobile app developers to integrate Facebook within a mobile app. SDK stands for software development kit, and it allows for a website or app to integrate with Facebook seamlessly.


1 Answers

I've actually manage to create a simple and quite dirty solution of this. The solution does not seem optimal but it's currently a working solution.

If anybody has used the explorer tool on facebook on this url: https://developers.facebook.com/tools/explorer/

You know how the URL will look like when facebook is sharing a like. It has to have the URL and an access-token. So my solution became just to disregard sending anything from the Facebook SDK and just send a post request to the same URL that I've used in the explorer tool.

There seems to be some referencing to it on the facebooks docs if you look closely and deep, but no one explains exactly how to actually make the connection, so this is my solution:

NSString *urlToLikeFor = facebookLike.titleLabel.text;

NSString *theWholeUrl = [NSString stringWithFormat:@"https://graph.facebook.com/me/og.likes?object=%@&access_token=%@", urlToLikeFor, FBSession.activeSession.accessToken];
NSLog(@"TheWholeUrl: %@", theWholeUrl);

NSURL *facebookUrl = [NSURL URLWithString:theWholeUrl];

NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:facebookUrl];
[req setHTTPMethod:@"POST"];

NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&err];
NSString *content = [NSString stringWithUTF8String:[responseData bytes]];

NSLog(@"responseData: %@", content);

If you look at the code I just take the url and puts two dynamic strings in the url, one with the object-url and one with the access token. I create a URLRequest and make it a POST request, and the response from facebook gets logged so one actually can see if the like go through or not.

There might be some performance improvements that can be done with the actual requests but I will leave it up to you if you see any slowdowns.

I'm still interested in other solutions but this is the one I will use for now.

like image 109
Joakim Engstrom Avatar answered Sep 21 '22 21:09

Joakim Engstrom