FBConnect facebook.stream.publish с проблемами NSDictionary - PullRequest
4 голосов
/ 30 октября 2009

У меня есть этот код, который не может отправить запрос 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 ]

13 голосов
/ 23 августа 2010

Если вы используете последнюю версию iOS SDK для 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, иначе API api не будет опубликован.

1 голос
/ 07 ноября 2009

Я не смог найти способ отправить словарь с массивом, но есть другой класс, который работает для меня:

FBStreamDialog

Который вызывает диалоговое окно до того, как отправляет информацию, а его методы делегата сообщают вам об изменениях.

0 голосов
/ 27 января 2010

Я немного изменил ваш пример, и он работает. Вот рабочий код:

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];
0 голосов
/ 31 октября 2009

dataUsingEncoding: - это метод NSString, поэтому, возможно, некоторый объект ожидает экземпляр NSString, в котором вы передали массив. Я предполагаю, что FBRequest не может работать с массивом, включенным в словарь attachment.

...