Data persistence after call to webservice iOS - ios

Sorry for my English.
I have many problems in achieving data persistence after call the webservice .
Everything works correctly but I need to use that data in other methods .
As seen in the image once charge data on the screen then I want to give the user the option to use them well or load new data . But not as saving the data recovery webservice .
I was trying to use NSUserDefault but neither work .
From already thank you very much , any response is helpful .
Regards!
ViewController:
class ViewController: UIViewController {
let defaults = NSUserDefaults.standardUserDefaults()
#IBOutlet weak var myLabel2: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let newVC : SecondViewController = segue.destinationViewController as! SecondViewController
let telefono = myLabel2.text!
newVC.recibirString = telefono
}
#IBAction func mostrarAlgo(sender:AnyObject) {
//something
}
}
SecondViewController:
class SecondViewController: UIViewController {
let defaults = NSUserDefaults.standardUserDefaults()
var tel: String = ""
#IBOutlet weak var myLabel1: UITextField!
#IBOutlet weak var myLabel3: UITextField!
#IBOutlet weak var myLabel4: UITextField!
#IBOutlet weak var myLabel5: UITextField!
#IBOutlet weak var myLabel6: UITextField!
#IBOutlet weak var myLabel7: UITextField!
#IBOutlet weak var myLabel8: UITextField!
var recibirString = String()
override func viewDidLoad() {
super.viewDidLoad()
myLabel1.text = recibirString
performRequestAndUpdateUI2(myLabel1.text!)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func performRequestAndUpdateUI2(tel:String) {
WebService.llamarWebService (tel,completionHandler: { datos in
guard datos.count == 5 else {
self.myLabel8.text = ("El número ingresado no está registrado.")
return
}
guard datos[0] as! String == "OK" else {
print("not everything was OK")
return
}
dispatch_async(dispatch_get_main_queue()) {
self.myLabel3.text = datos[0] as? String
self.myLabel4.text = datos[1] as? String
self.myLabel5.text = datos[2] as? String
self.myLabel6.text = datos[3] as? String
self.myLabel7.text = datos[4] as? String
self.myLabel8.text = ("Usted está registrado.")
//need save this data
}
})
}
#IBAction func guardarDatos(sender: AnyObject) {
// do something
}
}
WebService:
class WebService{
var telefono = ""
class func llamarWebService(telefono:String, completionHandler: (datos:NSArray)->()){
let urlPath = "http://xxxxxxxxxxx.com.ar/xxxxxxxxxx/forwarder_number?phone=\(telefono)"
let url = NSURL(string: urlPath)
let session = NSURLSession.sharedSession()
var arreglo:NSArray!
let task = session.dataTaskWithURL(url!,completionHandler: {data,response,error -> Void in
if (error != nil){
print(error?.localizedDescription)
}
let nsdata: NSData = NSData(data: data!)
arreglo = self.retornarDatos(nsdata)
completionHandler(datos: arreglo)
})
task.resume()
}
class func retornarDatos(nsdata: NSData)-> Array<String>{
let datos = NSString(data:nsdata,encoding: NSUTF8StringEncoding)
let partes = datos?.componentsSeparatedByString(",")
var arreglo : [String] = []
for i in partes!{
arreglo.append(i)
}
return arreglo
}
}

Related

error with type of string in swift 3

