iOS-从Drupal触发密码恢复电子邮件
在我的iOS应用程序中,我需要我的用户能够恢复/重置他们的密码。我使用Drupal iOS SDK来管理用户登录。一切都正常,但我正试图找出如何将用户的电子邮件地址发布到服务端点,以触发drupal密码恢复电子邮件例如用户将电子邮件输入UITextField,然后点击提交按钮。然而,似乎没有任何关于这方面的文档 代码如下-我只是不确定应该在sendButton中放什么方法?酒神?教区 DIOSUser.miOS-从Drupal触发密码恢复电子邮件,ios,objective-c,drupal,Ios,Objective C,Drupal,在我的iOS应用程序中,我需要我的用户能够恢复/重置他们的密码。我使用Drupal iOS SDK来管理用户登录。一切都正常,但我正试图找出如何将用户的电子邮件地址发布到服务端点,以触发drupal密码恢复电子邮件例如用户将电子邮件输入UITextField,然后点击提交按钮。然而,似乎没有任何关于这方面的文档 代码如下-我只是不确定应该在sendButton中放什么方法?酒神?教区 DIOSUser.m + (void)userSendPasswordRecoveryEmailWithEma
+ (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];
}
- (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];
}
ViewController.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];
}
- (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)
您只需执行以下操作,假设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];
}
}];
}
查找文档
干杯。您可以使用以下代码发送重置请求:
- (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];
}
}];
}
我希望这能对您有所帮助。我最后通过下面的代码完成了这项工作,以防其他人发现它有用!根据您的数据库结构,有两种稍有不同的选择:
- (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) {
}];
}
你们真是太棒了——尽管当我实现上述功能时,控制台会向我抛出一个“请求失败-找不到404-url:”错误。你知道这是为什么吗?@Brittany看起来你使用的SDK已经过时了,尽管如此,试一下DIOSUser的所有其他方法吧?只是在这个实例中找不到url?该路径应该是什么样子?是的,如果找不到URL端点,您可以深入了解DIOSUser的函数,了解他们是如何进行api端点调用的,您可以相应地修改您的调用。这个答案应该被接受,为什么您不接受您问题的答案,接受您的答案将帮助面临相同问题的其他人,@Brittany看到你未被接受的答案:1。2.你试过下面我的答案中提到的建议吗