【问题标题】:Parse Password reset function解析密码重置功能
【发布时间】:2015-02-19 15:20:46
【问题描述】:

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

有人知道一个很棒的地方吗? 这就是我目前所拥有的

@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 expression。

【问题讨论】:

  • 这段代码是用Objective-C写的,“只是”把它翻译成swift
  • 等等...您的代码在 swift 和 obj-c 中...这是为什么呢?你只需要将 Obj-C 代码翻译成 swift 吗?是这个问题吗?
  • 对不起,我是新手,但 Swift 是我正在学习和写作的。

标签: ios swift parse-platform xcode6.1


【解决方案1】:

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

@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)
        }
    }
}

在使用 parse 同步之前将用户注册的电子邮件转换为小写也很聪明,就像我使用重置方法展示的那样。

如果您想放入一个活动指示器,以便用户在操作之间看到应用程序忙碌,请创建以下两个方法并在重置操作中调用 Pause() 并在操作之前或之后调用 Restore() 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()
}

【讨论】:

  • 这对我正在使用 parse 进行的项目有所帮助。感谢您的回答!
【解决方案2】:

看起来您正在将 swift 与 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)
}

【讨论】:

    【解决方案3】:

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

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

    以下事情对我有用

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

    希望这会有所帮助

    【讨论】:

    • 猜猜更新的答案应该是:PFUser.requestPasswordResetForEmailInBackground("email@example.com", block: nil)
    猜你喜欢
    • 2019-06-04
    • 2013-05-17
    • 1970-01-01
    • 2013-09-12
    • 2020-07-05
    • 1970-01-01
    • 1970-01-01
    • 2019-11-06
    • 1970-01-01
    相关资源
    最近更新 更多