gpt4 book ai didi

ios - 在 HTTP 方法中使用 POST 请求保存 UI?

转载 作者:行者123 更新时间:2023-11-28 07:54:02 25 4
gpt4 key购买 nike

当我点击按钮时,它将执行服务请求操作。根据结果,它将重定向到下一个 View Controller 。加载 Next View Controller 后,会保留或阻止 UI。如何解决这个问题?我是第一次快速使用 RestAPI 和 GCD,所以不知道如何解决这个问题......

这是登录按钮

@IBAction func btnLogin(_ sender: Any)
{
self.api()
}

这就是我们所说的函数。

func api()
{
let myURL = URL(string: "http://www.digi.com/laravel_api_demo/api/demoapipost")

let request = NSMutableURLRequest(url: myURL!)
request.httpMethod = "POST"



let strEmail = tfLgnID.text
let strPwd = tfPwd.text


let postString = ["username":strEmail, "password":strPwd]
//let postString = ["username":"ayush", "password":"abc"]


request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")

//create the session object
//let session = URLSession.shared

do {
request.httpBody = try JSONSerialization.data(withJSONObject: postString, options: .prettyPrinted) // pass dictionary to nsdata object and set it as request body
print("Successfully passed data to server")
} catch let error {
print(error.localizedDescription)
}

let postTask = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in

guard error == nil else {
return
}

guard let data = data else {
return
}

do {
//create json object from data
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
print("POST Method :\(json)")

let dict = json as? [String: Any]
let num = dict!["status"]
print("Status : \(num)")
print("Dict : \(dict)")
print("username : \(dict!["username"])")
print("password : \(dict!["password"])")

if dict!["status"] as! Int == 1
{
print("Successfully Logged In")


DispatchQueue.main.async {
let visitorVC = self.storyboard?.instantiateViewController(withIdentifier: "VisitorVC") as! VisitorVC
self.present(visitorVC, animated: true, completion: nil)

}

print("OK")
}
else
{
print("Not OK")
}



// handle json...
}
} catch let error {
print(error.localizedDescription)
}
}
postTask.resume()


}

最佳答案

试试这个方法

  func api() {
var request = URLRequest(url: URL(string: "")!) // put your url
request.httpMethod = "POST"
let strEmail = tfLgnID.text
let strPwd = tfPwd.text
let postString:String = "user_id=\(strEmail)&user_id=\(strPwd)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
do {
if let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary {
print(jsonResult)
let status = jsonResult["status"]! as! NSString
print("status\(status)")

DispatchQueue.main.async(execute: {
// your error Alert

})

}
else {
DispatchQueue.main.async(execute: {
let visitorVC = self.storyboard?.instantiateViewController(withIdentifier: "VisitorVC") as! VisitorVC
self.present(visitorVC, animated: true, completion: nil)


})
}
}
} catch let error as NSError {
print(error.localizedDescription)
}

if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")

}

let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
}
task.resume()

}

关于ios - 在 HTTP 方法中使用 POST 请求保存 UI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48802916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com