Bhumika Bhumika -5 years ago 396
iOS Question

How to get user birthday from google plus using oauth 2 api in ios?

I have implemented Google plus signIn in my app and now i want to get user birthday via Google OAuth api.I have already get user birthday using google developer playground.but now i want to know than how can i get user birthday in iOS(swift) programmatically?

Any help will be appreciated.

Answer Source

You can get birthdate from Google plus using oauth api in this way:

First of all you have to set scope to get birthdate

signIn.scopes = ["me", "profile"]

let url : String = ""
    let request : NSMutableURLRequest = NSMutableURLRequest()
    request.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
    request.URL = NSURL(string: url)
    request.HTTPMethod = "GET"
    NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue()) { (response : NSURLResponse?, data:NSData?, error:NSError?) -> Void in
            let jsonResult : NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
            print("Birthdate   : \(jsonResult.valueForKey("birthday"))")
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download