I can't send data to the next UIViewController - ios

I need to send this data(id and status) to the next UIViewController
{"success":true,"id":37,"status":"WAITING"}
but happens this in output....
id: nil // Nextview
status try: nil // nextView
ID NEXT VIEW 37 // the present view
status NEXT VIEW WAITING // the present view
id: Optional(37) // the next view
status try: Optional("WAITING") // the next view
This is my method to get a json and parse, and after, with method, send to the next view controller.
func sendOrder(jsonOrder: AnyObject){
let request = NSMutableURLRequest(URL: NSURL(string: "http://192.168.0.21:8080/easy-coffee/get-order/")!)
request.HTTPMethod = "POST"
let postString = jsonOrder
print(postString)
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error in
guard error == nil && data != nil else {
print("error=\(error)")
return
}
if let httpStatus = response as? NSHTTPURLResponse where httpStatus.statusCode != 200 { print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(response)")
}
let responseString = NSString(data: data!, encoding: NSUTF8StringEncoding)!
print(responseString)
do{
let OrderDict = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
self.orderId = OrderDict["id"] as? Int
self.orderStatus = OrderDict["status"] as? String
self.nextView(self.orderId, orderStatus: self.orderStatus)
}catch{
}
}
task.resume()
}
Method to send data to the next UIView Controller
func nextView(orderId: Int, orderStatus: String){
if let waitingController = storyboard!.instantiateViewControllerWithIdentifier("waitingOrder") as? WaitingOrderViewController {
print("ID NEXT VIEW \(orderId)")
print("status NEXT VIEW \(orderStatus)")
waitingController.id = orderId
waitingController.status = orderStatus
self.presentViewController(waitingController, animated: true, completion: nil)
}
Thank you guys

send the data as NSDictionary, in the next view controller create a dictionary bar and in the segue properties set this.

Related

How to make dataTaskWithRequest to be chronological in swift(Xcode 9, swift 4)?

I have this question for Xcode9 Swift 4. I am trying to fetch some data from some api, and I need to use these data to display. However, since the urlSession is highly asynchronous, I cannot get the data at the right time (most of the time the data is nil). Here is the code.
func getUserInfo(){
let data = user!.Data as? [String : Any] ?? nil
if let data = data{
let ID = data["ID"] as? Int ?? nil
if let ID = ID{
let jsonUrlString = "SomeString"
let requestUrl = URL(string: jsonUrlString)
var request = URLRequest(url: requestUrl!)
request.httpMethod = "GET"
request.setValue("SomeKey", forHTTPHeaderField: "AppKey")
request.setValue(md5("Someinfo"), forHTTPHeaderField: "Sign")
dataTask = URLSession.shared.dataTask(with: request){(data, response, err) in
guard let data = data, err == nil else { // check for fundamental networking err
print("error=\(String(describing: err))")
return
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
// check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
do{
guard let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [Any] else{return}
var userInfo = userDetail(json: json)
let dataDic = userInfo.dataArray as? [String:Any] ?? nil
userInfo.ID = dataDic?["ID"] as? Int
userInfo.AccountName = dataDic?["AccountName"] as? String
userInfo.Avatar = dataDic?["Avatar"] as? String
} catch let jsonErr{
print(jsonErr)
}
}
dataTask?.resume()
}
}
}
I am storing the data into variable userInfo, which has properties like ID, Account Name, and Avatar. But when I call the function in another method "configNavigationBar", it cannot initialize userInfo for me.
func configNavigationBar(){
getUserInfo()
if dataTask?.state == .completed{
navigationItem.title = userInfo?.AccountName
navigationItem.setHidesBackButton(true, animated: true)
navigationController?.navigationBar.prefersLargeTitles = true
}
}
Can anybody help me with the question! I deeply appreciate any help.
How about changing title after successful http request? In a callback. You can configure everything except title before receiving data.
func configNavigationBar(){
getUserInfo { accountName in
self.navigationItem.title = accountName
}
navigationItem.setHidesBackButton(true, animated: true)
navigationController?.navigationBar.prefersLargeTitles = true
}
func getUserInfo(_ callback: #escaping (String) -> Void) {
...
var userInfo = userDetail(json: json)
let dataDic = userInfo.dataArray as? [String:Any] ?? nil
userInfo.ID = dataDic?["ID"] as? Int
userInfo.AccountName = dataDic?["AccountName"] as? String
userInfo.Avatar = dataDic?["Avatar"] as? String
// here's the insertion
callback(userInfo.AccountName)
// end of insertion
....
}
You might also need to wrap ui update into main thread if http request is using background thread.
getUserInfo { accountName in
DispatchQueue.main.async {
self.navigationItem.title = accountName
}
}
You simply need to wait until your data has been downloaded.
While download is busy you should decide to show temporary state on your navigation bar.
Then, when download is finished (or fails), you update the navigation bar again.

Using POST request in HTTP method holds the UI?

When I tap on button it will perform a service request operation.Based on the result it will redirect to next view controller.
After loading Next view controller holds or block the UI. How to solve this issue ? I am using RestAPI and GCD first time in swift, so don't know how to solve this.....
This is login button
#IBAction func btnLogin(_ sender: Any)
{
self.api()
}
This is the function what we call.
func api()
{
let myURL = URL(string: "http://www.digi.com/laravel_api_demo/api/demoapipost")
let request = NSMutableURLRequest(url: myURL!)
request.httpMethod = "POST"
let strEmail = tfLgnID.text
let strPwd = tfPwd.text
let postString = ["username":strEmail, "password":strPwd]
//let postString = ["username":"ayush", "password":"abc"]
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.addValue("application/json", forHTTPHeaderField: "Accept")
//create the session object
//let session = URLSession.shared
do {
request.httpBody = try JSONSerialization.data(withJSONObject: postString, options: .prettyPrinted) // pass dictionary to nsdata object and set it as request body
print("Successfully passed data to server")
} catch let error {
print(error.localizedDescription)
}
let postTask = URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
guard error == nil else {
return
}
guard let data = data else {
return
}
do {
//create json object from data
if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String: Any] {
print("POST Method :\(json)")
let dict = json as? [String: Any]
let num = dict!["status"]
print("Status : \(num)")
print("Dict : \(dict)")
print("username : \(dict!["username"])")
print("password : \(dict!["password"])")
if dict!["status"] as! Int == 1
{
print("Successfully Logged In")
DispatchQueue.main.async {
let visitorVC = self.storyboard?.instantiateViewController(withIdentifier: "VisitorVC") as! VisitorVC
self.present(visitorVC, animated: true, completion: nil)
}
print("OK")
}
else
{
print("Not OK")
}
// handle json...
}
} catch let error {
print(error.localizedDescription)
}
}
postTask.resume()
}
Try this method
func api() {
var request = URLRequest(url: URL(string: "")!) // put your url
request.httpMethod = "POST"
let strEmail = tfLgnID.text
let strPwd = tfPwd.text
let postString:String = "user_id=\(strEmail)&user_id=\(strPwd)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data, error == nil else { // check for fundamental networking error
print("error=\(String(describing: error))")
return
}
do {
if let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary {
print(jsonResult)
let status = jsonResult["status"]! as! NSString
print("status\(status)")
DispatchQueue.main.async(execute: {
// your error Alert
})
}
else {
DispatchQueue.main.async(execute: {
let visitorVC = self.storyboard?.instantiateViewController(withIdentifier: "VisitorVC") as! VisitorVC
self.present(visitorVC, animated: true, completion: nil)
})
}
}
} catch let error as NSError {
print(error.localizedDescription)
}
if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { // check for http errors
print("statusCode should be 200, but is \(httpStatus.statusCode)")
print("response = \(String(describing: response))")
}
let responseString = String(data: data, encoding: .utf8)
print("responseString = \(String(describing: responseString))")
}
task.resume()
}

