FBConnect facebook.stream.publish with NSDictionary problems

Carlos picture Carlos · Oct 30, 2009 · Viewed 8.3k times · Source

I have this code that can't send a Facebook request until now.

NSDictionary *firstDict = [NSDictionary dictionaryWithObjectsAndKeys:
    @"image", @"Type",
    @"http://mysite.com/image.jpg", @"src",
    @"http://mysite.com/page.html", @"href",
    nil];
NSDictionary *secondDict = [NSDictionary dictionaryWithObjectsAndKeys:
    @"image", @"Type",
    @"http://mysite.com/image.jpg", @"src",
    @"http://mysite.com/page.html", @"href",
    nil];
NSArray *mediaArray = [[NSArray alloc] initWithObjects:firstDict, secondDict, nil];

NSArray *keys = [NSArray arrayWithObjects:@"name", @"description", @"href", @"media", nil];
NSArray *objects = [NSArray arrayWithObjects:
    [NSString stringWithString:@"MyTitle"],
    [NSString stringWithString:@"My caption"],
    [NSString stringWithString:@"http://mysite.com/page.html"], mediaArray, nil];

NSDictionary *attachment = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
[[FBRequest requestWithDelegate:self] call:@"facebook.stream.publish" params:attachment];

I took this sample : http://forum.developers.facebook.com/viewtopic.php?pid=145965

But every time I run it, the console shows the following:

 {
     "api_key" = adc8d70987098sdc;
     "call_id" = 23456767;
     description = "Any user's description";
     format = XML;
     href = "http://mysite.com/page.html";
     media =     (
                 {
             Type = image;
             href = "http://mysite.com/page.html";
             src = "http://mysite.com/image.jpg";
         },
                 {
             Type = image;
             href = "http://mysite.com/page.html";
             src = "http://mysite.com/image.jpg";
         }
     );
     method = "Facebook.streamPublish";
     name = aName;
     "session_key" = "d0f98bfs89b7fg7v";
     sig = 89v0d9fv879fgv;
     ss = 1;
     v = "1.0"; }

It shows parentheses for array instead brackets, is that normal?.

Then it displays the error:

*** -[NSCFArray dataUsingEncoding:]: unrecognized selector sent to instance
     0x12fcb0 2009-10-30 13:54:27.758 GeoPixr[307:207]
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
*** -[NSCFArray dataUsingEncoding:]: unrecognized selector sent to instance 0x12fcb0

The [session resume] call returns TRUE.

Thanks in advance.

Answer

Mahmud Ahsan picture Mahmud Ahsan · Aug 23, 2010

If you're using latest iOS SDK For Facebook then using the below method you can publish image as a stream.


- (IBAction) publishStream: (id)sender {

  SBJSON *jsonWriter = [[SBJSON new] autorelease];

  NSDictionary* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
                               @"Always Running",@"text",@"http://thinkdiff.net",@"href", nil], nil];

  NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
  NSDictionary* imageShare = [NSDictionary dictionaryWithObjectsAndKeys:
                                @"image", @"type",
                                @"http://thinkdiff.net/mahmud_small.jpg", @"src",
                                @"http://thinkdiff.net", @"href",
                                nil];

  NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys:
                               @"a long run", @"name",
                               @"The Facebook Running app", @"caption",
                               @"it is fun", @"description",
                               @"http://itsti.me/", @"href",
                               [NSArray arrayWithObjects:imageShare, nil ], @"media",
                              nil];
  NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
    NSLog(attachmentStr);
  NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 kAppId, @"api_key",
                                 @"Share on Facebook",  @"user_message_prompt",
                                 actionLinksStr, @"action_links",
                                 attachmentStr, @"attachment",
                                 nil];

  [_facebook dialog: @"stream.publish"
          andParams: params
        andDelegate:self];
}

The image part should be another NSDictionary object.

NSDictionary* imageShare = [NSDictionary dictionaryWithObjectsAndKeys:
                                @"image", @"type",
                                @"http://thinkdiff.net/mahmud_small.jpg", @"src",
                                @"http://thinkdiff.net", @"href",
                                nil];

And in the attachment NSDictionary object must include imageShare object as array

[NSArray arrayWithObjects:imageShare, nil ]

This is because if you not include this as an array, Json parser avoid [] brackets as a result the publish functionality will not work. Remember the string needs to be a valid JSON string otherwise facebook api will not publish.