Facebook iOS SDK 3.0,在网址上实现点赞操作? [英] Facebook iOS SDK 3.0, implement like action on a url?

查看:28
本文介绍了Facebook iOS SDK 3.0,在网址上实现点赞操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Facebook iOS SDK 3.0 通过 facebook open-graph-api 实现 Like.除了 FbGraphObject 之外,一切似乎都可以工作,那是因为我不知道它应该是什么样子,因为这显然不起作用.

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.

我想要做的是喜欢作为对象发布的网址.一个简单的 Like with 通过开放图.

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

<小时>

更新:

检查了更多信息,我猜想使用这种方法:https://developers.facebook.com/docs/sdk-reference/iossdk/3.0/class/FBGraphObject/#//api/name/graphObject

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

以某种方式创建一个对象.这是我可能需要使用的 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.

推荐答案

我实际上已经设法为此创建了一个简单但相当脏的解决方案.该解决方案似乎不是最佳的,但它目前是一个可行的解决方案.

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.

如果有人在这个 url 上使用了 facebook 上的资源管理器工具:https://developers.facebook.com/tools/explorer/

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

您知道当 facebook 分享一个赞时 URL 的样子.它必须具有 URL 和访问令牌.所以我的解决方案变成了忽略从 Facebook SDK 发送任何内容,只向我在资源管理器工具中使用的相同 URL 发送一个发布请求.

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.

如果您仔细且深入地看,似乎在 facebooks 文档上有一些引用,但没有人确切地解释如何实际建立联系,所以这是我的解决方案:

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);

如果您查看代码,我只是获取 url 并在 url 中放入两个动态字符串,一个带有 object-url,另一个带有访问令牌.我创建了一个 URLRequest 并使其成为一个 POST 请求,然后来自 facebook 的响应被记录下来,因此人们实际上可以看到是否通过.

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.

这篇关于Facebook iOS SDK 3.0,在网址上实现点赞操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