FBConnect facebook.stream.publish with NSDictionary problems

后端 未结 4 978
北恋
北恋 2021-02-10 16:51

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

NSDictionary *firstDict = [NSDictionary dictionaryWithObjectsAndKeys:
    @\"image\", @\"Type\",         


        
相关标签:
4条回答
  • 2021-02-10 17:01

    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.

    0 讨论(0)
  • 2021-02-10 17:08

    dataUsingEncoding: is a NSString method, so presumably, some object is expecting an instance of NSString where you passed an array. I presume that FBRequest cannot deal with the array you included in the attachment dictionary.

    0 讨论(0)
  • 2021-02-10 17:13

    I couldn't find a way to send the dictionary with an array but there's another class that works for me:

    FBStreamDialog
    

    Which brings up a dialog before it sends the information and it's delegate methods lets you know about changes.

    0 讨论(0)
  • 2021-02-10 17:21

    I modified your example a little and it works. Here's the working code:

    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 讨论(0)
提交回复
热议问题