1
我正在嘗試在應用程序中集成foursquare API,成功登錄並獲取authtoken。現在我需要使用authtoken登錄用戶信息,如何獲取用戶信息(如用戶名和emailId等)。請幫幫我。如何在iOS中使用swift獲取auth令牌後獲得四方用戶信息
我正在嘗試在應用程序中集成foursquare API,成功登錄並獲取authtoken。現在我需要使用authtoken登錄用戶信息,如何獲取用戶信息(如用戶名和emailId等)。請幫幫我。如何在iOS中使用swift獲取auth令牌後獲得四方用戶信息
你可以這樣做是這樣的:
// 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()
如果使用https://github.com/SwiftyJSON/SwiftyJSON,你可以處理JSON數據很容易。