iosobjective-ctwittertwitter-oauthacaccountstore

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


I am aiming to get a user's 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's 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 iOS Settings App. I want user to navigate to login screen of Twitter (in Safari or in App itself, or using any other alternative).

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


Solution

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

    https://dev.twitter.com/twitter-kit/ios

    It has a simple Login Method:-

    // Objective-C
    TWTRLogInButton* logInButton =  [TWTRLogInButton
                                         buttonWithLogInCompletion:
                                         ^(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.