【问题标题】:iOS - Trigger Password Recovery Email from DrupaliOS - 从 Drupal 触发密码恢复电子邮件
【发布时间】:2017-02-17 01:55:29
【问题描述】:

在我的 iOS 应用中,我需要我的用户能够恢复/重置他们的密码。我正在使用 Drupal iOS SDK 来管理用户登录。一切正常,但是我试图弄清楚如何将用户的电子邮件地址发布到服务端点以触发 drupal 密码恢复电子邮件? 例如用户在 UITextField 中输入电子邮件,然后点击提交按钮。但是似乎没有任何文档?

代码如下 - 我只是不确定应该在我的 sendButton 中放置什么方法? DIOS用户? DIOSSession?

DIOSUser.m

 + (void)userSendPasswordRecoveryEmailWithEmailAddress: (NSString*)email

                                              success:(void (^)(AFHTTPRequestOperation *operation, id responseObject)) success
                                              failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error)) failure {

   NSString *path = [NSString stringWithFormat:@"user/request_new_password/%@", email];
     NSLog(@"This is the input email %@", email);

    [[DIOSSession sharedSession] sendRequestWithPath:path method:@"POST" params:nil success:success failure:failure];
}

ViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    self.forgotField.returnKeyType = UIReturnKeyDone;
    [self.forgotField setDelegate:self];

    // Do any additional setup after loading the view from its nib.

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
    [self.view addGestureRecognizer:tap];
}

- (IBAction)return:(id)sender {

     [self dismissViewControllerAnimated:YES completion:nil];

}
- (IBAction)sendButton:(id)sender {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Retrieving Password"
                                                    message:@"We're helping you retrieve your password! Please check your email in a few minutes for a rescue link."
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];

}

错误日志:

2017-07-12 22:29:34.264669-0700 myApp[4523:1331335] 
----- DIOS Failure -----
Status code: 404
URL: http://url.com/endpoint01/user/request_new_password/me@email.com
----- Response ----- 

----- Error ----- 
Request failed: not found (404)

【问题讨论】:

标签: ios objective-c drupal


【解决方案1】:

您应该简单地执行以下操作,假设 forgotField 将 emailID 作为输入并且您有适当的验证来检查有效的电子邮件。

- (IBAction)sendButton:(id)sender {

        [DIOSUser userSendPasswordRecoveryEmailWithEmailAddress:self.forgotField.text 
success:^(AFHTTPRequestOperation *operation, id responseObject) failure:^( AFHTTPRequestOperation *operation , NSError *error )){

         if(!error){
                  UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Retrieving Password"
                                                        message:@"We're helping you retrieve your password! Please check your email in a few minutes for a rescue link."
                                                       delegate:self
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
               [alert show];
         }

    }];

 }

查找文档here

干杯。

【讨论】:

  • 你太棒了 - 虽然当我实现上述内容时,控制台向我抛出“请求失败 - 未找到 404 - url:mywebsite/myendpoint/resetpassword/enteredemailaddress”错误。知道为什么会这样吗?
  • @Brittany 看起来你使用的 SDK 已经过时了,试试waterwheel
  • 即便如此,DIOSUser 的所有其他方法都有效吗?只是在此实例中找不到的 url?那条路径应该是什么样子?
  • 是的,没有找到 URL 端点,您可以深入了解 DIOSUser 的功能,他们是如何进行 api 端点调用的,您可以相应地修改您的调用。
  • 这个答案应该被接受,为什么你不接受你的问题的答案,接受你的答案将帮助面临同样问题的其他人,@Brittany 看到你不接受的答案:1.@987654324 @2 .stackoverflow.com/questions/44814745/…
【解决方案2】:

您可以使用以下代码发送重置请求:

     - (IBAction)sendButton:(id)sender {

     [DIOSUser userSendPasswordRecoveryEmailWithEmailAddress:self.txtForgotPassword.text
                                                                    success:^(AFHTTPRequestOperation *operation, id responseObject) {
     // Success Block   
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Retrieving Password" message:@"We have send you reset link to your email Please check your email." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
      [alert show];


        }failure:^( AFHTTPRequestOperation *operation , NSError *error ){

     // Failure Block
              if(!error){
       // error.description 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oopss" message: error.description delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
      [alert show];

             }
              }];
    }

我希望这会对你有所帮助。

【讨论】:

    【解决方案3】:

    我最终用下面的代码完成了这个 - 发布以防其他人发现它有用!根据您的数据库结构,有两种略有不同的替代方案:

    - (IBAction)sendButton:(id)sender {
    
        [[DIOSSession sharedSession] getCSRFTokenWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSString *csrfToken = [NSString stringWithUTF8String:[responseObject bytes]];
    
        NSString *email = self.forgotField.text;
    
        NSString *urlString2 = [NSString stringWithFormat:@"http://myapp.com/endpoint01/user/request_new_password?name=%@",
                            email];
        NSDictionary *jsonBodyDict = @{@"name":email};
        NSData *jsonBodyData = [NSJSONSerialization dataWithJSONObject:jsonBodyDict options:kNilOptions error:nil];
    
    
        NSMutableURLRequest *request = [NSMutableURLRequest new];
        request.HTTPMethod = @"POST";
    
        // for alternative 1:
        [request setURL:[NSURL URLWithString:urlString2]];
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    
        [request setHTTPBody:jsonBodyData];
    
        // for alternative 2:
        [request setURL:[NSURL URLWithString:urlString2]];
             [request addValue:csrfToken forHTTPHeaderField:@"X-CSRF-Token"];
    
        NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
        NSURLSession *session = [NSURLSession sessionWithConfiguration:config
                                                              delegate:nil
                                                         delegateQueue:[NSOperationQueue mainQueue]];
        NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                                completionHandler:^(NSData * _Nullable data,
                                                                    NSURLResponse * _Nullable response,
                                                                    NSError * _Nullable error) {
                                                    NSLog(@"Yay, done! Check for errors in response!");
    
                                                    NSHTTPURLResponse *asHTTPResponse = (NSHTTPURLResponse *) response;
                                                    NSLog(@"The response is: %@", asHTTPResponse);
                                                    // set a breakpoint on the last NSLog and investigate the response in the debugger
    
                                                    // if you get data, you can inspect that, too. If it's JSON, do one of these:
                                                    NSDictionary *forJSONObject = [NSJSONSerialization JSONObjectWithData:data
                                                                                                                  options:kNilOptions
                                                                                                                    error:nil];
                                                    // or
                                                    NSArray *forJSONArray = [NSJSONSerialization JSONObjectWithData:data
                                                                                                            options:kNilOptions
                                                                                                              error:nil];   
    
                                                }];
        [task resume];
    
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Retrieving Password"
                                                            message:@"We're helping you retrieve your password! Please check your email in a few minutes for a rescue link."
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
    
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    
        }];
    
        }
    

    【讨论】:

      猜你喜欢
      • 2012-07-07
      • 1970-01-01
      • 2014-12-11
      • 1970-01-01
      • 2011-10-07
      • 2010-11-05
      • 2018-10-07
      • 2010-11-30
      • 2013-04-19
      相关资源
      最近更新 更多