NSPratik NSPratik - 8 months ago 118
iOS Question

Get user profile details (especially email address) from twitter in iOS

I have been searching since couple of days on how I can get user details based on his/her Twitter account. Now first of all, let me explain what I want to do.

In my case, user will be presented an option to Sign-up with Twitter account. So, based on user twitter account, I want to be able to get user details (e.g. email id, name, profile picture, birth date, gender etc..) and save those details in database. Now, many people will probably suggest me to use ACAccount and ACAccountStore, a class that provides an interface for accessing, manipulating, and storing accounts. But, in my case, I want to sign up user, even if user has not configured an account for Twitter in Settings App. I want user to take to login screen of Twitter (in safari or in App itself, or using any other alternative).

I have also referred to the documentation of Twitter having API list here. But I am lot confused how user should be presented a login screen to log into the Twitter account and I will get profile information. Should I use UIWebView, or redirect user to Safari or adopt another way for that.

Please provide some code snippet if possible.

Thanks in Advance, I am desperate to the solution !!!


Twitter has provided a beautiful framework for that, you just have to integrate it in your app.


It has a simple Login Method:-

// Objective-C
TWTRLogInButton* logInButton =  [TWTRLogInButton
                                     ^(TWTRSession* session, NSError* error) {
    if (session) {
         NSLog(@"signed in as %@", [session userName]);
    } else {
         NSLog(@"error: %@", [error localizedDescription]);
logInButton.center = self.view.center;
[self.view addSubview:logInButton];

This is the process to get user profile information:-

/* Get user info */
        [[[Twitter sharedInstance] APIClient] loadUserWithID:[session userID]
                                                  completion:^(TWTRUser *user,
                                                               NSError *error)
            // handle the response or error
            if (![error isEqual:nil]) {
                NSLog(@"Twitter info   -> user = %@ ",user);
                NSString *urlString = [[NSString alloc]initWithString:user.profileImageLargeURL];
                NSURL *url = [[NSURL alloc]initWithString:urlString];
                NSData *pullTwitterPP = [[NSData alloc]initWithContentsOfURL:url];

                UIImage *profImage = [UIImage imageWithData:pullTwitterPP];

            } else {
                NSLog(@"Twitter error getting profile : %@", [error localizedDescription]);

I think rest you can find from Twitter Kit Tutorial, it also allows to request a user’s email, by calling the TwitterAuthClient#requestEmail method, passing in a valid TwitterSession and Callback.