Example Facebook object code.

无人久伴 提交于 2019-12-22 09:40:10

问题


This is the sample code i get from Facebook for the using custom objects. I created this with a custom action to make use of the Facebook story.
Facebook Documentation:
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
                         }];

Im curious how can i use this object for an action in the Facebook IOS sdk. I have tried to use the following codes and it crash upon creating the 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]
I have created FBOpenGraphObject and use FBRequestConnection method startForPostOpenGraphObject: completionHandler. Within the completion handler, i retrieve the id from the result and put it in another FBOpenGraphObject with the id. and it still crash.

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

                                                                    }];
                                   }

                               }]; 

The crash log:

2013-08-16 18:47:11.013 ZombieBlackout[3408:907] -[__NSCFBoolean dataUsingEncoding:]: unrecognized selector sent to instance 0x3a118530 2013-08-16 18:47:11.015 ZombieBlackout[3408:907] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFBoolean dataUsingEncoding:]: unrecognized selector sent to instance 0x3a118530'


回答1:


I've ran into the same issue. You first need to post the object, then get its ID, and provide the object ID instead of the object itself:

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 //...

The object is first posted as an object to Facebook, then you can use the action (new_zombie) on the object. I'm also in the exact same situation (with July 2013 breaking changes enabled) trying to figure out to create all in one step on Facebook, but couldn't figure it out without using a seperate, self-hosted webserver. In my issue, I'm getting an error about the object being already posted and can't be posted again. If you don't have this issue, you can go on with this one now.

It seems that the error itself is caused by the fbsdk:create_object key (and its value 1) in the object dictionary. This key is automatically added by Facebook SDK into the object. When I removed this key explicitly before posting, it did not throw an exception on post, but my answer about posting the object still applies. Even if you remove that key, you'll be getting an OAuthException at the server side, telling that the object is not a "reference". I'd be really glad to be proven wrong about this, though.




回答2:


I just added these lines before API call and that works fine!

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

It seems this is a bug in Facebook SDK! it tries to encode Boolean value

Sample code should be something like this

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


来源:https://stackoverflow.com/questions/17563582/example-facebook-object-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!