Reputation:
i'm using Fabric in order to add a "Sign In with Twitter" button in my app. Sign in process works fine, but I don't how (if it's possible) how to get the logged user's profile image.
Is there any property inside TWTRSession with this info (like userName and userID)? Looked for it but I didn't found anything?
[TWTRLogInButton buttonWithLogInCompletion:^(TWTRSession *session, NSError *error) {
// I get user data here...
// and need to get user Twitter's profile image
}];
Thanks
EDIT
Thanks to sak's answer, i figured out how to do it.
[TWTRLogInButton buttonWithLogInCompletion:^(TWTRSession *session, NSError *error) {
[[[Twitter sharedInstance] APIClient] loadUserWithID:session.userID completion:^(TWTRUser *user, NSError *error) {
NSLog(@"User image %@", user.profileImageURL);
}];
}];
Upvotes: 12
Views: 7002
Reputation: 1945
To get the picture large size, you can build a URL as follow:
if let twitterId = session.userID{
let twitterClient = TWTRAPIClient(userID: twitterId)
twitterClient.loadUser(withID: twitterId) {(user, error) in
if let userName = user?.screenName{
let url = "https://twitter.com/\(userName)/profile_image?size=original")
}
}
}
This approach uses the twitter user name, then it will be redirected to the real profile picture size.
Upvotes: 3
Reputation: 16820
For those who are getting this error :
Value of type 'Twitter' has no member 'APIClient'
This helped me,
let twitterClient = TWTRAPIClient(userID: userID)
twitterClient.loadUserWithID(userID) { (user:TWTRUser?, error:NSError?) in
print(user?.profileImageURL)
}
Upvotes: 14
Reputation: 4737
Try below code to get profile image -
Swift 3
let twitterClient = TWTRAPIClient(userID: session?.userID)
twitterClient.loadUser(withID: (session?.userID)!, completion: { (user, error) in
print(user!.profileImageURL)
})
Upvotes: 8
Reputation: 61009
Here is how I get userId,userName,authToken,authTokenSecret and profileImageURL
[[Twitter sharedInstance] logInWithCompletion:^(TWTRSession *session, NSError *error) {
if (session != nil) {
SPTWUser *user = [[SPTWUser alloc] init];
user.userID = session.userID;
user.userName = session.userName;
user.authToken = session.authToken;
user.authTokenSecret = session.authTokenSecret;
TWTRAPIClient *client = [[TWTRAPIClient alloc] initWithUserID:user.userID];
[client loadUserWithID:user.userID completion:^(TWTRUser * _Nullable user, NSError * _Nullable error) {
NSLog(@"%@",user.profileImageURL);
}];
}
}];
Reference TWTRAPIClient Class, TWTRUser Class,TWTRSession Class
Upvotes: 1
Reputation: 4858
Try this:
TWTRLogInButton *logInButton = [TWTRLogInButton buttonWithLogInCompletion:^(TWTRSession *session, NSError *error) {
// play with Twitter session
if (session) {
NSLog(@"Twitter signed in as -> name = %@ id = %@ ", [session userName],[session userID]);
/* 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]);
}
}];
} else {
NSLog(@"Twitter error signed in : %@", [error localizedDescription]);
}
}];
logInButton.center = self.view.center;
[self.view addSubview:logInButton];
Upvotes: 6
Reputation: 865
[[[Twitter sharedInstance] APIClient] loadUserWithID:[session userID] completion:^(TWTRUser *user, NSError *error) {
}];
Upvotes: 2
Reputation: 73
The way to do it in swift is as follows:
Twitter.sharedInstance().APIClient.loadUserWithID( session.userID )
{
(user, error) -> Void in
if( user != nil )
{
println( user.profileImageURL )
}
}
I hope this helps.
Upvotes: 7
Reputation: 3307
I don't think the TWTRSession object has the user's image, but you can use the session to request a TWTRUser object, and that will have what you need.
Upvotes: 5