A print(user) shows what I need, but the label wont take it. I don't really understand.
Can someone explain what's wrong ?
Here is the printscreen
edited: here is the code :
import UIKit
class MainViewController: UIViewController {
#IBOutlet weak var etqRetards: UILabel!
#IBOutlet weak var etqBiens: UILabel!
#IBOutlet weak var etqSortie: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//on récupère l'utilisateur courant.
let fm = FileManager.default
let urlRepDocuments = try! fm.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let url = urlRepDocuments.appendingPathComponent("user.plist")
if let dico = NSMutableDictionary.init(contentsOfFile: url.path){
if dico["email"] != nil {
let user = dico["email"] as! String
}
}
etqBiens.text = user
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func btnLocataires(_ sender: Any) {
}
#IBAction func btnBatiments(_ sender: Any) {
}
#IBAction func btnOutils(_ sender: Any) {
}
}
Here is the part where your code don't work anymore:
1: if let dico = NSMutableDictionary.init(contentsOfFile: url.path){
2: if dico["email"] != nil {
3: let user = dico["email"] as! String
4: }
5: }
5: etqBiens.text = user
userwill only be accessible on row 3, since it´s declared within that if-statement. What you need to do is to declare user globally so that you can access it and also declare it as a var to be able to change the value.
Do it like this instead:
import UIKit
class MainViewController: UIViewController {
#IBOutlet weak var etqRetards: UILabel!
#IBOutlet weak var etqBiens: UILabel!
#IBOutlet weak var etqSortie: UILabel!
var user = "" // declare user here
override func viewDidLoad() {
super.viewDidLoad()
//on récupère l'utilisateur courant.
let fm = FileManager.default
let urlRepDocuments = try! fm.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let url = urlRepDocuments.appendingPathComponent("user.plist")
if let dico = NSMutableDictionary.init(contentsOfFile: url.path){
if dico["email"] != nil {
user = dico["email"] as! String
}
}
etqBiens.text = user
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func btnLocataires(_ sender: Any) {
}
#IBAction func btnBatiments(_ sender: Any) {
}
#IBAction func btnOutils(_ sender: Any) {
}
}
I mean, to start off, user is not defined in the scope of the statement etqBiens.text...
If you want to copy the description of the user as a string, you can do so by saying user.description but I don't know how exactly that might be useful in the long run ¯\_(ツ)_/¯

How can I passing data UIViewContoller from UIView in swift3

