現在、Webサービスを使用して、ログインして情報のリストを受け取る必要があります。正常にWebサービスをログインした後複数のWebサービスとのスウィフト
「応答」情報を返します。情報のリストを取得するには、別のWebサービスに送信する必要がある{"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}}
。
現在、ログインボタンを使用してwebserviceにログインしてログインできます。
最初の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()
}
一般的な答えは、レスポンスのデータを受け取り、別のURLSessionを呼び出すURLSessionの補完ハンドラにコードを配置することです。あなたにこれをさせてくれるものがありますか? – dylanthelion