Sapan Diwakar

Software developer

Follow me on Twitter Check out my code on GitHub View some of my designs on Dribbble Take a look at my Linked In profile

Sharing multiple images on Facebook with Sharekit on iOS

Sharekit it great if you want to quickly add sharing features to your iOS app. I recently came across the need to share multiple images on facebook using Sharekit. In this post, I would assume that you already have sharekit included in your project. If you don't, have a look at How to install Sharekit.
Once you have installed Sharekit, you need to create an app on Facebook and get access tokens and put them at the right place and enable SSO (See step 7 on above guide). Now comes the fun part, enabling multiple sharing with Facebook.

First, we need to enable Autoshare for Facebook. Create a category SHKFacebook+Autoshare that returns YES in shouldAutoShare.

SHKFacebook+Autoshare.h

#import "SHKFacebook.h"

@interface SHKFacebook (Autoshare)
- (BOOL)shouldAutoShare;
@end

SHKFacebook+Autoshare.m

#import "SHKFacebook+Autoshare.h"

@implementation SHKFacebook (Autoshare)

- (BOOL)shouldAutoShare
{
    return YES;
}

@end

Sharekit still allows sharing just one item at a time. But, now we can share the first item and keep on subsequently sharing the other items in the SHKSendDidFinish notification handler. First, import the required files and register for the notification at an appropriate place:

#import "SHK.h"
#import "SHKFacebook.h"
#import "SHKFacebook+Autoshare.h"

....

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(SHKShareDidFinish:) name:@"SHKSendDidFinish" object:nil];
...

Then implement SHKShareDidFinish to share new items after one item is shared.

-(void) SHKShareDidFinish: (NSNotification *)notification
{

    // Remove sharekit message box for all images except last one.
    if ([imagesToSave count] > 0) {
        [(SHKFacebook*)notification.object setQuiet:YES];
    } else {
        [(SHKFacebook*)notification.object setQuiet:NO];
    }

    if ([imagesToSave count] > 0) {
        [self shareOneImageToFb];
    }
}

-(void) shareOneImageToFb
{
    if ([imagesToSave count] > 0) {
        NSNumber *index = [imagesToSave lastObject];
        [imagesToSave removeObject:index];

        SHKItem *item = [SHKItem image:[self getImageAtIndex:index] title:@"Testing multiple share with Facebook and Sharekit"];
        [SHKFacebook shareItem:item];
    }
}

To begin sharing the images, we just need to populate the imagesToSave NSMutableArray with image indices and implement the [self getImageAtIndex:index] and then call [self shareOneImageToFb];

The magic here is

[(SHKFacebook*)notification.object setQuiet:YES]; 

which sets Sharekit to quiet before saving more images . If we don't do this, it shows Saving to Facebook, Saved Successfully as many times as there are images. So, while we do want it to show the Saving to Facebook when it starts saving the image to Facebook, we want it to remain there until the last image is saved rather than having it to disappear every time and inform that the image has been saved successfully and then begin again.