Swift - View loads before http request is finished in viewDidLoad()

I am trying to load a values from a database and put them into a UITableView in the viewDidLoad function in one of my Swift files. When debugging, at the time of the view rendering, the list of values is empty, but after the view loads, the list gets populated by the view loads. I don't have much experience with threads in Swift, so I am not exactly sure why this is happening, any ideas? I have tried to run DispatchQueue.main.async, but that did not work My code is below:
override func viewDidLoad() {
super.viewDidLoad()
// Load any saved meals, otherwise load sample data.
loadDbMeals()
}
private func loadDbMeals() {
var dbMeals = [Meal]()
let requestURL = NSURL(string: self.URL_GET)
let request = NSMutableURLRequest(url: requestURL! as URL)
request.httpMethod = "GET"
//creating a task to send the post request
let task = URLSession.shared.dataTask(with: request as URLRequest){
data, response, error in
if error != nil{
print("error is \(String(describing: error))")
return;
}
//parsing the response
do {
//converting response to NSDictionary
let myJSON = try JSONSerialization.jsonObject(with: data!, options: [.mutableContainers]) as? NSDictionary
//parsing the json
if let parseJSON = myJSON {
if let nestedDictionary = parseJSON["message"] as? NSArray {
for meal in nestedDictionary {
if let nestedMeal = meal as? NSDictionary {
let mealName = nestedMeal["name"]
let rating = nestedMeal["rating"]
dbMeals.append(Meal(name: mealName as! String, photo: UIImage(named: "defaultPhoto"), rating: rating as! Int, ingredientList: [])!)
}
}
}
}
} catch {
print(error)
}
}
meals += dbMeals
//executing the task
task.resume()
}
So, the current order of breakpoints, is the call to loadDbMeals() in the viewDidLoad() function, then it tries to add the dbMeals variables to the global meals variable, and then the http request gets executed, after the empty list has already been added. I appreciate any help!
Reload your table after loading data
if let parseJSON = myJSON {
if let nestedDictionary = parseJSON["message"] as? NSArray {
for meal in nestedDictionary {
if let nestedMeal = meal as? NSDictionary {
let mealName = nestedMeal["name"]
let rating = nestedMeal["rating"]
dbMeals.append(Meal(name: mealName as! String, photo: UIImage(named: "defaultPhoto"), rating: rating as! Int, ingredientList: [])!)
}
}
DispatchQueue.main.async{
self.tableView.reloadData()
}
}
}
the request happens asynchronously. so the view is loaded while the request may still be in progress.
move the meals += dbMeals line into the request's completion handler (after the for loop), add a self. to the meals var since you are referencing it from within a closure and reload the tableview from the main thread afterwards:
DispatchQueue.main.async{
self.tableView.reloadData()
}
Because dataTask is not a synchronised call, we need to use lock to wait until all fetch is finished.
Code will look something like this:
override func viewDidLoad() {
super.viewDidLoad()
DispatchQueue.global().async {
let lock = DispatchSemaphore(value: 0)
// Load any saved meals, otherwise load sample data.
self.loadDbMeals(completion: {
lock.signal()
})
lock.wait()
// finished fetching data
}
}
private func loadDbMeals(completion: (() -> Void)?) {
var dbMeals = [Meal]()
let requestURL = NSURL(string: self.URL_GET)
let request = NSMutableURLRequest(url: requestURL! as URL)
request.httpMethod = "GET"
//creating a task to send the post request
let task = URLSession.shared.dataTask(with: request as URLRequest){
data, response, error in
if error != nil{
print("error is \(String(describing: error))")
return;
}
//parsing the response
do {
//converting response to NSDictionary
let myJSON = try JSONSerialization.jsonObject(with: data!, options: [.mutableContainers]) as? NSDictionary
//parsing the json
if let parseJSON = myJSON {
if let nestedDictionary = parseJSON["message"] as? NSArray {
for meal in nestedDictionary {
if let nestedMeal = meal as? NSDictionary {
let mealName = nestedMeal["name"]
let rating = nestedMeal["rating"]
dbMeals.append(Meal(name: mealName as! String, photo: UIImage(named: "defaultPhoto"), rating: rating as! Int, ingredientList: [])!)
}
}
}
}
} catch {
print(error)
}
// call completion
completion()
}
meals += dbMeals
//executing the task
task.resume()
}
So execute loadDbMeals with completion block which will be called when fetching is finished and lock will wait until completion block is called.

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();
}
}

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