你遇到了什么错误?
在与您的 Web 服务 API 进行通信时,您可能需要查看Alamofire,它易于使用、为您完成大部分繁重的工作,并且拥有一个非常扎实的支持社区。
以下是一些使用 Alamofire 的示例,您必须填写一些空白,但这是一个开始。
这是我使用 Swift 3 使用 Alamofire 对用户进行身份验证的方法
func authenticateAsync(email: String, password: String, completion: @escaping (_ token: String?, _ status: String?, _ error: String?, _ response: DataResponse<Any>) -> Void) {
let accumooParameters = self.formatAuthenticationParameters(email: email, password: password)
let dispatchQueue = DispatchQueue(label: "com.mysite.api-response-queue",
qos: DispatchQoS.userInitiated,
attributes: DispatchQueue.Attributes.concurrent)
Alamofire.request(Globals.Accumoo.Urls.Authentication.authenticateUser,
method: HTTPMethod.post,
parameters: accumooParameters,
encoding: JSONEncoding.default,
headers: ["Content-Type":"application/json; charset=UTF-8"])
.validate(statusCode: 200..<300)
.responseJSON(queue: dispatchQueue, options: JSONSerialization.ReadingOptions.allowFragments, completionHandler: {
(response: DataResponse<Any>) in
switch response.result {
case .success(let data):
let json:JSON = JSON(data)
print(json.rawString()!)
let result = self.getAuthenticationResult(json: json)
completion(result.token, result.status, result.error, response)
case .failure(let error):
completion("", String(describing: response.response?.statusCode ?? 0), String(error._code), response)
}
})
}
internal func formatAuthenticationParameters(email: String, password: String) -> [String : Any]? {
return ["email" : email, "password" : password]
}
internal func getAuthenticationResult(json: JSON) -> (token: String?, status: String?, error: String?) {
let token = json["token"].string
let status = json["status"].string
let error = json["error"].string
return getAuthenticationResult(token: token, status: status, error: error)
}
internal func getAuthenticationResult(token: String?, status: String?, error: String?) -> (token: String?, status: String?, error: String?) {
return (token: token, status: status, error: error)
}
这是我使用 Alamofire 在同一个应用程序中使用 Swift 3 发布经过身份验证的请求的方法
// Update full name
func updateFullName(with accessToken: String, fullName: String, completion: @escaping (DataResponse<Any>) -> Void) {
let url = "https://api-mysite.herokuapp.com/mysite/users/update_user/"
let params = ["user" : ["full_name" : fullName]]
put(with: accessToken, url: url, params: params, completion: { (dataResponse: DataResponse<Any>) in
completion(dataResponse)
})
}
// Post authenticated
internal func post(with accessToken: String, url: String, params: [String : Any], completion: @escaping (DataResponse<Any>) -> Void) {
let dispatchQueue = DispatchQueue(label: "com.mysite.api-response-queue",
qos: DispatchQoS.userInitiated,
attributes: DispatchQueue.Attributes.concurrent)
Alamofire.request(url,
method: HTTPMethod.post,
parameters: params,
encoding: JSONEncoding.default,
headers: ["Content-Type" : "application/json; charset=UTF-8; version=1",
"Authorization" : accessToken])
.validate(statusCode: 200..<300)
.validate(contentType: ["application/json"])
.responseJSON(
queue: dispatchQueue,
options: JSONSerialization.ReadingOptions.allowFragments,
completionHandler: { (response: DataResponse<Any>) in completion(response) }
)
}
祝你好运。