FBConnect facebook.stream.publish与NSDictionary问题

时间:2009-10-30 20:12:16

标签: iphone cocoa-touch fbconnect

我有这段代码,直到现在才能发送Facebook请求。

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

我拿了这个样本:http://forum.developers.facebook.com/viewtopic.php?pid=145965

但是每次运行它时,控制台都显示以下内容:

 {
     "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"; }

它显示数组的括号而不是括号,这是正常的吗?。

然后显示错误:

*** -[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

[session resume]调用返回TRUE。

提前致谢。

4 个答案:

答案 0 :(得分:13)

如果您使用的是最新的iOS SDK For Facebook,那么您可以使用以下方法将图片作为流发布。


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

图像部分应该是另一个NSDictionary对象。

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

在附件中,NSDictionary对象必须包含imageShare对象作为数组

[NSArray arrayWithObjects:imageShare, nil ]

这是因为如果不将其作为数组包含,则Json解析器会避免使用[]括号,因此发布功能将无效。请记住,字符串必须是有效的JSON字符串,否则facebook api将不会发布。

答案 1 :(得分:1)

我找不到用数组发送字典的方法,但还有另一个适合我的课程:

FBStreamDialog

在发送信息之前会显示一个对话框,它的委托方法可以让您了解更改。

答案 2 :(得分:0)

dataUsingEncoding:NSString方法,因此可能某些对象期望您传递数组的NSString实例。我假设FBRequest无法处理attachment字典中包含的数组。

答案 3 :(得分:0)

我修改了你的例子,但它确实有效。这是工作代码:

NSString *att = @"{\"name\":\"i\'m bursting with joy\",\"caption\": \"User rated the lolcat 5 stars\", \"description\": \"a funny looking cat\"}";
NSDictionary *attachment = [NSDictionary dictionaryWithObject:att forKey:@"attachment"];
[[FBRequest requestWithDelegate:self] call:@"facebook.stream.publish" params:attachment];