gpt4 book ai didi

swift - iOS swift 3.0 Json 解析和警报问题

转载 作者:搜寻专家 更新时间:2023-11-01 06:02:02 25 4
gpt4 key购买 nike

我正在处理登录表单。我刚接触 iOS 开发。

登录成功后,我想在json解析完成后显示一个提示。我已经在 do while block 中解析了 Ngoid。现在我想将值“Ngoid”传递给下一个 View Controller ,以便它可用于获取更多数据。

主要问题:这是我编写的代码,如果只在主线程上编写警报,就会出错。

因为我想在那里进一步使用“Ngoid”值,所以我应该如何编写它以及执行代码的正确方法是什么?

这是我写的代码:

@IBAction func loginbutton(_ sender: Any) {
let myUrl = NSURL(string: "http://www.shreetechnosolution.com/funded/ngo_login.php")

let request = NSMutableURLRequest(url:myUrl! as URL)

request.httpMethod = "POST"// Compose a query string

let postString = "uname=\(textfieldusername.text!)&password=\(textfieldpassword.text!)";

request.httpBody = postString.data(using: String.Encoding.utf8)



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

if error != nil
{
//let alert = UIAlertView()
let alert = UIAlertController(title: "Alert Box !", message: "Login Failed", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
return
}

// You can print out response object
print("*****response = \(String(describing: response))")

let responseString = NSString(data: data! , encoding: String.Encoding.utf8.rawValue )

if ((responseString?.contains("")) == nil) {
print("incorrect - try again")

let alert = UIAlertController(title: "Try Again", message: "Username or Password Incorrect", preferredStyle: .alert)
let yesAction = UIAlertAction(title: "Nochmalversuchen", style: .default) { (action) -> Void in

}


// Add Actions
alert.addAction(yesAction)


// Present Alert Controller
self.present(alert, animated: true, completion: nil)
}

else {
print("correct good")


}

print("*****response data = \(responseString!)")




do {
//create json object from data

if let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary {

if let email = json["UserName"] as? String,
let password1 = json["passowrd"] as? String {

print ("Found User id: called \(email)")
}
let msg = (json.value(forKey: "message") as! NSString!) as String
let id = (json.value(forKey: "NgoId") as! NSString!) as String


// let alert : UIAlertView = UIAlertView(title: "Alert box!", message: "\(msg!).",delegate: nil, cancelButtonTitle: "OK")
// alert.show()

self.alert = UIAlertController(title: "Alert Box!", message: "\(msg)", preferredStyle: .alert)
print("the alert\(self.alert)")
self.action = UIAlertAction(title: "OK", style: .default) { (action) -> Void in
let viewControllerYouWantToPresent = self.storyboard?.instantiateViewController(withIdentifier: "pass1") as! ViewControllerngodetails

viewControllerYouWantToPresent.temp1 = self.id

self.present(viewControllerYouWantToPresent, animated: true, completion: nil)

}

self.alert.addAction(self.action)

self.present(self.alert, animated: true, completion: nil)



}




}catch let error {
print(error)
}



}
task.resume()

}

最佳答案

专业提示:

所有与 UI 相关的任务都需要在主线程中完成。在这里,您在后台线程中执行的闭包内显示警报,这就是问题所在。您需要调用主队列并在该 block 中显示警报。

编辑:

只需将您的警报代码放在此处-

对于 Swift 3-

异步获取主队列

DispatchQueue.main.async {
//Code Here
}

同步获取主队列

DispatchQueue.main.sync {
//Code Here
}

关于swift - iOS swift 3.0 Json 解析和警报问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46924622/

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