PushViewController doesn't work after httpPost - ios

I have made a login screen which takes the input and communicates with the REST api to verify the user. If the response is true, I login the user else not.
I have written a method openViewControllerBasedOnIdentifier(id) to switch views.
The REST api returns true and false appropriately. The push controller gets called but view does not change. How if I place only one line in LoginAction method 'self.openViewControllerBasedOnIdentifier("PlayVC")' and remove the rest of the code , it works fine.
Here is my code
#IBAction func LoginAction(_ sender: Any) {
//self.openViewControllerBasedOnIdentifier("PlayVC")
Constants.login_status = false
//created NSURL
let requestURL = NSURL(string: URL_BK)
//creating NSMutableURLRequest
let request = NSMutableURLRequest(url: requestURL! as URL)
//setting the method to post
request.httpMethod = "POST"
let username = phonenumber.text
//creating the post parameter by concatenating the keys and values from text field
let postParameters = "username="+username!+"&password=bk&schoolId=0";
//adding the parameters to request body
request.httpBody = postParameters.data(using: String.Encoding.utf8)
//creating a task to send the post request
let task = URLSession.shared.dataTask(with: request as URLRequest){
data, response, error in
let responseData = String(data: data!, encoding: String.Encoding.utf8)
if error != nil{
print("error is \(error)")
return;
}
//parsing the response
do {
print(“Received data is ---%#",responseData as Any)
let myJSON = try JSONSerialization.jsonObject(with: data! , options: .allowFragments) as? NSDictionary
if let parseJSON = myJSON {
var status : Bool!
status = parseJSON["status"] as! Bool?
//print(status)
if status==false
{
Constants.login_status = false
}
else{
Constants.login_status = true
print("calling PLAYVC")
self.openViewControllerBasedOnIdentifier("PlayVC")
}
}
else{
print("NULL VALUE RECEIVED")
}
} catch {
print(error)
}
}
//executing the task
task.resume()
}

You should open the new view controller on the main thread like this:
DispatchQueue.main.async {
self.openViewControllerBasedOnIdentifier("PlayVC")
}
Your REST API query response is processed in a background thread when you call URLSession.shared.dataTask and so when you call any UI actions, you should wrap the code as above to execute the UI code in the main thread. Then it would work fine :)

Related

Synchronous Authentication SwiftUI

I am making an iOS app using SwiftUI that requires login. When the create account button is pressed, the action triggers a function in my NetworkManager class that sends the inputed email and password as a post request and receives the appropriate data back for authentication. It then uses the received data to determine whether the credentials are valid
My issue is that it runs the code that verifies the inputed credentials against the API response before the response is actually received. Consequently, the result is the same each time.
Button(action: {
self.networkManager.signUp(email: self.inputEmail, password: self.inputPassword)
// These if statements run before the above line is executed
if self.networkManager.signUpResponse.code == nil {
// SUCCESSFUL REGISTRATION
ProgressHUD.showSuccess("Account Successfully Created!")
UserDefaults.standard.set(true, forKey: "LoggedIn")
self.showingWelcomePage = false
}
if self.networkManager.signUpResponse.code == 201 {
// REGISTRATION FAILED
ProgressHUD.showError("This account already exists", interaction: false)
}
}) {
Text("Create Account")
.font(.headline)
}
I have tried using DispatchQueue.main.async() or creating my own thread, however nothing seems to work. I need to find a way to pause the main thread in order to wait for this line of code to execute before proceeding without using DispatchQueue.main.sync() as this results in deadlock and program crash.
Here is the code for the function that makes the post request to the API
class NetworkManager: ObservableObject {
#Published var signUpResponse = AccountResults()
func signUp(email: String, password: String) {
if let url = URL(string: SignUpAPI) {
let session = URLSession.shared
let bodyData = ["school": "1",
"email": email,
"password": password]
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = try! JSONSerialization.data(withJSONObject: bodyData)
let task = session.dataTask(with: request) { (data, response, error) in
if error == nil {
let decoder = JSONDecoder()
if let safeData = data {
do {
let results = try decoder.decode(AccountResults.self, from: safeData)
DispatchQueue.main.async {
self.signUpResponse = results
}
} catch {
print(error)
}
}
}
}
task.resume()
}
}
}
Thanks in advance!
Try it:
func signUp(email: String, password: String, completion: #escaping((Error?, YourResponse?) -> Void)) {
if let url = URL(string: SignUpAPI) {
let session = URLSession.shared
let bodyData = ["school": "1",
"email": email,
"password": password]
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = try! JSONSerialization.data(withJSONObject: bodyData)
let task = session.dataTask(with: request) { (data, response, error) in
if error == nil {
let decoder = JSONDecoder()
if let safeData = data {
do {
let results = try decoder.decode(AccountResults.self, from: safeData)
DispatchQueue.main.async {
self.signUpResponse = results
completion(nil, results)
}
} catch {
print(error)
completion(error, nil)
}
}
}
}
task.resume()
}
}
}
use escaping in your function, I think will get exactly the point server response data or get errors too.
my english is so bad.

