I have a application that requires a person to login for them to gain access. To do this i created a API to retrieve and store data from a database. For example, i created one to check and see if the username and password inputed is correct. The only problem though, is that the function that i use to retrieve the data is slow. It does not return the data in time for the checking system, to see if the username and password is correct.
我有一个应用程序,需要一个人登录才能获得访问权限。为此,我创建了一个API来检索和存储数据库中的数据。例如,我创建了一个来检查并查看输入的用户名和密码是否正确。唯一的问题是,我用来检索数据的函数很慢。它不会及时返回检查系统的数据,以查看用户名和密码是否正确。
class LoginViewController: UIViewController {
var statusCode = Int()
var returnedName = String()
@IBOutlet var usernameField: UITextField
@IBOutlet var passwordField: UITextField
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func loginAction(sender: AnyObject) {
var invalidLoginAlert = UIAlertView(title: "Invalid Username/Password", message: "Please input a correct Username or Password", delegate: nil, cancelButtonTitle: "Dismiss")
var noInfoLoginAlert = UIAlertView(title: "Invalid Username/Password", message: "Please input a Username and Password to Proceed", delegate: nil, cancelButtonTitle: "Dismiss")
usernameField.resignFirstResponder()
passwordField.resignFirstResponder()
if usernameField.text != "" && passwordField.text != "" {
login(usernameField.text.lowercaseString, password: passwordField.text.lowercaseString)
sleep(2)
if statusCode == 300 {
println("Login Success")
let MainView = self.storyboard.instantiateViewControllerWithIdentifier("MainView") as MainViewController
self.presentViewController(MainView, animated: true, completion: nil)
}
else {
invalidLoginAlert.show()
println("Login Failed")
}
}
else {
noInfoLoginAlert.show()
println("Cannot Login, Invalid Information")
}
}
As you see above, i have to use the sleep() function in order to slow down the function. This does not work always, because the connection is slower in different places (may take longer than 2 seconds to load) EX - 3G and LTE or slow WiFi.
如上所示,我必须使用sleep()函数来减慢功能。这并不总是有效,因为在不同的地方连接速度较慢(加载时间可能超过2秒)EX - 3G和LTE或慢速WiFi。
The function Login(username: string, password: string), returns a code determining if the username and password is correct. If they are both correct, it will return the code 300, if not it will return 400.
函数Login(用户名:字符串,密码:字符串)返回一个代码,用于确定用户名和密码是否正确。如果它们都是正确的,它将返回代码300,否则它将返回400。
Is there a way that i could could create a didFinishLoading() function, returning a bool, to check and see if the information from the login() function is loaded? this way it will not matter if their connection is slow.
有没有办法可以创建一个didFinishLoading()函数,返回一个bool,检查并查看login()函数中的信息是否已加载?这样,他们的连接是否缓慢无关紧要。
Blake
布莱克
EDIT: Here is my Networking Code:
编辑:这是我的网络代码:
func login(username: String, password: String){
// Now escape anything else that isn't URL-friendly
var usernameID = username.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
var passwordID = password.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
var urlPath = "http://www.example.com"
var url: NSURL = NSURL(string: urlPath)
var session = NSURLSession.sharedSession()
var task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in
var err: NSError?
var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary
var name: String? = jsonResult["data"] as String?;
var code: Int = jsonResult["status"] as Int;
self.statusCode = code
println(self.statusCode)
})
task.resume()
}
1 个解决方案
#1
1
In your completion handler - where you are computing jsonResult
- that is where you should notify your view controller that username and password have or have not been received.
在您的完成处理程序中 - 您正在计算jsonResult - 您应该通知您的视图控制器已经或者没有收到用户名和密码。
#1
1
In your completion handler - where you are computing jsonResult
- that is where you should notify your view controller that username and password have or have not been received.
在您的完成处理程序中 - 您正在计算jsonResult - 您应该通知您的视图控制器已经或者没有收到用户名和密码。