Use of ShareKit library for sharing on Facebook and Twitter through iOS apps

Share Kit library is widely used in iOS Applications. With the help of this library we can share posts,images,weblinks etc. on Facebook,Twitter,Email and some other social networking sites. Here is the managed guide for integration of ShareKit Library with iOS apps.

Step 1: Create an Xcode project(View Based).

Step 2: Add three frameworks to your project.┬áTo do this, click on your project name on left side of Xcode window,then on ‘Build phases’, then click on the link -binary with libraries. If you want to add, click ‘plus’ symbol there.

Frameworks:-

1.SystemConfiguration.framework

2.MessageUI.framework

3.Security.framework

Step 3: Go to link

https://github.com/dagerydoo/ShareKit

Step 4: Click on ZIP button on top of your Page.This will download the whole project for you. Then, open this project in your xcode.

Step 5: Drag the Share Kit folder of downloaded project into your project which you have just created.

Step 6: Now Open AppDelegate.h file of your project.

Replace your code with following code.

[objc]

#import

@class NewFinalShareViewController;//write ur class file

@interface NewFinalShareAppDelegate : NSObject {
UIWindow *window;
NewFinalShareViewController *viewController;//write your class file
UINavigationController *navigationController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet NewFinalShareViewController *viewController;
@property (nonatomic,retain) IBOutlet UINavigationController *navigationController;
@end

[/objc]

Replace the class names accordingly.

Step 7: Replace your AppDelegate.m File with following code.

[objc]

#import "NewFinalShareAppDelegate.h" //write ur class name
#import "NewFinalShareViewController.h" //Write ur class name

#import "SHKFacebook.h"
#import "SHKReadItLater.h"

@implementation NewFinalShareAppDelegate //write ur class name

@synthesize window;
@synthesize viewController;
@synthesize navigationController;

#pragma mark –
#pragma mark Application lifecycle

– (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

// Add the view controller’s view to the window and display.
navigationController=[[UINavigationController alloc]initWithRootViewController:self.viewController];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
navigationController.topViewController.title = SHKLocalizedString(@"ShareKit Testing");
[navigationController setToolbarHidden:NO];
[self performSelector:@selector(testOffline) withObject:nil afterDelay:0.5];

return YES;
}
– (void)testOffline
{
[SHK flushOfflineQueue];
}

– (BOOL)handleOpenURL:(NSURL*)url
{
NSDictionary *info = [[NSBundle mainBundle] infoDictionary];
NSNumber *index = [info objectForKey:@"FacebookURLTypeIndex"];
if (! index) return YES;
NSArray *urlTypes = [info objectForKey:@"CFBundleURLTypes"];
if ((! urlTypes) || ([urlTypes count] Source code.

[/objc]

Then Replace the whole code of info.plist by the following code. Make sure that you write your key as indicated below.

[objc]

CFBundleDevelopmentRegion
English
CFBundleDisplayName
${PRODUCT_NAME}
CFBundleExecutable
${EXECUTABLE_NAME}
CFBundleIconFile

CFBundleIdentifier
com.yourcompany.${PRODUCT_NAME:rfc1034identifier}
CFBundleInfoDictionaryVersion
6.0
CFBundleName
${PRODUCT_NAME}
CFBundlePackageType
APPL
CFBundleSignature
????
CFBundleURLTypes

CFBundleURLSchemes

fb282323663 // <–Write ur key here with fb in front always.

CFBundleVersion
1.0
FacebookURLTypeIndex
0
LSRequiresIPhoneOS

NSMainNibFile //for Xcode 4.2 remove this line
MainWindow //for Xcode 4.2 remove this line
NSMainNibFile~ipad //for Xcode 4.2 remove this line
MainWindow-iPad //for Xcode 4.2 remove this line

[/objc]

Step 8: Open ur viewcontroller.h file
Replace with following code.

[objc]

#import

@interface NewFinalShareViewController : UIViewController //Change ur Class Name
{
UIImageView *imgView;
UIButton *btnShare;
}
@property(nonatomic,retain)IBOutlet UIImageView *imgView;
@property(nonatomic,retain)IBOutlet UIButton *btnShare;

-(IBAction)btnSharePressed;

@end

[/objc]

Step 9: Open your viewcontroller.m file

Replace with following code.

[objc]

#import "NewFinalShareViewController.h"

#import "SHKItem.h"
#import "SHKActionSheet.h"

@implementation NewFinalShareViewController

@synthesize imgView,btnShare;

//if ur using xcode 4.2 there is no need for following function
– (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
//self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
{
self.toolbarItems = [NSArray arrayWithObjects:
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease],
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(share)] autorelease],
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease],
nil
];
} return self;
}

