-2

1) I am trying to access twitter profile photo and upload it to the parse user object

2) Below is the code I am using.

if PFTwitterUtils.isLinkedWithUser(user){
        //copy data to parse user
        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(" ")
            //var firstName = separatedNames.first!
            //var 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!)

            if(imageData != nil) {
                let profileFileObject = PFFile(data:imageData!)
                user.setObject(profileFileObject, forKey: "profilePicture")
            }

            user.saveInBackgroundWithBlock({ (success:Bool, error:NSError?) -> Void in
                if(success) {
                    //println("User details are now updated")
                    user.pinInBackgroundWithBlock({ (pinUserSuccess:Bool, pinUserError:NSError?) -> Void in
                        if (pinUserSuccess){
                            println("User successfully pinned in twitter")
                        }else {
                            println("Error in pining the user")
                        }
                    })
                }
            })
        } else {
            println(error?.description)
        }

3) I am using Parse User Interface to sign in using twitter. That is working. I am able to access the screen name

4) I am testing this on a simulator

Question 1 -

In the above code I am getting an error for the below code. Please help!

let data: NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse: &response, error: &error)!

Error Domain=NSURLErrorDomain Code=-1012 \"The operation couldn’t be completed. (NSURLErrorDomain error -1012.)\" UserInfo=0x7fd11aab87a0 {NSErrorFailingURLStringKey=https://api.twitter.com/1.1/users/show.json?screen_name=jayskapadia, NSUnderlyingError=0x7fd11aab51b0 \"The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1012.)\", NSErrorFailingURLKey=https://api.twitter.com/1.1/users/show.json?screen_name=jayskapadia}

Question 2 - I also want to access the email id from the twitter profile. How do I do that?

rici
  • 234,347
  • 28
  • 237
  • 341
  • Also I forgot to add I used this link for reference http://stackoverflow.com/questions/29924749/getting-twitter-profile-image-with-parse-in-swift – Jay Kapadia Sep 07 '15 at 18:13

1 Answers1

1

the photo part now works with the below code

if PFTwitterUtils.isLinkedWithUser(user){
        //copy data to parse user.
        let screenName = PFTwitterUtils.twitter()?.screenName!

        let verify = NSURL(string: "https://api.twitter.com/1.1/account/verify_credentials.json")
        var request = NSMutableURLRequest(URL: verify!)
        PFTwitterUtils.twitter()!.signRequest(request)
        var response: NSURLResponse?
        var error: NSError?
        var 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(" ")
            //var firstName = separatedNames.first!
            //var 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!)

            if (screenName != nil){
                user.setObject(screenName!, forKey: "username")
            }

            if(imageData != nil) {
                let profileFileObject = PFFile(data:imageData!)
                user.setObject(profileFileObject, forKey: "profilePicture")
            }

            user.saveInBackgroundWithBlock({ (success:Bool, error:NSError?) -> Void in
                if(success) {
                    //println("User details are now updated")
                    user.pinInBackgroundWithBlock({ (pinUserSuccess:Bool, pinUserError:NSError?) -> Void in
                        if (pinUserSuccess){
                            println("User successfully pinned in twitter")
                        }else {
                            println("Error in pining the user")
                        }
                    })
                }
            })
        } else {
            println(error?.description)
        }
    }