我当前需要使用网络服务来执行一些任务,即登录和接收信息列表。
成功登录后,网络服务将返回“响应”信息:{"LoginID":"1","Password":"","Role":"pol","LoginType":"Indevidual ","UserID":"6110895204062016","UserRoleID":"20202020202020","RoleID":"999674512042008","PartyId":"1063081525122008","PartyFunctionId":"123123","BranchCode “:”10“, "RoleCode":"123123","Status":{"isError":false,"ErCode":null,"Error":null}}
需要将其发送到另一个网络服务以获取信息列表。
当前使用登录按钮调用Web服务才能登录。
如何使用第一个 Web 服务中的信息调用另一个 Web 服务?
更好的想法的代码:
@IBAction func GetPolicyListButton(_ sender: Any) {
//I will need the information from the second web service to display after clicking this button.. how?
}
@IBAction func LoginButton(_ sender: Any) {
let postString = "cpr=\(usernameField.text!)&password=\(passwordField.text!)"
let url = URL(string:"http://login")!
let postData:Data = postString.data(using: String.Encoding.utf8, allowLossyConversion: false)!
let postLength:String = String(postData.count) as String
var request:URLRequest = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = postData
request.setValue(postLength as String, forHTTPHeaderField: "Content-Length")
request.setValue("application/json", forHTTPHeaderField: "Accept")
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else {
print("error=\(error)")
return
}
let httpStatus = response as? HTTPURLResponse
print("statusCode should be 200, but is \(httpStatus!.statusCode)")
print("response = \(response!)")
print(postString)
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(responseString!)")
let start = responseString!.index(responseString!.startIndex, offsetBy: 75)
let end = responseString!.index(responseString!.endIndex, offsetBy: -9)
let range = start..<end
let jsonStr = responseString!.substring(with: range)
print(jsonStr)
let data1 = jsonStr.data(using: .utf8)!
_ = try? JSONSerialization.jsonObject(with: data1) as? [String: Any]
let persondata = try? JSONSerialization.jsonObject(with: data, options: .allowFragments)
let personInfodata = persondata as? [String : Any]
_ = personInfodata?[""] as? [String : Any]
if (responseString?.contains("1001"))!{
DispatchQueue.main.async {
print("incorrect - try again")
let alert = UIAlertController(title: "Try Again", message: "Username or Password Incorrect", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
else{
DispatchQueue.main.async {
print("correct good")
let storyboard = UIStoryboard(name: "Maintest", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "correctone")
self.present(controller, animated: true, completion: nil)
}
}
}
task.resume()
}
最佳答案
您正在经历不在 MVC 工作的复杂性。在编写应用程序时,如果没有正确使用 MVC,复杂性和不必要的代码重复可能会失控,并且您会失去监督。
例如,由于缺乏更好的名称,要使用的样式是创建 LoginModel 和 ItemsModel。两者都会发出 Web 请求,因此请务必创建一个处理通用 Web 请求的类或实现类似 Alamofire 的框架。 (其中有一些很好的身份验证示例和基于 token 的 automatic retrying of requests 等)现在,在您的 ViewController 中,将所有数据处理分离到独立于 View 的 LoginClass,如下所示:
@IBAction func LoginButton(_ sender: UIButton) {
guard let username = usernameField.text else { print("no username") ; return }
guard let password = passwordField.text else { print("no password") ; return }
self.loginModel.login(username: username, password: password) { [weak self] success in
if success {
let dataModel = dataModel(credentials: credentialStyle)
dataModel.loadItems { items : [Item]? in
// Dispatch items to main queue
}
}
}
}
现在,在您的 loginModel
中,您可以处理登录,并在一个完全独立的模型中处理 dataModel,您可以使用从 loginModel 收到的凭据实例化该 dataModel。当然,这是一个粗略的示例,使用 Alamofire 时,您可以使用 Session Manager
来处理身份验证(请参阅“自动重试请求”的 URL,向下滚动一点,然后有一个身份验证的示例。)消除了使用凭据实例化 dataModel 的需要,这纯粹是为了演示如何拆分代码来处理这些请求。
关于与多个 Web 服务快速通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41918929/