ShareKit 2.0

| Comments

For those who don’t known ShareKit, it’s an easy social sharing library for iOS, you can share files, images and text in just three lines of code!! that’s why it just became very popular. It’s been refactored by the community in something called “ShareKit 2.0”. and now it’s ready. Here are some highlights:

  • new sharers (FourSquare, LinkedIn, VKontakte)
  • Twitter on iOS 5 uses system dialogue
  • many other improvements, mostly stability and memory issues
  • optimized for adding as git submodule to your project for easy updating

In this tutorial we are going to create a new project and share an image with ShareKit, so let’s do it!!

1: Download Starting Project

Download the starting project here, this is an Xcode 4 SingleView iOS Application project, the main window contains a UIImageView (for the image that we want to share) and a UIToolBar with a single UIBarButtonItem, this project has all the basic functionality to start this tutorial, if you compile and run, you can tap the UIBarButtonItem and you´ll see a log message in the console window, take a look to the ViewController Class to see what’s inside of it.

Note: please don’t forget to make it a Git repository!, if you don’t do it you can’t continue with this tutorial, if you don’t know anything about Git you can go here to start.

2: Get ShareKit Code!

The easiest way to add ShareKit to your project is by adding it as a Git submodule

1
2
git submodule add git://github.com/ShareKit/ShareKit.git Submodules/ShareKit
git commit -m 'ShareKit added as submodule'

this creates a new git submodule, downloads the ShareKit files in to Submodules/ShareKit directory and creates a new commit.

Now we need to get the ShareKit dependencies, navigate to the newly created ShareKit directory and download all submodule files

1
2
3
cd Submodules/ShareKit
git submodule init
git submodule update

for information on how to update ShareKit, you can go to the FAQ.

3: Adding ShareKit to your project

drag the Submodules/ShareKit/ShareKit.xcodeproj file (from a Finder window) to your project. You should see the ShareKit’s proj tree open up within your project in XCode like this:

now drag the ShareKit Group from the example project into your project’s Groups & Files. Then, you must remove ShareKit.xcodeproj (whole ShareKit project) from your project, so that it does not interfere with your project.

Add Frameworks

Expand the ‘Frameworks’ group in your project’s file list. Make sure you have the following frameworks:

  • SystemConfiguration.framework
  • Security.framework
  • MessageUI.framework
  • CFNetwork.framework (for Flickr)
  • CoreLocation.framework (for Foursquare)
  • Twitter.framework (it is new in iOS 5, so if you deploy to older versions of iOS, mark it optional)

4: Configuration

In this example we are going to use the same configuration from the example code that we have already downloaded, create a new class that extends from DefaultSHKConfigurator and name it SKCustomConfigurator, then copy the contents of ShareKitDemoConfigurator.m (they’re located at Submodules/ShareKit/Classes/Example folder) in to your class, or to make you life even easier download it from here

Remember that we downloaded a git repository if you modify the ShareKitDemoConfigurator, next time you try to update ShareKit to the latest version, it will conflict, here we are creating a new class to avoid that, now you’re free to modify the SKCustomConfigurator at your will.

next we need to tell ShariKit about this configuration, open your AppDelegate.m and include the following headers:

1
2
#import "SHKConfiguration.h"
#import "SKCustomConfigurator.h"

then in application:didFinishLaunchingWithOptions method paste the following code.

1
2
3
4
//Here you load ShareKit submodule with app specific configuration
DefaultSHKConfigurator *configurator = [[SKCustomConfigurator alloc] init];
[SHKConfiguration sharedInstanceWithConfigurator:configurator];
[configurator release];

you can see the Configuration page from the Wiki of ShareKit for more info about it.

5: Start Sharing!

open the ViewController.m implementation file and include the following headers

1
2
3
#import "SHK.h"
#import "SHKItem.h"
#import "SHKActionSheet.h"

next in the share method add the following code:

1
2
3
SHKItem *item = [SHKItem image:myImageView.image title:@"Apple Retro"];
SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
[actionSheet showFromToolbar:myToolBar];

and that’s it! ShareKit will take care of the rest from here!!

you can download the example project from here

Happy Coding!

Comments