How to cancel a URL session request

I am upload multiple image to server using convert image to base64 and send image in a API as a parameter. But when we call api again and again then how to stop api calling on button click. I am using below code to call API.
Thanks in advance
let urlPath: String = "URL"
let url: URL = URL(string: urlPath)!
var request1 = URLRequest(url: url)
request1.httpMethod = "POST"
let stringPost="imgSrc=\(image)"
let data = stringPost.data(using: String.Encoding.utf8)
// print("data\(data)")
request1.httpBody=data
request1.timeoutInterval = 60
let _:OperationQueue = OperationQueue()
let task = session.dataTask(with: request1){data, response, err in
do
{
if data != nil
{
print("data\(String(describing: data))")
if let jsonResult = try JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary
{
DispatchQueue.main.async
{
print("json\(jsonResult)")
}
}
}
catch let error as NSError
{
DispatchQueue.main.async
{
print("error is \(error)")
print("error desc \(error.localizedDescription)")
}
}}
task.resume()
Make the object task as a global variable, then you can cancel it anywhere by:
task.cancel()
Alternatively, if the object session is a URLSession instance, you can cancel it by:
session.invalidateAndCancel()
If you don't want to allow API call again if there is any previous download is on progress, you can do as follows,
Make your task(URLSessionDataTask type) variable as global variable in the class as follows,
let task = URLSessionDataTask()
Then on your button action do as below by checking the task download status,
func uploadButtonPressed() {
if task.state != .running {
// Make your API call here
} else {
// Dont perform API call
}
}
You can make use following states like running which is provide by URLSessionDataTask class and do action accordingly as per your need,
public enum State : Int {
case running
case suspended
case canceling
case completed
}
You can check result of your task. And if everything is alright you can
task.resume()
but if not
task.cancel()

Table only shows when interacting and not by default

I have a table in a view controller that is populated through a dictionary from which information is retrieved via a JSON request. In the viewDidLoad() function, I call the function that retrieves the data which is added to `IncompletedDeadlines dictionary:
override func viewDidLoad() {
super.viewDidLoad()
self.IncompleteDeadlines = [String:AnyObject]()
self.retrieveIncompletedDeadlines()
}
Everything works however the table only shows when interacted with. I thought maybe the best way to show the table the moment the view appears is by adding a tableView.reload to viewDidAppear as so:
override func viewDidAppear(_ animated: Bool) {
self.tableView.reloadData()
}
But this doesn't fix it. I have attached pictures for clarity of the situation. Picture one shows the view the moment the view appears. Picture 2 only happens once the table is interacted with i.e. swiped. So my question is how can I get the table to show immediately? I understand there can be a delay because of the load, but I shouldn't have to interact with it for it to show:
When the view is interacted with i.e. swiped:
The retrieveIncompletedDeadlines() function is as so:
func retrieveIncompletedDeadlines(){
let myUrl = NSURL(string: "https://www.example.com/scripts/retrieveIncompleteDeadlines.php");
let request = NSMutableURLRequest(url:myUrl! as URL)
let user_id = UserDetails[0]
request.httpMethod = "POST";
let postString = "user_id=\(user_id)";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(String(describing: error))")
return
}
var err: NSError?
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
let checker:String = parseJSON["status"] as! String;
if(checker == "Success"){
let resultValue = parseJSON["deadlines"] as! [String:AnyObject]
self.IncompleteDeadlines = resultValue
}
self.tableView.reloadData()
}
} catch let error as NSError {
err = error
print(err!);
}
}
task.resume();
self.tableView.reloadData()
}
JSON will be parsed on the background thread but any update to the UI must be done on the main thread hence you have to do it inside DispatchQueue.main.async {} This article explains well what is the problem.
Furthermore I would write a completions handler which returns the data once the operation has finished. This is another interesting article about.
Completion handlers are super convenient when your app is doing something that might take a little while, like making an API call, and you need to do something when that task is done, like updating the UI to show the data.
var incompleteDeadlines = [String:AnyObject]()
override func viewDidLoad() {
super.viewDidLoad()
//please note your original function has changed
self.retrieveIncompletedDeadlines { (result, success) in
if success {
// once all the data has been parsed you assigned the result to self.incompleteDeadlines
self.incompleteDeadlines = result
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
}
func retrieveIncompletedDeadlines(_ completion:#escaping ([String:AnyObject] , _ success: Bool)-> Void){
let myUrl = NSURL(string: "https://www.example.com/scripts/retrieveIncompleteDeadlines.php");
let request = NSMutableURLRequest(url:myUrl! as URL)
let user_id = UserDetails[0]
request.httpMethod = "POST";
let postString = "user_id=\(user_id)";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request as URLRequest) {
data, response, error in
if error != nil {
print("error=\(String(describing: error))")
return
}
var err: NSError?
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
let checker:String = parseJSON["status"] as! String;
var resultValue = [String:AnyObject]()
if(checker == "Success"){
resultValue = parseJSON["deadlines"] as! [String:AnyObject]
}
completion(resultValue, true)
}
} catch let error as NSError {
err = error
print(err!);
}
}
task.resume();
}
}

Xcode: URL Shared Session not running when unit testing

I have made a simple app which adds data to a database, and then retrieves it. Whilst creating unit testing, it appears that the URLSession.Shared.dataTask is not running. I can see this through the output of the print statements I have setup. Below is my code:
func addChild(childName:String,dob:String,number1:String,number2:String,parentNum:String,parentPass:String,notes:String){
//url to php file
let url = NSURL(string:"http://localhost/addChild.php")
//request to this file
let request = NSMutableURLRequest(url: url as! URL)
//method to pass data to this file
request.httpMethod = "POST"
//body to be appended to url
let body = "childName=\(childName)&dateOfBirth=\(dob)&contact1=\(number1)&contact2=\(number2)&parentAccNum=\(parentNum)&parentAccPass=\(parentPass)&notes=\(notes)"
request.httpBody = body.data(using: String.Encoding.utf8)
print("a")
//launching the request
URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data:Data?, response:URLResponse?, error:Error?) in
print("b")
if (error == nil){
print("c")
//send request
//get main queue in code process to communicate back to user interface
DispatchQueue.main.async(execute: {
do{
//get json result
let json = try JSONSerialization.jsonObject(with: data!,options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
print("d")
//assigning json to parseJSON in guard/secure way
//checking whether the parsing has worked
guard let parseJSON = json else{
print("Error while parsing")
return
}
//get id from parseJSON dictionary
let id = parseJSON["id"]
//if there is some id value
if id != nil{
print(parseJSON)
self.success = true
print("success")
}
}
catch{
print("Caught an error:\(error)")
}
} )
}
//if unable to proceed request
else{
print("Error:\(error)")
}
//launch prepared session
}).resume()
}
And then below is my unit testing script:
import XCTest
#testable import computerScienceCoursework
class addChildTest: XCTestCase {
//Setting up the values of the text fields
var testChildName:String = "Test name"
var testDOB:String = "99/99/99"
var testContact1:String = "00000000000"
var testContact2:String = "11111111111"
var testParAccNum:String = "-1"
var testParAccPass:String = "Password"
var testNotes:String = "Insert notes here"
var newChild = AddChildController()
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}
func testAddChildIsWorking(){
//Assigning the values to the text fields
newChild.addChild(childName: testChildName,dob: testDOB,number1: testContact1,number2: testContact2,parentNum: testParAccNum,parentPass: testParAccPass,notes: testNotes)
XCTAssert(newChild.success == true)
}
}
Problem here is that you don´t know when the async task is finished and the success property is getting updated.
There are some possible solutions for your problem one of them is to add a completion handler to your method.
func addChild(childName:String,dob:String,number1:String,number2:String,parentNum:String,parentPass:String,notes:String, completion: (Bool) -> Void){
//url to php file
let url = NSURL(string:"http://localhost/addChild.php")
//request to this file
let request = NSMutableURLRequest(url: url as! URL)
//method to pass data to this file
request.httpMethod = "POST"
//body to be appended to url
let body = "childName=\(childName)&dateOfBirth=\(dob)&contact1=\(number1)&contact2=\(number2)&parentAccNum=\(parentNum)&parentAccPass=\(parentPass)&notes=\(notes)"
request.httpBody = body.data(using: String.Encoding.utf8)
print("a")
//launching the request
URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data:Data?, response:URLResponse?, error:Error?) in
print("b")
if (error == nil){
print("c")
//send request
//get main queue in code process to communicate back to user interface
DispatchQueue.main.async(execute: {
do{
//get json result
let json = try JSONSerialization.jsonObject(with: data!,options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
print("d")
//assigning json to parseJSON in guard/secure way
//checking whether the parsing has worked
guard let parseJSON = json else{
print("Error while parsing")
completion(false)
return
}
//get id from parseJSON dictionary
let id = parseJSON["id"]
//if there is some id value
if id != nil{
print(parseJSON)
self.success = true
print("success")
completion(true)
}
}
catch{
print("Caught an error:\(error)")
completion(false)
}
} )
}
//if unable to proceed request
else{
print("Error:\(error)")
completion(false)
}
//launch prepared session
}).resume()
}
Then in your test method you can the method.
func testAddChildIsWorking()
{
let asyncExpectation = expectationWithDescription("addChildIsWorkingFunction")
newChild.addChild(childName: testChildName, dob: testDOB, number1: testContact1,
number2: testContact2, parentNum: testParAccNum, parentPass: testParAccPass, notes: testNotes) { (success) in
asyncExpectation.fulfill()
}
self.waitForExpectationsWithTimeout(10) { error in
XCTAssert(newChild.success == true)
}
}
waitForExpectationWithTimeout is waiting until a fulfill is trigger or a timeout occurs. In this way you could test your async code.
For more informations check this link
Hope that helps.

viewDidLoad not getting called for item on UITabViewController

on a button click ,via segue the flow goes to UITabViewController.
self.performSegueWithIdentifier(self.gotoResult, sender: nil)
let myUrl = NSURL(string: "XXXXXXXX");
let request = NSMutableURLRequest(URL:myUrl!);
request.HTTPMethod = "POST";
// Compose a query string
resultVar.city = cityText.text
resultVar.state = streetText.text
let postString = "streetaddr=\(streetText.text)&city=\(cityText.text)&state=\(stateVal)&degree=\(degreeVal)";
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding);
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) {
data, response, error in
if error != nil
{
print("error= \(error)")
return
}
// You can print out response object
print("response = \(response)")
// Print out response body
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)
print("responseString = \(responseString)")
do {
resultVar.myJSON = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary
} catch let error2 as NSError? {
print("error 2 \(error2)")
}
}
task.resume()
There is a UITabViewController with three items. when the initial view gets loaded(item1) the viewDidLoad is not getting called. for now I have added the same code in viewDidAppear and when i click on a different tab and come back to item1 ,the fields are populated. But I want it to work on initial load after the segue only. What am I missing?
ViewdidLoad of Item1
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
print("in here CityViewController")
if let parseJSON = resultVar.myJSON {
// Now we can access value of elements by its key
var weather_condition = parseJSON["weather_condition"] as! String
print("weather_condition: \(weather_condition)")
weatherconditionLbl.text = weather_condition
}
The code which gets called when i switch tabs:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
print("in here CityViewController1")
if let parseJSON = resultVar.myJSON {
// Now we can access value of elements by its key
var weather_condition = parseJSON["weather_condition"] as! String
print("weather_condition: \(weather_condition)")
weatherconditionLbl.text = weather_condition + "in " + resultVar.city+","+resultVar.state
}
}
so the code viewdidload is not getting called and viewDidAppear gets called when i switch tabs.
A network request will be slower than rendering the next scene so "parseJSON" is not there. You need to refresh your graphics using a callback from the network request. In order to do that I suggest that you call the network request from "Item 1" every time you need to refresh its content (that is up to you).

Resources