解密码重置功能

时间:2015-02-19 15:20:47

标签: ios swift parse-platform xcode6.1

我正在尝试在我的ios应用程序中使用密码重置进行解析,我似乎无法在swift中找到关于如何将其实现为按钮操作但没有找到任何内容的最佳方法的任何教程。

任何人都知道一个好看的地方吗? 这就是我到目前为止所拥有的

@IBAction func resetPassword(sender: AnyObject) {

    [PFUser requestPasswordResetForEmailInBackground:
        self.loginEmail.text
        block:^(BOOL succeeded, NSError *error)
        {
        [APP.hud hide:YES];
        if (!succeeded)
        {
        NSString *msg = @"Could not connect. Try again later.";
        [UIAlertView ok:msg];
        return;
        }

        [UIAlertView minimalistMessageFor:3.0
        title:nil
        message:@"Your password has been sent to your email address."
        then:^{ }];
        [self begin];
        }];

}

我在PFUser线上的这个容器文字错误中得到一个Expected表达式。

3 个答案:

答案 0 :(得分:5)

我在我正在使用的应用程序中使用以下内容,弹出警报框以输入您的电子邮件并在新警报框中确认结果:

@IBAction func resetPasswordPressed(sender: AnyObject) {

    let titlePrompt = UIAlertController(title: "Reset password",
        message: "Enter the email you registered with:",
        preferredStyle: .Alert)

    var titleTextField: UITextField?
    titlePrompt.addTextFieldWithConfigurationHandler { (textField) -> Void in
        titleTextField = textField
        textField.placeholder = "Email"
    }

    let cancelAction: UIAlertAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil)

    titlePrompt.addAction(cancelAction)

    titlePrompt.addAction(UIAlertAction(title: "Reset", style: .Destructive, handler: { (action) -> Void in
            if let textField = titleTextField {
                self.resetPassword(textField.text)
            }
    }))

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

func resetPassword(email : String){

    // convert the email string to lower case
    let emailToLowerCase = email.lowercaseString
    // remove any whitespaces before and after the email address
    let emailClean = emailToLowerCase.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())

    PFUser.requestPasswordResetForEmailInBackground(emailClean) { (success, error) -> Void in
        if (error == nil) {
            let success = UIAlertController(title: "Success", message: "Success! Check your email!", preferredStyle: .Alert)
            let okButton = UIAlertAction(title: "OK", style: .Default, handler: nil)
            success.addAction(okButton)
            self.presentViewController(success, animated: false, completion: nil)

        }else {
            let errormessage = error!.userInfo!["error"] as! NSString
            let error = UIAlertController(title: "Cannot complete request", message: errormessage as String, preferredStyle: .Alert)
            let okButton = UIAlertAction(title: "OK", style: .Default, handler: nil)
            error.addAction(okButton)
            self.presentViewController(error, animated: false, completion: nil)
        }
    }
}

将您的电子邮件用户注册表转换为小写,然后将其与解析同步,这与我使用重置方法显示的方式类似。

如果要放入活动指示器,以便用户看到应用程序在操作之间忙碌,请创建以下两种方法并在重置操作和恢复()之前或之后调用Pause()在resetPassword方法中使用if / else。还包括以下类变量: var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()

func pause(){
    activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
    activityIndicator.center = self.view.center
    activityIndicator.hidesWhenStopped = true
    activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
    view.addSubview(activityIndicator)
    activityIndicator.startAnimating()
    UIApplication.sharedApplication().beginIgnoringInteractionEvents()
}

func restore(){
    activityIndicator.stopAnimating()
    UIApplication.sharedApplication().endIgnoringInteractionEvents()
}

答案 1 :(得分:1)

看起来你正在快速地与Objective-c混合。我假设你在一个快速的课堂上。请尝试以下方法:

@IBAction func resetPassword(sender: AnyObject) {
    PFUser.requestPasswordResetForEmailInBackground(self.loginEmail.text)

    var alert = UIAlertController(title: nil, message: "Your password has been sent to your email address.", preferredStyle: UIAlertControllerStyle.Alert)
    alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler: nil))
    self.presentViewController(alert, animated: true, completion: nil)
}

答案 2 :(得分:1)

我遇到了类似的问题,问题是该文档显然没有更新

PFUser.requestPasswordResetForEmailInBackground("email@example.com")

以下事情对我有用

PFUser.requestPasswordResetForEmailInBackground("email@example.com", nil)

希望这会有所帮助