how to post URL with Image on facebook by ShareKit

10,179

Solution 1

Please have a look to the code I just made my own Send method in SHKFacebook.m/h files, I think it will help you.

  -(BOOL) sendWithImage :(NSString*)creativeUrl 
   {
            self.pendingFacebookAction = SHKFacebookPendingStatus;

        SHKFBStreamDialog* dialog = [[[SHKFBStreamDialog alloc] init] autorelease];
        dialog.delegate = self;
        dialog.userMessagePrompt = SHKLocalizedString(@"Enter your message:");
        dialog.attachment = [NSString stringWithFormat:
                             @"{\
                             \"name\":\"%@\",\
                             \"href\":\"%@\",\
                             \"media\":[{\"type\":\"image\",\"src\":\"%@\",\"href\":\"%@\"}]\
                             }",
                            item.title == nil ? SHKEncodeURL(item.URL) : SHKEncode(item.title),
                            SHKEncodeURL(item.URL),
                            creativeUrl,
                            SHKEncodeURL(item.URL) 

                        ];
    dialog.defaultStatus = item.text;
    dialog.actionLinks = [NSString stringWithFormat:@"[{\"text\":\"Get %@\",\"href\":\"%@\"}]",
                            SHKEncode(SHKMyAppName),
                            SHKEncode(SHKMyAppURL)];
    [dialog show];
    return YES; 

}

Here is How I Use it

SHKFacebook *fb =[[SHKFacebook alloc]init];

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@iphone/photo?id=%d",[SplashViewController getBaseURL],[photoId intValue]]];
    fb.item = [SHKItem URL:url title:[dicResult valueForKey:@"Caption"]];
    //fb.shareType = SHKShareTypeURL;
    fb.quiet = YES;
    [TedryUITabBarController updateProgress:10 totalByteToSent:11 message:@"Sharing on facebook"];
    [fb sendWithImage:[dicResult valueForKey:@"CreativeURL"] :@""]; 

Solution 2

This may be slightly simpler, I hope, for those having problems with other answers here (although basically the same in principle).

Add the custom values to your item, such as:

[item setCustomValue:@"Your caption" forKey:@"caption"];
[item setCustomValue:@"Your description" forKey:@"description"];
[item setCustomValue:@"Your image URL" forKey:@"mediaSrc"];
[item setCustomValue:@"Your image link" forKey:@"mediaHREF"];

Note the two values needed for the image at the bottom: the source URL and the link URL. You then need to modify the "send" method in SHKFacebook.m to use those values, as follows:

// ...
if (item.shareType == SHKShareTypeURL)
{
    self.pendingFacebookAction = SHKFacebookPendingStatus;

    SHKFBStreamDialog* dialog = [[[SHKFBStreamDialog alloc] init] autorelease];
    dialog.delegate = self;
    dialog.userMessagePrompt = SHKLocalizedString(@"Enter your message:");

    // with image...
    dialog.attachment = [NSString stringWithFormat:
                         @"{\
                         \"name\":\"%@\",\
                         \"href\":\"%@\",\
                         \"caption\":\"%@\",\
                         \"description\":\"%@\",\
                         \"media\":[\
                            {\
                                \"type\": \"image\",\
                                \"src\": \"%@\",\
                                \"href\": \"%@\"\
                            }]\
                         }",
                         item.title == nil ? SHKEncodeURL(item.URL) : SHKEncode(item.title),
                         SHKEncodeURL(item.URL),
                         [item customValueForKey:@"caption"],
                         [item customValueForKey:@"description"],
                         [item customValueForKey:@"mediaSrc"],
                         [item customValueForKey:@"mediaHREF"]
                         ];

    dialog.defaultStatus = item.text;
    dialog.actionLinks = [NSString stringWithFormat:@"[{\"text\":\"Get %@\",\"href\":\"%@\"}]",
                          SHKEncode(SHKMyAppName),
                          SHKEncode(SHKMyAppURL)];
    [dialog show];

}
// ...

And the link given by jumponadoughnut is the definitive list of fields you can use (I could not vote his up because my rep is not high enough): http://developers.facebook.com/docs/guides/attachments

Solution 3

I'm using the latest sharekit from github. Actually, you just set the url as the custom value of the link, with key "picture"

NSString *urlImage = @"http://someurl.com/someimage.jpg";
[linkItem setCustomValue:urlImage forKey:@"picture"];

It works for me.

Solution 4

You need to include a media url in the attachment json NSString found in the [SHKFacebook send:] implementation. See http://developers.facebook.com/docs/guides/attachments

Share:
10,179
justicepenny
Author by

justicepenny

Updated on July 17, 2022

Comments

  • justicepenny
    justicepenny almost 2 years

    I am using sharekit in my iPhone app to share url link on facebook. However, it seems to me that it is not possible to share url with image with sharekit. Do you guys know how to do it? Thanks a lot.

  • Lior Frenkel
    Lior Frenkel about 13 years
    scompt, can you help me get started with your code here? where is the item? I thought sharekit is all about the item... how do i call sendWithImage - can you show a sample? Thanks!
  • daihovey
    daihovey over 12 years
    Youve missed out big chunks of your code, so it's impossible to work how you've solved it
  • PetrV
    PetrV over 12 years
    you should also escape these custom values with SHKEncode or SHKEncodeURL or via other means, because you can end up with invalid JSON data if you don't
  • Chris Nolet
    Chris Nolet over 12 years
    Looking at the source code: you need to set SHKShareTypeURL = SHKShareTypeURL and set item.URL. Worked well for me.