【发布时间】:2015-05-24 09:06:50
【问题描述】:
我正在尝试在应用中实现登录功能。我有一个带有用户名和密码的 url,验证它们是否正确并返回 true 或 false。如果为 true,则应加载新的视图控制器。
1) 如何在我的viewController 中获取异步url 调用的结果?来自服务器的响应是正确的,并且 json 传递是正确的。但是我不确定如何在 viewController 中获得结果。我已经阅读了有关堆栈溢出的其他问题,其中一些提到了委托和完成处理程序的使用。我查看了如何使用委托,但我不太确定在这种情况下如何使用它。
这是我的代码。 在一个班级中,我有这个功能。我通过单例访问此功能。我在 viewController 的 shouldPerformSegueWithIdentifier 方法中调用它(如下所示)。
func loginRequest() -> Bool {
let urlPath: String = "http:**************.mybluemix.net/login/login.php?username=Tony&password=pass"
var url: NSURL = NSURL(string: urlPath)!
var request1: NSURLRequest = NSURLRequest(URL: url)
let queue:NSOperationQueue = NSOperationQueue()
var loginRequest:Bool = false
NSURLConnection.sendAsynchronousRequest(request1, queue: queue, completionHandler:{ (response: NSURLResponse!, data: NSData!, error: NSError!) -> Void in
var err: NSError
var jsonResult: AnyObject? = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.AllowFragments, error: nil)
println("AsSynchronous\(jsonResult)")
if let jsonDictionary = jsonResult as? NSDictionary {
if let success = jsonDictionary["Success"] as? NSString {
if success == "true" {
// User has permission to login.
loginRequest = true
println("login should be true: \(loginRequest)")
}else{
// User does not have permission to login.
loginRequest = false
println("login should be false: \(loginRequest)")
}
}
}
})
}
这个函数在视图控制器中。
override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
if identifier == "SubmitLogin" {
// Username empty.
if(username.text.isEmpty){
let alert = UIAlertView()
alert.title = "no Text"
alert.message = "No username or password"
alert.addButtonWithTitle("OK")
alert.show()
return false
}else{
// Send login credentials to server.
var login = (Model.sharedInstance.loginRequest())
// loginRequest returns ture if credentials are correct.
if (Model.sharedInstance.loginRequest()){
// Start the next view controller.
return true
}else{
// Login failed
let alert = UIAlertView()
alert.title = "Failed Login"
alert.message = "The username or password was wrong. Please try again"
alert.addButtonWithTitle("OK")
alert.show()
return false
}
}
}
}
那么我如何使用代理和 NSURLConnection.sendAsynchronousRequest 来检索视图控制器中的结果并使用结果来确定我是否应该让用户登录?
****** 编辑澄清******
我有一个名为 BluemixDAO 的类,它看起来像:
类 BluemixDAO {
init(){
}
// The login function that I posted above
func loginRequest() -> Bool {
}
}
下一个类是用于调用 BluemixDAO loginRequest 函数的 Model。
class Model {
private let bluemix:BluemixDAO
private struct Static
{
static private var instance:Model?
}
class var sharedInstance: Model
{
if !(Static.instance != nil)
{
return Static.instance!
}
}
private init(){
bluemix = BluemixDAO()
}
// I call this function in my view controller using Model.sharedInstance.loginRequest()
func loginRequest() -> Bool
{
// This calls the function in the BluemixDAO class
return bluemix.loginRequest()
}
}
最后一个类是 viewController。它只有上面的函数“shouldPerformSegueWithIdentifier”
class viewController: UIViewController {
//This function is detailed at the top of the post.
override func shouldPerformSegueWithIdentifier(identifier: String?, sender:AnyObject?) -> Bool {
// I call the loginRequest() function in here.
}
}
我可能写的太多了。这应该是所有相关代码。 干杯
【问题讨论】:
标签: swift