不要忘記導入Alamofire
和SwiftyJSON
。
// This is Alamofire Request
func LoginWithAPI(completion : (String) ->()) {
Alamofire.request(.POST, "your_api_url", parameters: ["username":"bodrum", "password":"yalikavak"], encoding: ParameterEncoding.JSON)
.validate()
.responseJSON { response in
switch response.result {
case .Success:
// Parse your result to JSON object first.
// JSON is SwiftyJSON object.
let jsonResponse = JSON(data: response.data!)
let result = jsonResponse["result"].stringValue
completion(result)
case .Failure(let error):
completion(error.description)
}
}
}
,如果你想添加任何header
,使用該行:正如你在評論說
Alamofire.request(.POST, "your_api_url", parameters: ["username":"bodrum", "password":"yalikavak"], encoding: ParameterEncoding.JSON,
headers: ["header_key": "header_value"])
你有什麼需要幫助用?從服務器獲取JSON或解析生成的JSON? – rmaddy
最後 – Sascha
您可以發佈一些示例代碼或發佈您收到的錯誤嗎? – Anchor