         }
         return true
     }
-
+    
 }

//
// Copyright 2016 Anton Tananaev (anton.tananaev@gmail.com)
// Copyright 2016 William Pearse (w.pearse@gmail.com)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.

        }
        
        if self.serverField.text?.characters.count > 0 && self.emailField.text?.characters.count > 0 {
            if let p = KeychainWrapper.standard.string(forKey: TCDefaultsPassKey) {
                self.passwordField.text = p
                self.loginButton.becomeFirstResponder()
            } else {
                self.passwordField.becomeFirstResponder()
            }
        }
        
    }
    
    @IBAction func loginButtonPressed() {
        
        MBProgressHUD.showAdded(to: view, animated: true)
        
        WebService.sharedInstance.authenticate(serverField!.text!, email: emailField!.text!, password: passwordField!.text!, onFailure: { errorString in
            
            DispatchQueue.main.async(execute: {
                
                MBProgressHUD.hide(for: self.view, animated: true)
                
                let ac = UIAlertController(title: "Couldn't Login", message: errorString, preferredStyle: .alert)
                let okAction = UIAlertAction(title: "OK", style: .default, handler: nil)
                ac.addAction(okAction)
                self.present(ac, animated: true, completion: nil)
            })
            
        }, onSuccess: { (user) in
            
            DispatchQueue.main.async(execute: {
                
                // save server, user
                let d = UserDefaults.standard
                d.setValue(self.serverField!.text!, forKey: TCDefaultsServerKey)
                d.setValue(self.emailField!.text!, forKey: TCDefaultsEmailKey)
                d.synchronize()
                
                //save password to keychain
                if self.rememberSwitch.isOn {
                    KeychainWrapper.standard.set(self.passwordField.text!, forKey: TCDefaultsPassKey)
                } else {
                    KeychainWrapper.standard.removeObject(forKey: TCDefaultsPassKey)
                }
                
                
                MBProgressHUD.hide(for: self.view, animated: true)
                
                self.dismiss(animated: true, completion: nil)
                
                self.performSegue(withIdentifier: "ShowMap", sender: self)
            })
        }
        )
    }
    
