title: Custom Twitter login with Swift and Parse
body: <p>I've been stuck on how to implement a custom Twitter login with Parse for my Swift app for a while now. Most resources I've seen for Twitter login with iOS are for Objective-C, and I've tried changing the suggested solutions into Swift, but no luck so far. This is my current code:</p>
  
  <pre><code>@IBAction func twitterLogIn(sender: AnyObject) {
      PFTwitterUtils.logInWithBlock( { (user: PFUser?, error: NSError?) -&gt; Void in
          // apparently both user and currentUser are nil...
          if PFTwitterUtils.isLinkedWithUser(PFUser.currentUser()) {
              var info: NSURL! = NSURL(string: "https://api.twitter.com/1.1/account/settings.json")
              var request: NSMutableURLRequest = NSMutableURLRequest(URL: info)
              NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: { (response: NSURLResponse!, data: NSData!, connectionError: NSError!) -&gt; Void in
                  if let d = data {
                      var dict: NSDictionary = NSJSONSerialization.JSONObjectWithData(d, options: NSJSONReadingOptions.MutableLeaves, error: nil) as! NSDictionary
                      // do stuff with user's profile info
                  } else {
                      println("Apparently no Twitter response.")
                  }
              })
          } else {
              println("Apparently failed login.")
          }
      })
  }
  </code></pre>
  
  <p>This prints out <code>Apparently failed login.</code> I've also tried this:</p>
  
  <pre><code>PFTwitterUtils.logInWithBlock( { (user: PFUser?, error: NSError?) -&gt; Void in
      if let user = user {
          if user.isNew {
              println("User signed up and logged in with Twitter.")
              // put information in user object
              user.save()
          } else {
              println("User logged in with Twitter.")
          }
      } else {
          println("User was nil.")
      }
  })
  </code></pre>
  
  <p>It printed out <code>User was nil.</code> And I also tried this:</p>
  
  <pre><code>if let twitter = PFTwitterUtils.twitter() {
      if twitter.userId != nil &amp;&amp; twitter.screenName != nil &amp;&amp; twitter.authToken != nil &amp;&amp; twitter.authTokenSecret != nil {
          PFTwitterUtils.logInWithTwitterId(twitter.userId!, screenName: twitter.screenName!, authToken: twitter.authToken!, authTokenSecret: twitter.authTokenSecret!, block: {  (user: PFUser?, error: NSError?) -&gt; Void in
              if let user = user {
                  if user.isNew {
                      println("User signed up and logged in with Twitter.")
                      // put info in user object
                      user.save()
                  } else {
                      println("User logged in with Twitter.")
                  }
              } else {
                  println("User was nil. Something went wrong. Did the user cancel the login?")
              }
          })
      } else {
          println("Couldn't access a Twitter account on this device.")
      }
  } else {
      println("Couldn't find a Twitter account on this device.")
  }
  </code></pre>
  
  <p>Which printed <code>Couldn't access a Twitter account on this device.</code> The only other option I know of right now is to use the built-in login view controller, but I'd rather use a custom one. How can I implement a custom Twitter login function?</p>
  
  <p>Edit: I tried another method that didn't work yet again:</p>
  
  <pre><code>PFTwitterUtils.logInWithBlock( { (user: PFUser?, error: NSError?) -&gt; Void in
      // apparently both user and currentUser are nil...
      println(2)
      if !(PFTwitterUtils.isLinkedWithUser(user)) {
          println(3)
          PFTwitterUtils.linkUser(user!, block: { (succeeded: Bool, error: NSError?) -&gt; Void in
              println(4)
              if succeeded {
                  println("User linked with Twitter.")
              }
          })
      }
      println(5)
      if let u = user {
          println(6)
          if u.isNew {
              println("User signed up and logged in with Twitter.")
  
              // put info in user object
  
              var info: NSURL! = NSURL(string: "https://api.twitter.com/1.1/account/settings.json")
              println(7)
              var request: NSMutableURLRequest = NSMutableURLRequest(URL: info)
              println(8)
              NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue(), completionHandler: { (response: NSURLResponse!, data: NSData!, connectionError: NSError!) -&gt; Void in
                  println(9)
                  if let d = data {
                      println(10)
                      var dict: NSDictionary = NSJSONSerialization.JSONObjectWithData(d, options: NSJSONReadingOptions.MutableLeaves, error: nil) as! NSDictionary
                      // do stuff with this data
                  } else {
                      println("Apparently no Twitter response.")
                  }
              })
          } else {
              println("User logged in with Twitter.")
          }
      } else {
          println("User was nil.")
      }
  })
  </code></pre>
  
  <p>This prints up to <code>3</code> and then complains of a nil optional value, most likely <code>user</code>.</p>
  
  <p>Edit: I've been testing Fabric as a possible solution and although it provides a good Twitter login method, I can't figure out how to use it with Parse, specifically, how to create and handle a corresponding user object as I can't access the user's password. I'd much prefer to use <code>PFTwitterUtils</code> for the best integration with Parse, but I'm running out of options for that. Can anyone help?</p>