How to get foursquare user information after getting auth token in iOS using swift

287 Views Asked by At

I am trying to integrate foursquare api in application , logged in successfully and getting authtoken . Now i need to get loggedin user information using authtoken, how to get userinfo(like username and emailId and etc) . Please help me.

1

There are 1 best solutions below

0
On BEST ANSWER

You can do so like this:

    // Construct url object via string
    let url = NSURL(string: "https://api.foursquare.com/v2/users/self?oauth_token=(YOUR_OAUTH_TOKEN)&v=20160207")
    let config = NSURLSessionConfiguration.defaultSessionConfiguration()
    let session = NSURLSession(configuration: config)
    let req = NSURLRequest(URL: url!)

    // NSURLSessionDownloadTask is retured from session.dataTaskWithRequest
    let task = session.dataTaskWithRequest(req, completionHandler: {
        (data, resp, err) in
        print(NSString(data: data!, encoding: NSUTF8StringEncoding))

        do {
            let json = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)

            if let response = json["response"] as? [String: AnyObject] {
                if let user = response["user"] as? [String: AnyObject] {
                    if let name = user["firstName"] as? String {
                        // User name
                        print(name)
                    }
                }
            }
        } catch {
            print("error serializing JSON: \(error)")
        }

    })
    task.resume()

If you use https://github.com/SwiftyJSON/SwiftyJSON, you can deal with JSON data easily.