Swift:如何查询外部数据库以执行登录? [英] Swift: How to query an external database to perform login?

查看:130
本文介绍了Swift:如何查询外部数据库以执行登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Swift和iOS的新手,为了考试,我正在开发一个小的iOS应用。首先要做的是登录:在我的视图控制器中,我有2个文本字段(登录,密码)和一个登录按钮。我创建了一个带有属性的快速用户类(根据存储在数据库中的字段)。然后,我创建了一个登录服务swift类,其中(根据YouTube教程)其中有以下两个重要方法:

I'm very new to Swift and iOS and for an exam I am developing a little iOS app. First thing to do is login: in my view controller I have 2 text fields (login, password) and a "Sign in" button. I created a swift User Class with attributes (according to fields stored in database). Then I created a login service swift class where (following a Youtube tutorial) there are these two important methods:

func getUser(username:String, password:String, callback:(NSDictionary) -> ()) {
    self.settings.webServerLoginURL = "\(self.settings.webServerLoginURL)?username=\(username)&password=\(password)"
    println(self.settings.webServerLoginURL)
    self.requestLogin(self.settings.webServerLoginURL, callback: callback)

}

func requestLogin(url:String, callback:(NSDictionary) -> ()) {
    var nsURL = NSURL(string: url)
    let task = NSURLSession.sharedSession().dataTaskWithURL(nsURL!) {
        (data, response, error) in
        var error:NSError?
        var response = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &error) as! NSDictionary
        callback(response)

    }
    task.resume()

}

其中的URL应该是这样的:

where the URL is supposed to be something like this:

localhost:8888/excogitoweb/loginM.php?username=xxx&password=xxx

现在我想要尝试这些东西,因此在我的视图控制器快捷文件中,我已将按钮链接到以下方法:

Now I would like to try this stuff and so in my view controller swift file I have linked the button to the following method:

@IBAction func doLogin(sender : AnyObject) {
    self.service = LoginService()
    self.service.getUser(self.userTextField.text, self.passwordTextField.text)
}

我不了解的事情是如何调用该函数,其第三个参数应为回调函数以及总体上该如何管理响应数据以检查登录是否正确:如果是这样,则php文件应返回(查询数据库后)用户信息,

The things I am not understanding is how to call that function, whose third argument should be a callback, and overall how to manage with response data to check if login is corrected: if so the php file should return (after querying the database) user's info with

echo json_encode()

,所以我应该控制返回的数据是否为空。
您能帮我个忙还是链接更多有用的教程?

and so I should control if the returned data is empty or not. Can you help me please or link some more userful tutorial?

推荐答案

对于回调,让Xcode帮助您它的自动完成功能。

For the callback, let Xcode help you with its autocomplete feature.

开始输入您的方法调用:

Start typing your method call:

self.requestLogin

并接受自动完成。

现在您看到Xcode突出显示了回调部分;单击一次以选择它,然后按Enter,Xcode将自动按照以下格式正确格式化调用:

Now you see that Xcode has highlighted the callback part; click once on it to select it then hit ENTER, Xcode will automatically format the call properly like this:

self.requestLogin(self.settings.webServerLoginURL, { (myDictionary) -> () in
    println(myDictionary)
})

这篇关于Swift:如何查询外部数据库以执行登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