ios - 在 Parse (Swift) 中接受登录参数

标签 ios swift parse-platform authentication

对于我的应用程序,它需要用户登录。如果用户登录无效,我需要让用户知道这一点。所以我想知道如果登录参数无效,如何调出 UIAlertView。它会自动在终端中让我知道其是否有效。但我需要知道如何创建 AlertView,以便用户知道他们是否成功输入了信息。

代码

IBAction func loginTapped(sender: AnyObject) {

    let username = usernameField.text
    let password = passwordField.text

    if username.isEmpty || password.isEmpty {
        var emptyFieldsError:UIAlertView = UIAlertView(title: "Please try again", message: "Please fill in all the fields we can get you logged in to your account.", delegate: self, cancelButtonTitle: "Try again")
        emptyFieldsError.show()
    }

    PFUser.logInWithUsernameInBackground(username, password:password) {
        (user: PFUser?, error: NSError?) -> Void in
        if user != nil {
            println("Sucessful")
        } else {
            println("Invalid login")
        }
    }

}

最佳答案

试试这个..:

 // Mark: Error message alert.

var errorMessage = String()

func alertView(title: String, message: String, buttonName: String){

    var alert:UIAlertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: buttonName, style: UIAlertActionStyle.Default, handler: { (action) -> Void in

        self.dismissViewControllerAnimated(true, completion: nil)

    }))

    self.presentViewController(alert, animated: true, completion: nil)
}


@IBAction func loginTapped(sender: AnyObject) {

    PFUser.logInWithUsernameInBackground(userName.text, password: password.text) { (user: PFUser?, error: NSError?) -> Void in

        if user != nil {

            // user logged in.. Perform Segue here..

            self.performSegueWithIdentifier("a", sender: self)

        } else {

            // Show error here..

            if let errorString = error!.userInfo?["error"] as? String {
                self.errorMessage = errorString

            }

            self.alertView("Error!", message: self.errorMessage, buttonName: "Try again")

        }


    }

}

关于ios - 在 Parse (Swift) 中接受登录参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32281290/

相关文章:

ios - 尝试在 Objective-C 中解析日期时出现错误

ios - 应用商店审核中的 IAP 错误,无法重现

ios - 将 View Controller View 添加到 subview 时发现 nil

ios - UIActivityViewController 的自定义 header

ios - 将元素附加到数组时遇到问题

ios - 使用 iOS 从特定指针 ID 加载多个图像

ios - 如果删除 PFUser,则以编程方式删除与 PFUser.objectID 关联的数据

android - 在设备上找不到图像

ios - 翠鸟导致主线程问题

javascript - Parse.com 失败,错误代码 : ReferenceError