4

我有这段代码,直到现在才能发送 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

[会话恢复] 调用返回 TRUE。

提前致谢。

4

4 回答 4

13

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

- (IBAction) 发布流:(id)sender {

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

  NSDictionary* actionLinks = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:
                               @"一直在运行",@"text",@"http://thinkdiff.net",@"href", nil], nil];

  NSString *actionLinksStr = [jsonWriter stringWithObject:actionLinks];
  NSDictionary* imageShare = [NSDictionary dictionaryWithObjectsAndKeys:
                                @“图像类型”,
                                @"http://thinkdiff.net/mahmud_small.jpg", @"src",
                                @"http://thinkdiff.net", @"href",
                                零];

  NSDictionary* 附件 = [NSDictionary 字典WithObjectsAndKeys:
                               @"a long run", @"name",
                               @“Facebook Running 应用”,@“标题”,
                               @“这很有趣”,@“描述”,
                               @"http://itsti.me/", @"href",
                               [NSArray arrayWithObjects:imageShare, nil ], @"media",
                              零];
  NSString *attachmentStr = [jsonWriter stringWithObject:attachment];
    NSLog(attachmentStr);
  NSMutableDictionary* 参数 = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 kAppId, @"api_key",
                                 @“在 Facebook 上分享”,@“user_message_prompt”,
                                 actionLinksStr, @"action_links",
                                 附件Str,@“附件”,
                                 零];

  [_facebook 对话:@"stream.publish"
          andParams:参数
        andDelegate:self];
}

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

NSDictionary* imageShare = [NSDictionary dictionaryWithObjectsAndKeys:
                                @“图像类型”,
                                @"http://thinkdiff.net/mahmud_small.jpg", @"src",
                                @"http://thinkdiff.net", @"href",
                                零];

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

[NSArray arrayWithObjects:imageShare, nil ]

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

于 2010-08-23T18:32:25.967 回答
1

我找不到用数组发送字典的方法,但是还有另一个类对我有用:

FBStreamDialog

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

于 2009-11-06T22:39:48.297 回答
0

dataUsingEncoding:是一种NSString方法,因此可以推测,某些对象期望NSString您传递数组的实例。我认为 FBRequest 无法处理您包含在attachment字典中的数组。

于 2009-10-30T21:20:07.093 回答
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];
于 2010-01-26T22:44:17.480 回答