iOS - 从Drupal触发密码恢复电子邮件

时间:2017-02-17 01:55:29

标签: ios objective-c drupal

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

代码如下 - 我只是不确定我应该在sendButton中放入什么方法? DIOSUser? 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)

3 个答案:

答案 0 :(得分:5)

您应该执行以下操作,假设 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

干杯。

答案 1 :(得分:0)

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

{{1}}

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

答案 2 :(得分:0)

我最终用下面的代码完成了这个 - 发布任何人都认为它有用!两种略有不同的选择取决于您的数据库结构:

- (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) {

    }];

    }