Get User Profile Details (Especially Email Address) from Twitter in iOS

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

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.

How to get user email address via Twitter API in iOS?

EDIT

After Twitter has updated APIs, Now user can get Email using TWTRShareEmailViewController class.

// Objective-C
if ([[Twitter sharedInstance] session]) {
TWTRShareEmailViewController* shareEmailViewController = [[TWTRShareEmailViewController alloc] initWithCompletion:^(NSString* email, NSError* error) {
NSLog(@"Email %@, Error: %@", email, error);
}];
[self presentViewController:shareEmailViewController animated:YES completion:nil];
} else {
// TODO: Handle user not signed in (e.g. attempt to log in or show an alert)
}

// Swift
if Twitter.sharedInstance().session {
let shareEmailViewController = TWTRShareEmailViewController() { email, error in
println("Email \(email), Error: \(error)")
}
self.presentViewController(shareEmailViewController, animated: true, completion: nil)
} else {
// TODO: Handle user not signed in (e.g. attempt to log in or show an alert)
}

NOTES:
Even if the user grants access to her email address, it is not guaranteed you will get an email address. For example, if someone signed up for Twitter with a phone number instead of an email address, the email field may be empty. When this happens, the completion block will pass an error because there is no email address available.

Twitter Dev Ref


PAST

There is NO way you can get email address of a twitter user.

The Twitter API does not provide the user's email address as part of the OAuth token negotiation process nor does it offer other means to obtain it.

Twitter Doc.

Getting Twitter user details using swift

You can access the username and userID of the logged-in user pretty easily. Inside most Twitter login methods you'll see something like this:

@IBAction func loginTwitter(sender: UIBarButtonItem) {
Twitter.sharedInstance().logInWithCompletion {
(session, error) -> Void in
if (session != nil) {

print(session?.userName)
print(session?.userID)
} else {
print("error")

}
}
}

Twitter does not expose the email address of users as far as I'm aware.

For the profile image you'll need to send a GET request. Here is some code that may not be up to date with the latest TwitterKit version but should at least give you a sense of how the request should be formatted.

func getUserInfo(screenName : String){
if let userID = Twitter.sharedInstance().sessionStore.session()!.userID {
let client = TWTRAPIClient(userID: userID)
let url = "https://api.twitter.com/1.1/users/show.json"
let params = ["screen_name": screenName]
var clientError : NSError?
let request = Twitter.sharedInstance().APIClient.URLRequestWithMethod("GET", URL: url, parameters: params, error: &clientError)

client.sendTwitterRequest(request) { (response, data, connectionError) -> Void in
if let someData = data {
do {
let results = try NSJSONSerialization.JSONObjectWithData(someData, options: .AllowFragments) as! NSDictionary
print(results)

} catch {
}
}
}
}
}

You'll need to go through the JSON that gets returned and find "profile_image_url_https" a couple levels down.

Good Luck!

How to get user account information from Twitter using objective-c?

what about this call

 https://api.twitter.com/1/users/show.json?screen_name=TwitterAPI&include_entities=true

Can I get the email address from a user sign-in with the Twitter framework in iOS 5+?

Unfortunately it seems that we cant get email address from twitter, so I had to change my signup mode by validation through the twitter login and users profile id. If anybody else has found a workaround please do post your solution. I'll mark that as best answer.



Related Topics



Leave a reply



Submit