//for xcode 4.2 start from here

-(IBAction)btnSharePressed
{
//SHKItem *item = [SHKItem text:@"chasjashdasdada"];//image:imgView.image title:@"Funnnnn"];
// SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
//
// [actionSheet showFromToolbar:self.navigationController.toolbar];
self.toolbarItems = [NSArray arrayWithObjects:
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease],
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(share)] autorelease],
[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease],
nil
];
//} return self;
}
– (void)share
{
//SHKItem *item = [SHKItem image:imgView.image title:@"Funnnnn"];//image sharing
SHKItem *item = [SHKItem text:@"MY FIRST SAHRING THROUGH MY OWN APP……:)"];//image:imgView.image title:@"Funnnnn"];
SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
[actionSheet showFromToolbar:self.navigationController.toolbar];
}

//for xcode 4.2 omit this function (void)loadView
– (void)loadView
{
[super loadView];
self.toolbarItems = [NSArray arrayWithObjects:
[[[UIBarButtonItem alloc] initWithTitle:SHKLocalizedString(@"Logout") style:UIBarButtonItemStyleBordered target:self action:@selector(logout)] autorelease],
nil
];
}

– (void)logout
{
[[[[UIAlertView alloc] initWithTitle:SHKLocalizedString(@"Logout")
message:SHKLocalizedString(@"Are you sure you want to logout of all share services?")
delegate:self
cancelButtonTitle:SHKLocalizedString(@"Cancel")
otherButtonTitles:@"Logout",nil] autorelease] show];
[SHK logoutOfAll];
}

//SKIP THE PORTION BELOW IF YOU ARE USING XCODE 4.2 AND ABOVE
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
– (void)loadView {
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
– (void)viewDidLoad {
[super viewDidLoad];
}

/*
// Override to allow orientations other than the default portrait orientation.
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

– (void)didReceiveMemoryWarning {
// Releases the view if it doesn’t have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren’t in use.
}

– (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

– (void)dealloc {
[super dealloc];
}

@end

[/objc]

Step 10: Open your viewController.xib file and add uimageview and uibutton and make proper connections.

Save it and run. It will work smoothly.

Now Twitter sharing———————————————

Step 1: Click on following url-

Go to

https://dev.twitter.com/

Sign in
Then click on Create new Application link
Then fill Name of application and other fields also.
In website textfield u can fill “http://www.google.co.in/”
Step 2: Your keys will be generated.

[objc]

#define SHKTwitterConsumerKey @"LrSgzLQKCX" //Write ur key
#define SHKTwitterSecret @"2c1OACDjV3mTzvppNrIpGw" //Write ur key
#define SHKTwitterCallbackUrl @"http://www.google.co.in/" // You need to set this if using OAuth, see note above (xAuth users can skip it)
#define SHKTwitterUseXAuth 0 // To use xAuth, set to 1
#define SHKTwitterUsername @""

[/objc]

Step 3: When you are finished with it, run. If an error named Unknown error displays then make the following setting:-

Click on Sharekit folder->Sharers->Services->Twitter->SHKTwitter.m
Then look in function (void)sendStatus

replace the whole sendStatus function with following code.

[objc]

– (void)sendStatus
{
OAMutableURLRequest *oRequest = [[OAMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"https://api.twitter.com/1/statuses/update.json"]
consumer:consumer
token:accessToken
realm:nil
signatureProvider:nil];
[oRequest setHTTPMethod:@"POST"];
OARequestParameter *statusParam = [[OARequestParameter alloc] initWithName:@"status"
value:[item customValueForKey:@"status"]];
NSArray *params = [NSArray arrayWithObjects:statusParam, nil];
[oRequest setParameters:params];
[statusParam release];
OAAsynchronousDataFetcher *fetcher = [OAAsynchronousDataFetcher asynchronousFetcherWithRequest:oRequest
delegate:self
didFinishSelector:@selector(sendStatusTicket:didFinishWithData:)
didFailSelector:@selector(sendStatusTicket:didFailWithError:)];
[fetcher start];
[oRequest release];
}

[/objc]

References:-

https://github.com/dagerydoo/ShareKit
http://stackoverflow.com/

Leave a Reply

Your email address will not be published. Required fields are marked *


4 − four =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>