iphone - FBConnect facebook.stream.publish 与 NSDictionary 问题

标签 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 恢复]调用返回 TRUE。

提前致谢。

最佳答案

如果您使用最新的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 将不会发布。

关于iphone - FBConnect facebook.stream.publish 与 NSDictionary 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1652049/

相关文章:

objective-c - UIGestureRecognizer 或 touchesBegan 没有响应

iphone - Facebook IOS SDK 提要帖子可见性

ios - iOS5 上的 FBConnect 和 ARC

iphone - iPhone 版本的 "Show Package Contents"显示空白 PNG?

iphone - 使用 Xcode 查找错误 - 尝试插入 nil 值

iphone - 如何在 iPhone 上添加全屏 iAd?

xcode - FBConnect 问题?

ios webview流后台模式

ios - 确定使用了哪个共享扩展

iphone - 发送推送通知和 APNS 握手