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

青春壹個敷衍的年華 提交于 2019-11-28 19:54:29
Joakim Engstrom

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.

We don't currently support Like through our Graph API. What you can look through is something like this : https://developers.facebook.com/docs/opengraph/actions/builtin/likes/

I’m not sure what initWithContentsOfURL does, but from the name I guess it tries to actually load content from a given URL(?).

You only have to give the URL as a text parameter – a URL is what represents an Open Graph object. Facebook will do the rest, scraping the page behind that URL and reading it’s OG meta tags, etc.

Jack Nutting

Maybe just this?

    FBRequest *requestLike = [[FBRequest alloc]initForPostWithSession:[FBSession activeSession]
    graphPath:@"me/og.likes"
    graphObject:[NSURL URLWithString:facebookLike.titleLabel.text]];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!