Ios 如何为我的登录设置API请求?
我使用Swift创建的用户名和密码字段进行登录和注册。我需要将其附加到下面的API。当我点击register时,它会创建一个新用户。当我登录时,它应该创建并存储访问令牌。登录或注册后,我会被带到一个tableview,其中显示我自己的当前用户以及tableview中的所有其他用户。我可以在tableView中编辑或删除用户。我该怎么做?这方面的信息将非常有用,因为我对RESTAPI的了解有限。例如,这是一个假的URI。谢谢大家! API参考 URI相对于:https://myloginapi.com/api/v1 -模拟URI 身份验证令牌 为用户创建新的访问令牌并返回它 要求 回答 user.get 按id获取一个用户并返回其数据 要求 回答 user.update 使用请求正文按id更新一个用户并返回其数据 要求 回答 user.create 使用请求正文创建一个新用户并返回其数据 要求 回答 user.delete 按id删除一个用户并返回空响应 要求 回答 用户列表 列出所有用户并返回一个集合 要求 回答 使用阿拉莫菲尔 样本请求将是:Ios 如何为我的登录设置API请求?,ios,json,swift,rest,Ios,Json,Swift,Rest,我使用Swift创建的用户名和密码字段进行登录和注册。我需要将其附加到下面的API。当我点击register时,它会创建一个新用户。当我登录时,它应该创建并存储访问令牌。登录或注册后,我会被带到一个tableview,其中显示我自己的当前用户以及tableview中的所有其他用户。我可以在tableView中编辑或删除用户。我该怎么做?这方面的信息将非常有用,因为我对RESTAPI的了解有限。例如,这是一个假的URI。谢谢大家! API参考 URI相对于:https://myloginapi.c
Alamofire.request("https://myloginapi.com/api/v1").responseJSON { response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
有关详细信息,请查看他们的文档:您可以使用URLSession
let urlconfig = URLSessionConfiguration.default
urlconfig.timeoutIntervalForRequest = 20
urlconfig.timeoutIntervalForResource = 60
let session = Foundation.URLSession(configuration: urlconfig, delegate: self, delegateQueue: OperationQueue.main)
let request = NSMutableURLRequest(url: URL(string:"https://myloginapi.com/api/v1")) // Set Url here
request.httpMethod = "POST" //(can set GET/POST)
request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringCacheData
let paramString = String(format:"name=%@&pass=%@",username,password) //Set Parameter string here
request.httpBody = paramString.data(using: String.Encoding.utf8)
task = session.dataTask(with: request as URLRequest) {
(data, response, error) in
do {
if data == nil
{
//Failure
}
else
{
if let json = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary {
print(json) // Json response from server
}
}
}
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Path | `GET` /user/`(id)` |
| Headers | Authorization: `(access_token)` |
| Body | ~~(empty)~~ |
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Body | `JSON` {"data":`user`} |
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Path | `PUT` /user/`(id)` |
| Headers | Authorization: `(access_token)` |
| Body | `JSON` {"name":"`(username)`","pass":"`(password)`"} |
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Body | `JSON` {"data":`user`} |
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Path | `POST` /user |
| Headers | ~~(empty)~~ |
| Body | `JSON` {"name":"`(username)`","pass":"`(password)`"} |
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Body | `JSON` {"data":`user`} |
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Path | `DELETE` /user/`(id)` |
| Headers | Authorization: `(access_token)` |
| Body | ~~(empty)~~ |
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Body | ~~(empty)~~ |
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Path | `GET` /user |
| Headers | Authorization: `(access_token)` |
| Body | ~~(empty)~~ |
| Param | Value |
| ------------ | ---------------------------------------------------- |
| Body | `JSON` {"data":[`users`]}
Alamofire.request("https://myloginapi.com/api/v1").responseJSON { response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
let urlconfig = URLSessionConfiguration.default
urlconfig.timeoutIntervalForRequest = 20
urlconfig.timeoutIntervalForResource = 60
let session = Foundation.URLSession(configuration: urlconfig, delegate: self, delegateQueue: OperationQueue.main)
let request = NSMutableURLRequest(url: URL(string:"https://myloginapi.com/api/v1")) // Set Url here
request.httpMethod = "POST" //(can set GET/POST)
request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringCacheData
let paramString = String(format:"name=%@&pass=%@",username,password) //Set Parameter string here
request.httpBody = paramString.data(using: String.Encoding.utf8)
task = session.dataTask(with: request as URLRequest) {
(data, response, error) in
do {
if data == nil
{
//Failure
}
else
{
if let json = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary {
print(json) // Json response from server
}
}
}