Reputation: 183
The following code is supposed to ask for access and then retrieve the user's twitter account in order to sync the account with the app for iOS. The code for what I am trying to do is below.
- (IBAction)connectToTwitter:(id)sender {
// borrowed from: http://eflorenzano.com/blog/2012/04/18/using-twitter-ios5-integration-single-sign-on/
ACAccountStore *store = [[ACAccountStore alloc] init];
ACAccountType *twitterType = [store accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[store requestAccessToAccountsWithType:twitterType options:nil completion:^(BOOL granted, NSError *error)
{
if(granted) {
// Access has been granted, now we can access the accounts
// Remember that twitterType was instantiated above
NSArray *twitterAccounts = [store accountsWithAccountType:twitterType];
// If there are no accounts, we need to pop up an alert
if(twitterAccounts != nil && [twitterAccounts count] == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Twitter Accounts"
message:@"There are no Twitter accounts configured. You must add or create a Twitter separately."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
} else {
ACAccount *account = [twitterAccounts objectAtIndex:0];
// Do something with their Twitter account
NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/account/verify_credentials.json"];
SLRequest *req = [SLRequest requestForServiceType:SLServiceTypeTwitter
requestMethod:SLRequestMethodPOST
URL:url
parameters:nil];
// Important: attach the user's Twitter ACAccount object to the request
req.account = account;
[req performRequestWithHandler:^(NSData *responseData,
NSHTTPURLResponse *urlResponse,
NSError *error)
{
// If there was an error making the request, display a message to the user
if(error != nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Twitter Error"
message:@"There was an error talking to Twitter. Please try again later."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
return;
}
// Parse the JSON response
NSError *jsonError = nil;
id resp = [NSJSONSerialization JSONObjectWithData:responseData
options:0
error:&jsonError];
// If there was an error decoding the JSON, display a message to the user
if(jsonError != nil)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Twitter Error"
message:@"Twitter is not acting properly right now. Please try again later."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
return;
}
NSString *screenName = [resp objectForKey:@"screen_name"];
self.twitterHandle = screenName;
PFUser *currentUser = [PFUser currentUser];
PFQuery *query = [PFQuery queryWithClassName:@"_User"];
[query whereKey:@"username" equalTo:currentUser.username];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// Do something with the found objects
for (PFObject *object in objects)
{
object[@"TwitterHandle"] = self.twitterHandle;
[object saveInBackground];
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}];
}
}
}];
}
Currently, this code works in the simulator in that it detects that there are no Twitter
accounts with which to sync. But, on an actual device, when a Twitter account is logged in on the Twitter app on the phone, the app crashes after the user allows access. I do not know where the problem is with my code. It is quite difficult because it runs fine in the simulator, but you cannot download Twitter to put accounts on the simulator.
First, what is wrong with my code, how can I fix it? Second, how can I test this in the simulator so that I don't have to keep redownloading the app to my device every time I make a simple change and want to test it?
Upvotes: 2
Views: 1183
Reputation: 16543
iOS default checks the Twitter account in the iOS settings and not by Twitter app. Check there
You can add a exception breakpoint to find out where the crash is. Check my answer here to find the crash location. Crash that not able to get
EDIT: Ok i just put your code and tested myself. Everything in your code works i am able to retrieve the information upto this line.
NSString *screenName = [resp objectForKey:@"screen_name"];
self.twitterHandle = screenName;
That is twitter returns the data correctly and i NSlogged the screenname and i got it.
But just one minor change verifyCredentials service is a GET method just change the SLRequest requestMethod as SLRequestMethodGET
You have created some object called PFUser and PFQuery i am not able to get that probably you should check that
Upvotes: 2