Advertisement
  1. Code
  2. iOS SDK

iOS Project Spotlight: ShareKit

Scroll to top
Read Time: 6 min

ShareKit is an open-source library that will quickly add content sharing capabilities to any iOS app. The project supports sharing links, images, plaintext, and files. Integration is a snap and the project is well-documented on the official ShareKit homepage.

ShareKit Overview

For a quick overview of the capabilities that ShareKit can bring to your project, just watch the official project overview video:

The ShareKit project currently supports sharing via the following services:

  • Delicious
  • Facebook
  • Google Reader
  • Instapaper
  • Pinboard
  • Read It Later
  • Tumblr
  • Twitter
  • E-mail

Integration Instructions

ShareKit integration is well documented in a five step tutorial on the official site. However, at the time of this writing, the instructions on the official site are not accurate for Xcode 4. What follows is a modified version for Xcode 4 users.

Step 1: Download

You can download the ShareKit code either directly from the project site or on GitHub.

Step 2: Add ShareKit to Your Xcode 4 Project

After opening the ShareKit download, expand the "classes" folder and find the folder titled "ShareKit". Drag this folder into your own project. You can place the ShareKit folder anywhere within your destination project, but I would suggest placing it as a subgroup named "ShareKit" within your main project group.

Step 3: Add the Frameworks Necessary for ShareKit

Select your project in the project navigator pane, and then highlight the main target. Next select the "Build Phases" tab, and then expand the "Link Binary With Libraries" section.

Adding Frameworks in Xcode 4Adding Frameworks in Xcode 4Adding Frameworks in Xcode 4

Now click the "+" symbol to open a dialogue with a list of frameworks available. In order for ShareKit to work, add the following frameworks to your project:

  • SystemConfiguration.framework
  • Security.framework
  • MessageUI.framework

Step 4: Add Web Service API Keys

Most of the methods ShareKit provides for content sharing will require you to provide an API key for your application. This key will be used to identify your application to the third-party service. In order to obtain an API key, you'll need to walk through the signup process for each service. App registration links for various ShareKit supported services are listed below:

*Note that if you want to be able to share content via Twitter, you'll also need to register with Bit.ly (http://bit.ly/account/register). ShareKit will automatically use your Bit.ly account to shorten links posted to Twitter.

After you've obtained the necessary API keys for your app, open the file SHKConfig.h in Xcode (It's in the ShareKit group). If you peruse the comments in this file, you should quickly be able to find out how to configure each service. As an example, this is what a sample Twitter configuration might look like when using OAuth:

1
2
#define SHKTwitterConsumerKey @"wUf7eXia2pMl1QUvkaL4"

3
#define SHKTwitterSecret @"kHudJVKjM6m5DRKYCutSxkBHHUwiTrRNMvGNhL1LMk3G"

4
#define SHKTwitterCallbackUrl @"https://code.tutsplus.com"

5
6
#define SHKBitLyLogin @"username"

7
#define SHKBitLyKey @"kHudJVKjM6m5DRKYCutSxkBHHUwiTrRNMvGNhL1LMk3G"

Step 5: Import the ShareKit Header & Launch ShareKit

Before you can use ShareKit in your own classes, you'll need to import the proper ShareKit header file, like so:

1
2
#import "SHK.h"

As listed on the official ShareKit site, the code necessary to launch ShareKit will look something like this:

1
2
	// Create the item to share (in this example, a url)

3
	NSURL *url = [NSURL URLWithString:@"http://getsharekit.com"];
4
	SHKItem *item = [SHKItem URL:url title:@"ShareKit is Awesome!"];
5
6
	// Get the ShareKit action sheet

7
	SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
8
9
	// Display the action sheet

10
	[actionSheet showFromToolbar:navigationController.toolbar];

(Reference: http://www.getsharekit.com/install/)

The above example would be used when launching ShareKit from a UINavigationController toolbar button, but the general logic can be customized and placed anywhere in your app code. For example, you could execute it in response to a normal UIButton action or in viewDidLoad:.


Sharing Code Snippets

After successfully integrating ShareKit with your app, sharing links, text, images, or files is a snap and the remainder of this article will demonstrate the code snippets necessary to do so.

Sharing A Link

1
2
    // Create an NSURL. This could come from anywhere in your app.

3
    NSURL *url = [NSURL URLWithString:@"https://code.tutsplus.com"];
4
5
    // Wrap the URL within the SHKItem Class

6
    SHKItem *item = [SHKItem URL:url title:@"Mobiletuts!"];
7
    
8
    // Create a ShareKit ActionSheet and Assign the Sheet an SHKItem 

9
    SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
10
    
11
    // Display the ActionSheet in the current UIView

12
    [actionSheet showInView:self.view];

Sharing Plaintext

1
2
    // Create an NSString. This could be taken from a UITextField or anywhere else. 

3
    NSString *textToShare = @"To iterate is human, to recurse divine. -L. Peter Deutsch";
4
5
    // Wrap the NSString within the SHKItem Class

6
    SHKItem *item = [SHKItem text:textToShare];
7
    
8
    // Create a ShareKit ActionSheet and Assign the Sheet an SHKItem 

9
    SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
10
    
11
    // Display the ActionSheet in the current UIView

12
    [actionSheet showInView:self.view];

Sharing An Image

1
2
    // Create a UIImage. 

3
    UIImage *image = [UIImage imageNamed:@"ShareKit.jpg"];
4
    
5
    // Wrap the UIImage within the SHKItem class

6
    SHKItem *item = [SHKItem image:image title:@"This image was sent with ShareKit!"];
7
    
8
    // Create a ShareKit ActionSheet and Assign the Sheet an SHKItem

9
    SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
10
    
11
    // Display the ActionSheet in the current UIView

12
    [actionSheet showInView:self.view];

Sharing A File

1
2
    // Get the path to the file we want to share. This example resuses the 

3
    // the JPG file from above, but it could be anything. 

4
    NSString *path = [[NSBundle mainBundle] pathForResource:@"ShareKit" ofType:@"jpg"];  
5
6
    
7
    // Create an NSData object from the contents of a file.

8
    NSData *file = [NSData dataWithContentsOfFile:path];
9
10
    // Wrap the NSData object within an SHKItem

11
    SHKItem *item = [SHKItem file:file 
12
                         filename:@"ShareKit.jpg" 
13
                         mimeType:@"application/image" 
14
                            title:@"A ShareKit Picture"];
15
    
16
    // Create a ShareKit ActionSheet and Assign the Sheet an SHKItem

17
    SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
18
    
19
    // Display the ActionSheet in the current UIView

20
    [actionSheet showInView:self.view];

Learning More

In this article we've covered the main use cases you're likely to encounter when integrating ShareKit. However, there are still several significant features that we haven't covered, such as offline ShareKit support, sharing to a specific service, customizing the ShareKit interface, and creating custom ShareKit services. The official ShareKit documentation does an excellent job of explaining these features, and most of the code in this article was just a slight customization of what is already available there.


Conclusion

Next time you need to quickly enable sharing services in your app, keep this project in mind! If you do use ShareKit in your own work, be sure to thank ShareKit creator Nate Weiner (@IdeaShower) for all of his hard work in both creating the project and writing such excellent documentation on the official site.


Next Time. . .

What mobile project would you like to see explored with the Mobiletuts+ project spotlight next? Leave your answer in the comments section below!


One More Thing:


Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.