My app displays a short list of tweets based on a given TV hashtag with the additional ability to post a tweet.
But now twitter has gone oauth for all requests, my tweet list doesnt appear as it was using the old search.atom API.
Therefore, how do I access the search api and pass in OAuth credentials using Sharekit so user authenticates just the once for viewing tweets and posting between sessions.
I have tried using SHKRequest, hoping, that as ShareKit has already authorised it will pass this information through; with no joy, is there some other way of doing this or am I just using it badly/wrong.
request = [[[SHKRequest alloc] initWithURL:[NSURL URLWithString:#"https://api.twitter.com/1.1/search/tweets.json?q=twitter&result_type=recent"]
delegate:self
isFinishedSelector:#selector(sendFinishedSearch:)
params:nil
method:#"GET"
autostart:YES] autorelease];
I do need to maintain compatibility with 4.3 API so I cant just use iOS5 Twitter API.
Disclaimer: I am inheriting project from someone so my XCode/ObjC knowledge is being learnt whilst I modify project (I come from C/C++ background), so please ignore my ignorance.
ShareKit contains an SHKTwitter class. It is a subclass of SHKOAuthSharer. As such, you can ask it to perform authorisations / refreshes and get the resulting token.
Create an instance of SHKTwitter and register as it's delegate. Implement the - (void)sharerAuthDidFinish:(SHKSharer *)sharer success:(BOOL)success delegate method. Then call tokenRequest. When the delegate method is called, if success is YES you can get the accessToken.
SHKTwitter *twitter = [[SHKTwitter alloc] init];
twitter.shareDelegate = self;
[twitter tokenRequest];
- (void)sharerAuthDidFinish:(SHKSharer *)sharer success:(BOOL)success
{
SHKTwitter *twitter = (SHKTwitter *)sharer;
if (twitter.accessToken != nil) {
NSLog(#"session: %#, %#", twitterSharer.accessToken.key, twitterSharer.accessToken.secret);
} else {
[twitter tokenAccess];
}
}
IN SHKOAuthSharer.m class You will get access Token in method:
- (void)tokenAccessTicket:(OAServiceTicket *)ticket didFinishWithData:(NSData *)data
{
if (SHKDebugShowLogs) // check so we don't have to alloc the string with the data if we aren't logging
SHKLog(#"tokenAccessTicket Response Body: %#", [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
///[[SHKActivityIndicator currentIndicator] hide];
if (ticket.didSucceed)
{
NSString *responseBody = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
//piyush Added
NSArray *strArray = [responseBody componentsSeparatedByString:#"="];
[[NSUserDefaults standardUserDefaults]setObject:[strArray objectAtIndex:([strArray count]-1)] forKey:#"TwitterUsername"];
**self.accessToken** = [[OAToken alloc] initWithHTTPResponseBody:responseBody];
[responseBody release];
[self storeAccessToken];
//[self tryPendingAction];
[[NSNotificationCenter defaultCenter] postNotificationName:#"TwitterDidLogin" object:nil];
}
else
// TODO - better error handling here
[self tokenAccessTicket:ticket didFailWithError:[SHK error:SHKLocalizedString(#"There was a problem requesting access from %#", [self sharerTitle])]];
}
Related
Twitter has deprecated its API 1.0 and implemented new API 1.1
As per the new API we need to authentication before fetching the public time line.
I read the new API documentation here: https://dev.twitter.com/docs/api/1.1
but I have not clear idea, how to implement it in iOS.
Can anyone tell me the best way to fetch public time-line form Twitter using the API 1.1
Thanks in advance.
First, you need to Authenticate your request (Get permission).
second, see follow these steps:
1.Download FHSTwitterEngine Twitter Library.
2.Add the folder FHSTwitterEngine" to your project and #import "FHSTwitterEngine.h".
3.add SystemConfiguration.framework to your project.
Usage : 1.in the [ViewDidLoad] add the following code.
UIButton *logIn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
logIn.frame = CGRectMake(100, 100, 100, 100);
[logIn setTitle:#"Login" forState:UIControlStateNormal];
[logIn addTarget:self action:#selector(showLoginWindow:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:logIn];
[[FHSTwitterEngine sharedEngine]permanentlySetConsumerKey:#"<consumer_key>" andSecret:#"<consumer_secret>"];
[[FHSTwitterEngine sharedEngine]setDelegate:self];
and don't forget to import the delegate FHSTwitterEngineAccessTokenDelegate.
you need to get the permission for your request, with the following method which will present Login window:
- (void)showLoginWindow:(id)sender {
[[FHSTwitterEngine sharedEngine]showOAuthLoginControllerFromViewController:self withCompletion:^(BOOL success) {
NSLog(success?#"L0L success":#"O noes!!! Loggen faylur!!!");
}];
}
when the Login window is presented, enter your Twitter Username and Password to authenticate your request.
add the following methods to your code:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[FHSTwitterEngine sharedEngine]loadAccessToken];
NSString *username = [[FHSTwitterEngine sharedEngine]loggedInUsername];// self.engine.loggedInUsername;
if (username.length > 0) {
lbl.text = [NSString stringWithFormat:#"Logged in as %#",username];
[self listResults];
} else {
lbl.text = #"You are not logged in.";
}
}
- (void)storeAccessToken:(NSString *)accessToken {
[[NSUserDefaults standardUserDefaults]setObject:accessToken forKey:#"SavedAccessHTTPBody"];
}
- (NSString *)loadAccessToken {
return [[NSUserDefaults standardUserDefaults]objectForKey:#"SavedAccessHTTPBody"];
}
4.Now you are ready to get your request, with the following method(in this method I created a `Twitter` search for some `Hashtag`, to get the screen_name for example):
- (void)listResults {
dispatch_async(GCDBackgroundThread, ^{
#autoreleasepool {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
// the following line contains a FHSTwitterEngine method wich do the search.
dict = [[FHSTwitterEngine sharedEngine]searchTweetsWithQuery:#"#iOS" count:100 resultType:FHSTwitterEngineResultTypeRecent unil:nil sinceID:nil maxID:nil];
// NSLog(#"%#",dict);
NSArray *results = [dict objectForKey:#"statuses"];
// NSLog(#"array text = %#",results);
for (NSDictionary *item in results) {
NSLog(#"text == %#",[item objectForKey:#"text"]);
NSLog(#"name == %#",[[item objectForKey:#"user"]objectForKey:#"name"]);
NSLog(#"screen name == %#",[[item objectForKey:#"user"]objectForKey:#"screen_name"]);
NSLog(#"pic == %#",[[item objectForKey:#"user"]objectForKey:#"profile_image_url_https"]);
}
dispatch_sync(GCDMainThread, ^{
#autoreleasepool {
UIAlertView *av = [[UIAlertView alloc]initWithTitle:#"Complete!" message:#"Your list of followers has been fetched" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[av show];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
});
}
});
}
That's all. I just got the screen_name from a search Query, you can get a timeline for a user using the following methods:
// statuses/user_timeline
- (id)getTimelineForUser:(NSString *)user isID:(BOOL)isID count:(int)count;
- (id)getTimelineForUser:(NSString *)user isID:(BOOL)isID count:(int)count sinceID:(NSString *)sinceID maxID:(NSString *)maxID;
instead of the search method above.
Note: see the FHSTwitterEngine.h to know what method you need to use.
Note: to get the <consumer_key> and the <consumer_secret> you need to
to visit this link to register your app in Twitter.
You might be interested in the following library.
https://github.com/nst/STTwitter
It is a lightweight Objective-C wrapper for Twitter REST API 1.1.
Look at the ReadMe and Demo project for examples
This is just one of the many methods the library has:
- (void)getUserTimelineWithScreenName:(NSString *)screenName
successBlock:(void(^)(NSArray *statuses))successBlock
errorBlock:(void(^)(NSError *error))errorBlock;
I'm using the Parse SDK to implement a Facebook apprequests dialog in my app. For all intents and purposes it just wraps the Facebook SDK, prefixing Facebook methods with PF_.
I use this code to prepare and raise the dialog:
NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys: [NSString stringWithFormat:#"I just challenged you to a game!"], #"message",
[friendIds componentsJoinedByString:#"," ], #"to",
nil];
PF_FBSession *session = [PFFacebookUtils session];
PF_Facebook *facebook = [[PF_Facebook alloc] initWithAppId:session.appID andDelegate:nil];
facebook.accessToken = session.accessToken;
facebook.expirationDate = session.expirationDate;
[facebook dialog:#"apprequests" andParams:params andDelegate:self];
This works well, I'm getting the dialog, I'm able to invite friends to play with the app.
The problem is that the delegate methods are not being called, even though I've set the view controller as a PF_FBDialogDelegate:
#interface ChallengeFriendsViewController : UIViewController <UITableViewDelegate, PF_FBDialogDelegate> {
NSArray *facebookFriends;
NSMutableArray *selectedFriends;
}
These are some of the delegate methods I'm talking about:
- (void)dialog:(PF_FBDialog *)dialog didFailWithError:(NSError *)error {
NSLog(#"Error in Dialog: %#", error);
}
- (void)dialogDidNotCompleteWithUrl:(NSURL *)url {
NSLog(#"Failure on Facebook server side");
}
- (void)dialogCompleteWithUrl:(NSURL *)url {
NSLog(#"Did complete with URL");
[self.navigationController popViewControllerAnimated:YES];
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}
- (void)dialogDidNotComplete:(PF_FBDialog *)dialog {
NSLog(#"Cancelled");
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
}
Without these methods being called I'm really not able to handle the sharing in an intuitive way. I'm stumped as to why they wouldn't be called and feel I've tried everything. Any pointers to where I'm going wrong?
In your code you have
PF_Facebook *facebook = [[PF_Facebook alloc] initWithAppId:session.appID andDelegate:nil];
Since you set a nil delegate, I would expect the delegate methods are never called, as you describe.
Could you change this to
PF_Facebook *facebook = [[PF_Facebook alloc] initWithAppId:session.appID andDelegate:self];
That is assuming that you put the delegate methods in the same class.
I finally got around the problem by using the facebook instance provided by PFFacebookUtils. This is deprecated but appears to be the only way to make it call the correct delegate methods at the moment.
Replaced:
PF_Facebook *facebook = [[PF_Facebook alloc] initWithAppId:session.appID andDelegate:nil];
With:
PF_Facebook *facebook = [PFFacebookUtils facebook];
Thanks to JP and Aromal for your input.
I'm trying to modify Apple's sample code for using the Twitter API so that I can use the streaming API to filter tweets. I am attempting to implement the method suggested in response to this question:
Does TWRequest work for the twitter streaming api?
I have created the signed NSURLRequest and NSURLConnection objects as suggested, and set the delegate for the connection object. A valid twitter account is selected and used to sign the url. The problem is that the delegates connection:didReceiveData: method is never being called.
Here's my code:
#implementation Twitter
-(id)init{
if (self=[super init]) {
NSLog(#"Twitter init");
// Tell the notification centre to inform the app if the twitter account changes
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(twitterAccountChanged)
name:ACAccountStoreDidChangeNotification object:nil];
// Create an account store object.
ACAccountStore *accountStore = [[ACAccountStore alloc] init];
// Create an account type that ensures Twitter accounts are retrieved.
ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request access from the user to use their Twitter accounts.
[accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
if(granted) {
NSLog(#"Twitter: Access to twitter accounts granted");
// Get the list of Twitter accounts.
NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];
// Pick the twitter account to use
if ([accountsArray count] > 0) {
// Grab the initial Twitter account to tweet from.
ACAccount *twitterAccount = [accountsArray objectAtIndex:0];
// This is for a status filter
NSURL *url=[NSURL URLWithString:#"https://stream.twitter.com/1/statuses/filter.json"];
// Create the parameters dictionary
NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:#"twitter", #"track", nil];
// Create TWRequest object
TWRequest *req=[[TWRequest alloc] initWithURL:url parameters:dictionary requestMethod:TWRequestMethodPOST];
// Set the account
[req setAccount:twitterAccount];
// Get a signed URL request
NSURLRequest *signedRequest=[req signedURLRequest];
// Initate the connection
[NSURLConnection connectionWithRequest:signedRequest delegate:self];
} else {
NSLog(#"Twitter: No twitter accounts to access");
}
} else {
NSLog(#"Twitter: Access to twitter accounts denied");
}
}];
return self;
}
return nil;
}
-(void)twitterAccountChanged{
NSLog(#"Twitter twitterAccountChanged");
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
NSLog(#"data received");
}
The output from the program is:
2012-07-24 09:50:03.668 TwitterAPITest[36722:10403] Twitter init
2012-07-24 09:50:03.836 TwitterAPITest[36722:11d03] Twitter: Access to twitter accounts granted
As you can see, everything appears to work OK, the only problem is that the delegate method is never called and I currently have no idea why.
Any help would be much appreciated...
Mike
It took me a bit of time to get this up and running, So I thought I aught to post my code for others. In my case I was trying to get tweets close to a certain location, so you will see that I used a locations parameter and a location struct I had in scope. You can add whatever params you want to the params dictionary.
Also note that this is bare bones, and you will want to do things such as notify the user that an account was not found and allow the user to select the twitter account they would like to use if multiple accounts exist.
Happy Streaming!
//First, we need to obtain the account instance for the user's Twitter account
ACAccountStore *store = [[ACAccountStore alloc] init];
ACAccountType *twitterAccountType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request permission from the user to access the available Twitter accounts
[store requestAccessToAccountsWithType:twitterAccountType
withCompletionHandler:^(BOOL granted, NSError *error) {
if (!granted) {
// The user rejected your request
NSLog(#"User rejected access to the account.");
}
else {
// Grab the available accounts
NSArray *twitterAccounts = [store accountsWithAccountType:twitterAccountType];
if ([twitterAccounts count] > 0) {
// Use the first account for simplicity
ACAccount *account = [twitterAccounts objectAtIndex:0];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:#"1" forKey:#"include_entities"];
[params setObject:location forKey:#"locations"];
[params setObject:#"true" forKey:#"stall_warnings"];
//set any other criteria to track
//params setObject:#"words, to, track" forKey#"track"];
// The endpoint that we wish to call
NSURL *url = [NSURL URLWithString:#"https://stream.twitter.com/1.1/statuses/filter.json"];
// Build the request with our parameter
TWRequest *request = [[TWRequest alloc] initWithURL:url
parameters:params
requestMethod:TWRequestMethodPOST];
// Attach the account object to this request
[request setAccount:account];
NSURLRequest *signedReq = request.signedURLRequest;
// make the connection, ensuring that it is made on the main runloop
self.twitterConnection = [[NSURLConnection alloc] initWithRequest:signedReq delegate:self startImmediately: NO];
[self.twitterConnection scheduleInRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
[self.twitterConnection start];
} // if ([twitterAccounts count] > 0)
} // if (granted)
}];
I am trying to make a basic iphone app that shows nearby tweets. I was using the TWRequest object to accomplish this with the twitter search api. Unfortunately, I would actually like to mark the tweets on a map using their GPS coordinates and the search api doesn't seem to return the actual location that a tweet was made with any better accuracy than the city name.
As such, I think I need to switch to the streaming api. I am wondering if it is possible to continue using the TWRequest object in this case or if I need to actually switch over to using NSURLConnection? Thanks in advance!
Avtar
Yes, you can use a TWRequest object. Create your TWRequest object using the appropriate URL and parameters from the Twitter API doco, and set the TWRequest.account property to the ACAccount object for the Twitter account.
You can then use the signedURLRequest method of TWRequest to get an NSURLRequest which can be used to create an asynchronous NSURLConnection using connectionWithRequest:delegate:.
Once this is done, the delegate's connection:didReceiveData: method will be called whenever data is received from Twitter. Note that each NSData object received may contain more than one JSON object. You will need to split these up (separated by "\r\n") before converting each one from JSON using NSJSONSerialization.
It took me a bit of time to get this up and running, So I thought I aught to post my code for others. In my case I was trying to get tweets close to a certain location, so you will see that I used a locations parameter and a location struct I had in scope. You can add whatever params you want to the params dictionary.
Also note that this is bare bones, and you will want to do things such as notify the user that an account was not found and allow the user to select the twitter account they would like to use if multiple accounts exist.
Happy Streaming!
//First, we need to obtain the account instance for the user's Twitter account
ACAccountStore *store = [[ACAccountStore alloc] init];
ACAccountType *twitterAccountType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
// Request permission from the user to access the available Twitter accounts
[store requestAccessToAccountsWithType:twitterAccountType
withCompletionHandler:^(BOOL granted, NSError *error) {
if (!granted) {
// The user rejected your request
NSLog(#"User rejected access to the account.");
}
else {
// Grab the available accounts
NSArray *twitterAccounts = [store accountsWithAccountType:twitterAccountType];
if ([twitterAccounts count] > 0) {
// Use the first account for simplicity
ACAccount *account = [twitterAccounts objectAtIndex:0];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:#"1" forKey:#"include_entities"];
[params setObject:location forKey:#"locations"];
[params setObject:#"true" forKey:#"stall_warnings"];
//set any other criteria to track
//params setObject:#"words, to, track" forKey#"track"];
// The endpoint that we wish to call
NSURL *url = [NSURL URLWithString:#"https://stream.twitter.com/1.1/statuses/filter.json"];
// Build the request with our parameter
TWRequest *request = [[TWRequest alloc] initWithURL:url
parameters:params
requestMethod:TWRequestMethodPOST];
// Attach the account object to this request
[request setAccount:account];
NSURLRequest *signedReq = request.signedURLRequest;
// make the connection, ensuring that it is made on the main runloop
self.twitterConnection = [[NSURLConnection alloc] initWithRequest:signedReq delegate:self startImmediately: NO];
[self.twitterConnection scheduleInRunLoop:[NSRunLoop mainRunLoop]
forMode:NSDefaultRunLoopMode];
[self.twitterConnection start];
} // if ([twitterAccounts count] > 0)
} // if (granted)
}];
I'm working with the engine for the first time and am fairly new to iOS development. I' m curious though if there's a way to call a function after I log in successfully? I can't seem to get it to work along the lines that FBConnect called for fbDidLogin after your initial login function. I have MGTwitterEngineDelegate declared and I tried to use the following code after login:
//TWITTER LOGIN FUNCTION/////////////////////
-(void)twitLogin{
NSLog(#"Inside of twitLogin...");
if(![_engine isAuthorized]){
UIViewController *controller = [SA_OAuthTwitterController controllerToEnterCredentialsWithTwitterEngine:_engine delegate:self];
if (controller){
[self presentModalViewController: controller animated: YES];
NSLog(#"Inside of Twitter login UIWebView.");
} else {
NSLog(#"ONCE WE'RE LOGGED IN, LETS SEND THE TWEET!");
NSString *tweet = [[NSString alloc] initWithFormat:#"My Tweet!"];
[_engine sendUpdate:tweet];
NSLog(#"You just tweeted: %#",tweet);
}
} else {
NSString *twitterToken=[[NSUserDefaults standardUserDefaults] objectForKey:#"authData"];
NSLog(#"Twitter is authorized?: %#",twitterToken);
//Twitter Integration Code Goes Here
NSString *tweet = [[NSString alloc] initWithFormat:#"My Tweet!"];
[_engine sendUpdate:tweet];
NSLog(#"You just tweeted: %#",tweet);
}
}
Any help is appreciated. Thanks so much!
The solution I posted previously didn't work %100, turned out I didn't have the right delegate set for login success in my controller:
#pragma mark SA_OAuthTwitterController Delegate
- (void) OAuthTwitterController: (SA_OAuthTwitterController *) controller authenticatedWithUsername: (NSString *) username {
NSLog(#"Authenticated with user %#", username);
tweets = [[NSMutableArray alloc] init];
[self updateStream:nil];
}
Hope that helps anyone!