Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Swift + Parse + Twitter

I am wondering how I can incorporate more info From Twitter into my App at Login.

I have the Twitter API setup so I am getting logged in through Twitter and it is collecting the username perfectly.

I would like to collect the profile image, first name and last name as well.

Thank you for all your help.

2 Answers

This shoud be set inside the class (I'm assuming you want this code inside a UIViewController, but it can be inside whichever class you want).

class WhateverNameYouHaveViewController : UIViewController {

    // Could also be "let firstName : String?" in one line and "let lastName : String?"
   // It depends on what you're doing.
    var firstName, lastName : String?

   // (...) Do whatever you need to do... viewWillAppear, etc.

   func viewDidLoad() // blablabla {

  // --> INSERT TWITTER CODE HERE.

   }



}

Currently the only way to do this is using the REST API and JSON.

Credits: http://stackoverflow.com/questions/29924749/getting-twitter-profile-image-with-parse-in-swift

If you're already succesfully logging in with Parse/Twitter, just add this:

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 : AnyObject? = 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!)
    }

}

I am getting an error with: self.firstName = separatedNames.first! self.lastName = separatedNames.last!

Here is the error: 'ViewController' does not have a member named 'firstName' 'ViewController' does not have a member named 'lastName'

Where do I have to set firstName and lastName?

Thank you