Follow by Email

Search This Blog

Wednesday, 2 November 2011

Twitter Integration In Your App



Hello Friends!
In this post i am going to guide about Twitter integration in iphone application with some easy steps you just have to copy paste. so here we go!

Download Twitter SDK folder and add in your project where your classes show on lefts side.

import headers files in your .h file
#import "SA_OAuthTwitterEngine.h"
#import "SA_OAuthTwitterController.h"

add these two delegate in .h file
<SA_OAuthTwitterEngineDelegate,SA_OAuthTwitterControllerDelegate>

example:

@interface TwitterViewController : UIViewController <SA_OAuthTwitterEngineDelegate,SA_OAuthTwitterControllerDelegate>{

then declare twitter engine variable in .h file
SA_OAuthTwitterEngine *_engine;


Right click on Frameworks on lefts side of the window then select Add
then select Existing Frameworks. Select 
libxml2.dylib

Now Goto Project in pulldown menu and select Edit Active Target, and search Header Search Path
add this line 
$(SDKROOT)/usr/include/libxml2

Now we have to add delegate function  in .m file
just copy and paste from twitter start to twitter end

//Twitter start
- (void)Twitter:(BOOL)animated {
if(_engine) {
return;
}
_engine = [[SA_OAuthTwitterEngine alloc] initOAuthWithDelegate:self];
_engine.consumerKey = @"9wHWtHz6C52dkBAsqRO8Q";
_engine.consumerSecret = @"qO8E2ags3WrGLO6ddRJVePabfxUnax8GYznHzNA";
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine: _engine delegate: self];
if (controller){
[self presentModalViewController: controller animated: YES];
}
else {
[self updateStream:nil];
}
}

-(IBAction)updateStream:(id)sender {
[_engine getFollowedTimelineSinceID:1 startingAtPage:1 count:100];
}

#pragma mark SA_OAuthTwitterEngineDelegate

- (void) storeCachedTwitterOAuthData: (NSString *) data forUsername: (NSString *) username {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject: data forKey: @"authData"];
[defaults synchronize];
}

- (NSString *) cachedTwitterOAuthDataForUsername: (NSString *) username {
return [[NSUserDefaults standardUserDefaults] objectForKey: @"authData"];
}

#pragma mark SA_OAuthTwitterController Delegate

- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username {
NSLog(@"Authenticated with user %@", username);
[self updateStream:nil];
}

- (void) OAuthTwitterControllerFailed: (SA_OAuthTwitterController *) controller {
NSLog(@"Authentication Failure");
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:@"Fail !" message:@"Authentication Failure !" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
[alert release];
_engine =nil;
}

- (void) OAuthTwitterControllerCanceled: (SA_OAuthTwitterController *) controller {
NSLog(@"Authentication Canceled");
_engine =nil;
}

#pragma mark MGTwitterEngineDelegate Methods

- (void)requestSucceeded:(NSString *)connectionIdentifier {
NSLog(@"Request Suceeded: %@", connectionIdentifier);
[_engine sendUpdate:[NSString stringWithFormat:@"http://iphone-app-dev-beginners.blogspot.com/"]];
[_engine clearAccessToken];
_engine = nil;
}

- (void)receivedObject:(NSDictionary *)dictionary forRequest:(NSString *)connectionIdentifier {
NSLog(@"Recieved Object: %@", dictionary);
}

- (void)directMessagesReceived:(NSArray *)messages forRequest:(NSString *)connectionIdentifier {
NSLog(@"Direct Messages Received: %@", messages);
}

- (void)userInfoReceived:(NSArray *)userInfo forRequest:(NSString *)connectionIdentifier {
NSLog(@"User Info Received: %@", userInfo);
}

- (void)miscInfoReceived:(NSArray *)miscInfo forRequest:(NSString *)connectionIdentifier {
NSLog(@"Misc Info Received: %@", miscInfo);
}

///twitter end

Last step you have to call twitter function in your Button action
like this:
-(IBAction)ButtonTwitterPressed
{
    [self Twitter:YES];
}


Download Complete Project: FacebookTwitter Example app

No comments:

Post a Comment