Facebook对象代码示例



这是我从Facebook获得的使用自定义对象的示例代码。我用一个自定义动作创建了这个,以利用Facebook的故事
Facebook文档:
https://developers.facebook.com/docs/opengraph/overview/

NSMutableDictionary<FBGraphObject> *object =
[FBGraphObject openGraphObjectForPostWithType:@"sotd_facebook:new_zombie"
                                        title:@"Sample New Zombie"
                                        image:@"https://fbstatic-a.akamaihd.net/images/devsite/attachment_blank.png"
                                          url:@"http://samples.ogp.me/191078581053171"
                                  description:@""];;
[FBRequestConnection startForPostWithGraphPath:@"me/objects/sotd_facebook:new_zombie"
                               graphObject:object
                         completionHandler:^(FBRequestConnection *connection,
                                             id result,
                                             NSError *error) {
                             // handle the result
                         }];

我很好奇如何在Facebook IOS sdk中使用此对象进行操作。我曾尝试使用以下代码,但它在创建FBRequestConnection时崩溃。

[__NSCFBoolean dataUsingEncoding:]: unrecognized selector sent to instance 0x3af00530
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFBoolean dataUsingEncoding:]: unrecognized selector sent to instance 0x3af00530'

[edit]
我已经创建了FBOpenGraphObject并使用FBRequestConnection方法startForPostOpenGraphObject:completeHandler。在完成处理程序中,我从结果中检索id,并将其与id一起放入另一个FBOpenGraphObject中。它仍然会崩溃。

NSMutableDictionary<FBOpenGraphObject> *object = [FBGraphObject
                                                  openGraphObjectForPostWithType:@"sotd_facebook:new_zombie"
                                                  title:@"Sample New Zombie"
                                                  image:@"https://fbstatic-a.akamaihd.net/images/devsite/attachment_blank.png"
                                                  url:@"http://samples.ogp.me/191078581053171"
                                                  description:@""];

[FBRequestConnection startForPostOpenGraphObject:object
                               completionHandler:^(FBRequestConnection *connection,
                                                   id result,
                                                   NSError *error) {
                                   // handle the result
                                   // handle the result
                                   if (error)
                                   {
                                       NSLog(@"Error sharing story: %@", error.localizedDescription);
                                   }
                                   else if(result != nil)
                                   {
                                       NSLog(@"Result: %@", result);
                                       NSString* resultID = [result objectForKey:@"id"];
                                       NSMutableDictionary<FBOpenGraphObject> *newObject = [FBGraphObject openGraphObjectForPost];
                                       newObject.id = resultID;

                                       [FBRequestConnection startForPostWithGraphPath:@"me/objects/sotd_facebook:new_zombie"
                                                                          graphObject:newObject                                                                        completionHandler:^(FBRequestConnection *connection,
                                                                                        id result,
                                                                                        NSError *error) {
                                                                        // handle the result
                                                                        // handle the result
                                                                        if (error)
                                                                        {
                                                                            NSLog(@"Error sharing story: %@", error.localizedDescription);
                                                                        }
                                                                        else
                                                                        {
                                                                            NSLog(@"Result: %@", result);
                                                                        }
                                                                    }];
                                   }
                               }]; 

崩溃日志:

2013-08-16 18:47:111.013僵尸走秀[3408:907]-[__NSCFBooleandataUsingEncoding:]:无法识别的选择器发送到实例0x3a1185302013-08-16 18:47:111.015僵尸走秀〔3408:907〕*终止应用程序由于未捕获异常"NSInvalidArgumentException",原因:'-[__NSCFBoolean dataUsingEncoding:]:无法识别的选择器已发送到实例0x3a118530'

我遇到了同样的问题。您首先需要发布对象,然后获取其ID,并提供对象ID而不是对象本身:

NSMutableDictionary<FBOpenGraphObject> *object = [...];
[FBRequestConnection startForPostOpenGraphObject:object completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                if(result != nil){
                    NSString* resultID = [result objectForKey:@"id"];
                    [FBRequestConnection startForPostWithGraphPath:@"me/objects/sotd_facebook:new_zombie"
                               graphObject:resultID //...

该对象首先作为对象发布到Facebook,然后您可以对该对象使用操作(new_zombie)。我也处于完全相同的情况(2013年7月启用了突破性更改),试图在Facebook上一步创建所有内容,但如果不使用独立的自托管网络服务器,我就无法实现。在我的问题中,我收到了一个关于已经发布的对象的错误,不能再发布了。如果你没有这个问题,你现在可以继续这个问题。

错误本身似乎是由对象字典中的fbsdk:create_object键(及其值1)引起的。此密钥由Facebook SDK自动添加到对象中。当我在发布之前明确删除了这个键时,它并没有在发布时抛出异常,但我关于发布对象的回答仍然适用。即使删除了该键,您也会在服务器端得到一个OAuthException,告知该对象不是"引用"。不过,我真的很高兴能被证明这是错误的。

我只是在API调用之前添加了这些行,这很好!

    object[@"create_object"] = @"1";
    object[@"fbsdk:create_object"] = @"1";

这似乎是Facebook SDK中的一个错误!它试图对布尔值进行编码

示例代码应该类似于

    NSMutableDictionary<FBGraphObject> *object =
        [FBGraphObject openGraphObjectForPostWithType:@"{YOUR NAMESPACE}:{YOUR OBJECT TYPE}"
                                                title:@"title"
                                                image:@"https://fbstatic-a.akamaihd.net/images/devsite/attachment_blank.png"
                                                  url:@"http://samples.ogp.me/192059197639963"
                                          description:@"description"];
    object[@"create_object"] = @"1";
    object[@"fbsdk:create_object"] = @"1";
    [FBRequestConnection startForPostWithGraphPath:@"me/objects/{YOUR NAMESPACE}:{YOUR OBJECT TYPE}"
                                       graphObject:object
                                 completionHandler:^(FBRequestConnection *connection,
                                                     id result,
                                                     NSError *error) {
                                     NSLog(@"%@",result);
                                 }];
}

最新更新