我对IOS和swift还不熟悉。我有带登录的应用程序。单击登录按钮,我将使用post方法检查用户名和密码。但是在成功的结果上,我想转到新的viewcontroller,但是它给了我错误的UI API,在后台线程上调用
按钮点击代码如下。
有没有像swift中的android那样的asynctask在postexecute上,然后告诉我。
谢谢你的帮助。
@IBAction func chkbt(_ sender: Any) {
var url = URL(string: "http://www.miappserver.co.in:8079/chs_server/rest/menuService/getLoginDetails")!
var request = URLRequest(url: url)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let postString = "{\"one\":\"*userid*\",\"two\":\"*password*\",\"three\":\"strimg\",\"authApiKey\":\"zpfkenaojjawlwjnbsoej-fhfbs\"}"; // which is your parameters
let idString : String = txtUserId.text!
let passString : String = txtPassword.text!
let newString1 = postString.replacingOccurrences(of: "*userid*", with: idString, options: .literal, range: nil)
let newString2 = newString1.replacingOccurrences(of: "*password*", with: passString, options: .literal, range: nil)
request.httpBody = newString2.data(using: .utf8)
// Getting response for POST Method
DispatchQueue.main.async {
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
return // check for fundamental networking error
}
// Getting values from JSON Response
let responseString = String(data: data, encoding: .utf8)
self.parseJSON(data)
var users = [Meal]()
do{
users = try JSONDecoder().decode(Array<Meal>.self, from: data)
print(users.first?.userId)
//print(users.first?.userPersonName)
if users.first?.userId != nil {
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "Dashboard") as! Dashboard
self.present(newViewController, animated: true, completion: nil)
}
}catch{
print(error)
}
//print("responseString = \(String(describing: responseString))")
//print(responseString)
do {
let jsonResponse = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions()) as? NSDictionary
}catch _ {
print ("OOps not good JSON formatted response")
}
}
task.resume()
}
}
最佳答案
你需要
DispatchQueue.main.async {
self.storyBoard!.instantiateViewController(withIdentifier: "Dashboard") as! Dashboard
self.present(newViewController, animated: true, completion: nil)
}
因为
URLSession.shared.dataTask(with: request)
在后台线程中运行,而不管您用DispatchQueue.main.async {
包围它@IBAction func chkbt(_ sender: Any) {
var url = URL(string: "http://www.miappserver.co.in:8079/chs_server/rest/menuService/getLoginDetails")!
var request = URLRequest(url: url)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
let postString = "{\"one\":\"*userid*\",\"two\":\"*password*\",\"three\":\"strimg\",\"authApiKey\":\"zpfkenaojjawlwjnbsoej-fhfbs\"}"; // which is your parameters
let idString : String = txtUserId.text!
let passString : String = txtPassword.text!
let newString1 = postString.replacingOccurrences(of: "*userid*", with: idString, options: .literal, range: nil)
let newString2 = newString1.replacingOccurrences(of: "*password*", with: passString, options: .literal, range: nil)
request.httpBody = newString2.data(using: .utf8)
// Getting response for POST Method
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
return // check for fundamental networking error
}
// Getting values from JSON Response
let responseString = String(data: data, encoding: .utf8)
self.parseJSON(data)
var users = [Meal]()
do{
users = try JSONDecoder().decode(Array<Meal>.self, from: data)
print(users.first?.userId)
//print(users.first?.userPersonName)
if users.first?.userId != nil {
DispatchQueue.main.async {
self.storyBoard!.instantiateViewController(withIdentifier: "Dashboard") as! Dashboard
self.present(newViewController, animated: true, completion: nil)
}
}
}catch{
print(error)
}
//print("responseString = \(String(describing: responseString))")
//print(responseString)
do {
let jsonResponse = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions()) as? NSDictionary
}catch _ {
print ("OOps not good JSON formatted response")
}
}
task.resume()
}