Reputation: 4678
Haven't found anything about this in Swift, so i'm asking! How can I get the profile Image of a logged in User with Parse from Twitter?
The User is already logged in via the parse internal login-/signupController
var username = PFUser.currentUser()?.username
And the image should be stored in an UIImage variable
Upvotes: 1
Views: 1572
Reputation: 236
André, to get profile image of a logged user with parse from twitter, you would need to make a Twitter API request to http://api.twitter.com/1/users/show.json as documented here => https://dev.twitter.com/rest/reference/get/users/show.
Ideally, You can authenticate this request using the PFTwitterUtils class.
This is how I did in my app: (Its in Objective C), hope it will help you.
- (void)loginWithTwitter {
[PFTwitterUtils logInWithBlock:^(PFUser *user, NSError *error) {
if (!user) {
NSLog(@"Uh oh. The user cancelled the Twitter login.");
[[NSNotificationCenter defaultCenter] postNotificationName:notificationUserLoginFailed
object:error];
return;
} else {
// Fetch Twitter details
NSString * requestString = [NSString stringWithFormat:@"https://api.twitter.com/1.1/users/show.json?screen_name=%@", user.username];
NSURL *verify = [NSURL URLWithString:requestString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:verify];
[[PFTwitterUtils twitter] signRequest:request];
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:request
returningResponse:&response
error:&error];
if ( error == nil){
NSDictionary* result = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
_NSLog(@"%@",result);
[user setObject:[result objectForKey:@"profile_image_url_https"]
forKey:@"picture"];
// does this thign help?
[user setUsername:[result objectForKey:@"screen_name"]];
NSString * names = [result objectForKey:@"name"];
NSMutableArray * array = [NSMutableArray arrayWithArray:[names componentsSeparatedByString:@" "]];
if ( array.count > 1){
[user setObject:[array lastObject]
forKey:@"last_name"];
[array removeLastObject];
[user setObject:[array componentsJoinedByString:@" " ]
forKey:@"first_name"];
}
[user saveInBackground];
}
[[NSNotificationCenter defaultCenter] postNotificationName:notificationUserDidLogin
object:nil];
return;
}
}];
}
Upvotes: 2
Reputation: 181
AHere is the Swift version. I had to change the username from Parse to screenName from PFTwitter, and added a line to get the highest resolution version of the picture. Note that as I'm using this to populate a sign up screen after logging in, this will test for the app account being connected to a Twitter account. Most probably this would work even if the link has been added after account creation, too.
if PFTwitterUtils.isLinkedWithUser(PFUser.currentUser()!) {
let screenName = PFTwitterUtils.twitter()?.screenName!
let requestString = ("https://api.twitter.com/1.1/users/show.json?screen_name=" + screenName!)
let verify: NSURL = NSURL(string: requestString)!
let request: NSMutableURLRequest = NSMutableURLRequest(URL: verify)
PFTwitterUtils.twitter()?.signRequest(request)
var response: NSURLResponse?
var error: NSError?
let data: NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)!
if error == nil {
let result = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: &error)
let names: String! = result?.objectForKey("name") as! String
let separatedNames: [String] = names.componentsSeparatedByString(" ")
self.firstName = separatedNames.first!
self.lastName = separatedNames.last!
let urlString = result?.objectForKey("profile_image_url_https") as! String
let hiResUrlString = urlString.stringByReplacingOccurrencesOfString("_normal", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
let twitterPhotoUrl = NSURL(string: hiResUrlString)
let imageData = NSData(contentsOfURL: twitterPhotoUrl!)
let twitterImage: UIImage! = UIImage(data:imageData!)
}
You probably want to add something like this to prepare a PFFile to save to your new user, too!
let cgImage = twitterImage.CGImage
let bitsPerComponent = CGImageGetBitsPerComponent(cgImage)
let bytesPerRow = CGImageGetBytesPerRow(cgImage)
let colorSpace = CGImageGetColorSpace(cgImage)
let bitmapInfo = CGImageGetBitmapInfo(cgImage)
let context = CGBitmapContextCreate(nil, 300, 300, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo)
CGContextSetInterpolationQuality(context, kCGInterpolationHigh)
CGContextDrawImage(context, CGRect(origin: CGPointZero, size: CGSize(width: CGFloat(300), height: CGFloat(300))), cgImage)
let scaledImage = UIImage(CGImage: CGBitmapContextCreateImage(context))
let imageUIImage = UIImageJPEGRepresentation(scaledImage, 0.6)
let imageFile: PFFile = PFFile(name: (PFUser.currentUser().objectId! + "profileImage.jpg"), data:imageUIImage)
Upvotes: 4