How can i passing data uiviewController from uiview
I am Using function but it was not working
protocol name is startcalldelegate and function name is startcall
UIView Code
protocol StartCallDelegate: class {
func startCall(localNickname :String, remoteNickname :String)}
class CardView: UIView {
let managedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
weak var delegate: CardViewDelegate?
weak var socketdelegate: StartCallDelegate?
#IBOutlet weak var UserPhoto: UIImageView!
#IBOutlet weak var UserNickName: UILabel!
#IBOutlet weak var UserAge: UILabel!
#IBOutlet weak var UserPeople: UILabel!
var localNickname: String = ""
var remoteNickname: String = ""
#IBAction func SendMessage(_ sender: Any) {
print("SendMessage")
//print(localNickName)
//print(UserNickName.text!)
}
#IBAction func SendVideoCall(_ sender: Any) {
print("SendVideoCall")
let entityDescription = NSEntityDescription.entity(forEntityName: "Profile", in: managedObjectContext)
let request = NSFetchRequest<NSFetchRequestResult>()
request.entity = entityDescription
do {
let objects = try managedObjectContext.fetch(request)
if objects.count > 0 {
let match = objects[0] as! NSManagedObject
localNickname = match.value(forKey: "nick") as! String
} else {
print("Nothing founded")
}
} catch {
print("error")
}
remoteNickname = UserNickName.text!
socketdelegate?.startCall(localNickname: localNickname, remoteNickname: remoteNickname)
delegate?.VideoChatSegue()
}
}
UIViewcontroller Code
class ViewController: UIViewcontroller, StartCallDelegate {
var localNickname: String = ""
var remoteNickname: String = ""
override func viewDidLoad() {
super.viewDidLoad()
print(localNickname)
print(remoteNickname)
}
func startCall(localNickname: String, remoteNickname: String) {
print("Action startcall func")
self.localNickname = localNickname
self.remoteNickname = remoteNickname
}
startCall func not working
You need to define delegate in viewcontroller' ViewDidLoad
let objOardView = CardView() // this is only test purpose
objOardView.socketdelegate = self

Firebase Database not Uploading

I have my upload code here
import UIKit
import Firebase
class ChatViewController: UIViewController {
let chatRef = FIRDatabase.database().reference().child("chat")
let userUid = FIRAuth.auth()?.currentUser?.uid
var userName = ""
#IBOutlet weak var topBar: UINavigationItem!
#IBOutlet weak var containerView: UIView!
#IBOutlet var inputTextField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
topBar.title = "Chat Log Controller"
FIRDatabase.database().reference().child("users/\(userUid!)/name").observe(.value) { (snap: FIRDataSnapshot) in
self.userName = (snap.value! as! String).description
}
}
#IBAction func handleSend(_ sender: AnyObject) {
let childChatRef = chatRef.childByAutoId()
let message = inputTextField.text!
childChatRef.child("text").setValue(message)
print(inputTextField.text)
}
#IBAction func handleSendByEnter(_ sender: AnyObject) {
let childChatRef = chatRef.childByAutoId()
let message = inputTextField.text!
print(userName)
childChatRef.child("name").setValue(userName)
childChatRef.child("text").setValue(message)
print(inputTextField.text)
}
}
text is successfully uploaded But
It doesn't print userName and doesn't upload it to Firebase Database
But username is nut nil!
Try to use your observer code as,
ref.observeSingleEventOfType(.Value, withBlock: { snapshot in
}
Just take self.username = snap.value! as! String
It will solve your problem.

OBD2Kit Swift example can't typecast, keep getting nil

Since there was no example code for using OBD2Kit and Swift I forked it into https://github.com/YannisDC/OBD2Kit and used it as a pod.
I translated some OBJ-C example code but can't seem to downcast the FLWiFiScanTool into the ELM327 type. Why do I keep getting nil?
import UIKit
import OBD2Kit
class ViewController: UIViewController, FLScanToolDelegate {
#IBOutlet weak var hostIpAddress: UITextField!
var scanTool: ELM327!
#IBOutlet weak var statusLabel: UILabel!
#IBOutlet weak var scanToolLabel: UILabel!
#IBOutlet weak var rpmLabel: UILabel!
#IBOutlet weak var speedLabel: UILabel!
#IBOutlet weak var tempLabel: UILabel!
var scanning = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.hostIpAddress.text = "192.168.0.10"
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func scanButton(sender: UIButton) {
if !scanning {
startScan()
} else {
stopScan()
}
}
func startScan() {
if let scanTool = ELM327(host: self.hostIpAddress.text!, andPort: 35000) {
self.statusLabel.text = "Initializing..."
scanTool.useLocation = true
scanTool.delegate = self
scanTool.startScanWithSensors({() -> [AnyObject] in
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.statusLabel.text = "Scanning..."
self.scanToolLabel.text = scanTool.scanToolName
})
// let sensors: [AnyObject] = [ OBD2Sensor.SensorEngineRPM as! AnyObject,
// OBD2Sensor.SensorVehicleSpeed as! AnyObject,
// OBD2Sensor.SensorOxygenSensorsPresent as! AnyObject ]
let sensors: [AnyObject] = [ 0x0C,
0x0D,
0x13 ]
return sensors
})
self.scanTool = scanTool
print("So far succesfull")
scanning = !scanning
} else {
self.statusLabel.text = "Not working"
}
}
func stopScan() {
statusLabel.text = "Stopped"
let scanTool: ELM327 = self.scanTool
scanTool.cancelScan()
scanTool.sensorScanTargets = nil
scanTool.delegate = nil
scanning = !scanning
}
// MARK: - FLScanToolDelegate
func scanTool(scanTool: FLScanTool, sensor: FLECUSensor) {
var sensorLabel: UILabel? = nil
switch sensor.pid {
case OBD2Sensor.SensorEngineRPM:
sensorLabel = self.rpmLabel
case OBD2Sensor.SensorVehicleSpeed:
sensorLabel = self.speedLabel
default:
sensorLabel = self.tempLabel
}
self.showSensorValue(sensor, onLabel: sensorLabel!)
}
func showSensorValue(sensor: FLECUSensor, onLabel label: UILabel) {
let sensorValue: String = "\(sensor.valueStringForMeasurement1(false)) \(sensor.imperialUnitString)"
dispatch_async(dispatch_get_main_queue(), {() -> Void in
label.text = sensorValue
})
}
}
Edit 1:
I can already scan for the tool now since ELM327 is a FLWiFiScanTool and not the other way around. I can get the toolname so it's connecting but can't seem to get the sensors output.
I managed to figure it out. The expected sensors array should be one of NSNumbers so I casted the sensors UInt's to NSNumbers.
And I also missed the didUpdateSensor part in the scanTool function.
Make sure to use the metric system, this is somehow way more reliable in my case. (My car is using the metric system as well, maybe that's why.)
import UIKit
import OBD2Kit
class ViewController: UIViewController, FLScanToolDelegate {
var scanTool: ELM327!
#IBOutlet weak var statusLabel: UILabel!
#IBOutlet weak var scanToolLabel: UILabel!
#IBOutlet weak var rpmLabel: UILabel!
#IBOutlet weak var speedLabel: UILabel!
#IBOutlet weak var tempLabel: UILabel!
var scanning = false
override func viewDidLoad() {
super.viewDidLoad()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
#IBAction func scanButton(sender: UIButton) {
if !scanning {
startScan()
} else {
stopScan()
}
}
func startScan() {
if let scanTool = ELM327(host: "192.168.0.10", andPort: 35000) {
self.statusLabel.text = "Initializing..."
scanTool.useLocation = true
scanTool.delegate = self
scanTool.startScanWithSensors({() -> [AnyObject] in
dispatch_async(dispatch_get_main_queue(), {() -> Void in
self.statusLabel.text = "Scanning..."
self.scanToolLabel.text = scanTool.scanToolName
})
let sensors: [AnyObject] = [ OBD2Sensor.SensorEngineRPM as NSNumber, OBD2Sensor.SensorVehicleSpeed as NSNumber, OBD2Sensor.SensorOxygenSensorsPresent as NSNumber ]
return sensors
})
self.scanTool = scanTool
print("So far succesfull")
scanning = !scanning
} else {
self.statusLabel.text = "Not working"
}
}
func stopScan() {
statusLabel.text = "Stopped"
let scanTool: ELM327 = self.scanTool
scanTool.cancelScan()
scanTool.sensorScanTargets = nil
scanTool.delegate = nil
scanning = !scanning
}
// MARK: - FLScanToolDelegate
func scanTool(scanTool: FLScanTool!, didUpdateSensor sensor: FLECUSensor!) {
var sensorLabel: UILabel? = nil
switch sensor.pid {
case OBD2Sensor.SensorEngineRPM:
sensorLabel = self.rpmLabel
case OBD2Sensor.SensorVehicleSpeed:
sensorLabel = self.speedLabel
default:
sensorLabel = self.tempLabel
}
self.showSensorValue(sensor, onLabel: sensorLabel!)
}
func showSensorValue(sensor: FLECUSensor, onLabel label: UILabel) {
let sensorValue: String = "\(sensor.valueStringForMeasurement1(true)) \(sensor.metricUnitString)"
dispatch_async(dispatch_get_main_queue(), {() -> Void in
label.text = sensorValue
})
}
}

.Type does not have a member named "x" when trying to get variable value in Struct(Swift)

I am currently working on an app and I am having an issue. When the user login the webservice, if the login is successful the server responds with JSON, where we use the "firstName" and "SecondName" to then create our "User" which is a struct defined in another file called User.swift . Then, what I want to do is user the "firstName" that has been given to the "User struct" as a UILabel in my homepageview that comes after a successful login. when I try to give my label User.prenom(which is firstName in french) I get the error: User.type does not have a member called...
Here is my code:
the client file where the Login Method is defined:
import Foundation
import Alamofire
import SwiftyJSON
private let _instance = Client()
class Client {
// Router is used to do a request to the server.
private enum Router: URLRequestConvertible {
private static let baseURL = "https://mobile.uqam.ca/portail_etudiant/"
// stores the authentication token.
static var code_perm: String?
static var nip:String?
// Login request.
case Login(String, String)
// URLRequestConvertible protocol.
var URLRequest: NSURLRequest {
// Returns the path, http method and parameters for the request.
var (path: String, method: Alamofire.Method, parameters: [String: AnyObject]) = {
switch self {
case .Login (let code_perm, let nip):
let params: [String: AnyObject] = [
"code_perm": code_perm,
"nip": nip,
]
return ("proxy_dossier_etud.php", .POST, params)
}
}()
// Setup the URLRequest.
let url = NSURL(string: Router.baseURL)
let urlRequest = NSMutableURLRequest(URL: url!.URLByAppendingPathComponent(path))
urlRequest.HTTPMethod = method.rawValue
if let code_perm = Router.code_perm {
if let nip = Router.nip{
parameters["nip"] = nip
parameters["code_perm"] = code_perm
}
}
let encoding = Alamofire.ParameterEncoding.URL
return encoding.encode(urlRequest, parameters: parameters).0
}
}
// Singleton
class var sharedInstance: Client {
return _instance
}
private init() {}
// Login logs in the user with his email and password.
func login(code_perm:String, nip:String, callback:(LoginResponse?) -> Void) {
Alamofire.request(Router.Login(code_perm, nip)).responseJSON { (_, _, data, error) in
if(error != nil) {
callback(nil)
return
}
var json = JSON(data!)
let prenom = json["socio"]["prenom"].stringValue
let nom = json["socio"]["nom"].stringValue
Router.code_perm = code_perm
Router.nip = nip
callback(LoginResponse(
user: User(prenom: prenom,nom: nom)
))
}
}
}
the loginViewController where the login function is called
import UIKit
class LoginViewController: UIViewController {
#IBOutlet weak var LoginScreenImage: UIImageView!
#IBOutlet weak var codeTextField: UITextField!
#IBOutlet weak var nipTextField: UITextField!
#IBOutlet weak var loadingLogin: UIActivityIndicatorView!
let client = Client.sharedInstance
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
LoginScreenImage.image = UIImage(named: "UQAMLOGO")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func connect() {
let code_perm = codeTextField.text
let nip = nipTextField.text
self.loadingLogin.startAnimating()
if code_perm != "" && nip != "" {
client.login(code_perm, nip: nip, callback: { (response) in
if let response = response {
self.loadingLogin.stopAnimating()
let homeViewController = self.storyboard!.instantiateViewControllerWithIdentifier("HomeViewController") as HomeViewController
self.showViewController(homeViewController, sender: self)
} else {
self.loadingLogin.stopAnimating()
let badLogin = UIAlertController(title: "Échec de connexion", message: "La combinaison du code permanent et du nip n'est pas bonne", preferredStyle: .Alert)
let reessayer = UIAlertAction(title: "Réessayer", style: .Default, handler: { (reessayer) -> Void in
self.dismissViewControllerAnimated(true , completion: nil)
})
badLogin.addAction(reessayer)
self.presentViewController(badLogin, animated: true, completion: nil)
}
})
}
}
}
the User.swift while where the user struct is
import Foundation
struct User {
var prenom :String
var nom: String
}
struct LoginResponse {
var user: User
}
and finally the HomePageViewController where I try to give the value to my label:
import UIKit
class HomeViewController: UIViewController {
#IBOutlet weak var schedule: UIImageView!
#IBOutlet weak var courses: UIImageView!
#IBOutlet weak var email: UIImageView!
#IBOutlet weak var grades: UIImageView!
#IBOutlet weak var bienvenueLabel: UILabel!
let client = Client.sharedInstance
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
schedule.image = UIImage(named:"schedule")
courses.image = UIImage(named: "courses")
email.image = UIImage(named:"mail")
grades.image = UIImage(named:"grades")
bienvenueLabel.text = User.prenom
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Thanks everyone for the help and have a great day/night
Charles
You are accessing the class instead of an instance. Instead, you should pass the response instance to your HomeViewController:
class HomeViewController : .. {
// ...
var loginResponse : LoginResponse
// ...
override func viewDidLoad() {
// ...
bienvenueLabel.text = loginResponse.user.prenom
}
}
// ...
client.login(code_perm, nip: nip, callback: { (response) in
if let loginResponse = response as LoginResponse {
self.loadingLogin.stopAnimating()
let homeViewController = self.storyboard!.instantiateViewControllerWithIdentifier("HomeViewController") as HomeViewController
homeViewController.loginResponse = loginResponse
// assign your instance ^^^^^^^^^^^^^^^^^^^^^^^^
self.showViewController(homeViewController, sender: self)
}
You are accessing the class instead of an instance. Instead, you should pass the response instance to your HomeViewController:
class HomeViewController : .. {
// ...
var loginResponse : LoginResponse
// ...
override func viewDidLoad() {
// ...
bienvenueLabel.text = loginResponse.user.prenom
}
}
// ...
client.login(code_perm, nip: nip, callback: { (response) in
if let loginResponse = response as LoginResponse {
self.loadingLogin.stopAnimating()
let homeViewController = self.storyboard!.instantiateViewControllerWithIdentifier("HomeViewController") as HomeViewController
homeViewController.loginResponse = loginResponse
// assign your instance ^^^^^^^^^^^^^^^^^^^^^^^^
self.showViewController(homeViewController, sender: self)
}
This really isn't very good structure, but it should at least answer your question.

Resources