Search code examples
iosobjective-cuiwebviewnsurlcredential

Saving username/password in UIWebView for future auto login


I am working to make a simple iOS app for a school website. The app has built-in UIWebView to load the login page of the website. The first time a user loads the page, he/she needs to login as before. I hope the app can then save the username/password for the user. So the next the user user the app, it autofills and submits the form to login automatically.

I have found several links from stack overflow.com are extremely helpful. For example,

[1] Is it possible for a UIWebView to save and autofill previously entered form values (e.g., username & password)?

[2] Autofill Username and Password in UIWebView

I tried to do the same for my program. The autofill part works fine for a hardcoded test username/password. However, I have got a problem to save the credentials from the UIWebView. The "stringByEvaluatingJavaScriptFromString: @" method doesn't retrieve the element from HTML.

My code is like below. I load the website with viewDidLoad and want to save the credentials with webView.

- (void)viewDidLoad {
  [super viewDidLoad];
  NSURL *url = [NSURL URLWithString:urlString];
  NSString *urlString = @"https://learn.packagingschool.com/users/sign_in";
  NSURL *url = [NSURL URLWithString:urlString];
  NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
  [self.webView loadRequest:urlRequest];
  [self.webView setDelegate:self];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; {

    //save form data
    if(navigationType == UIWebViewNavigationTypeFormSubmitted) {

    //grab the data from the page
    NSString *inputEmail = [webView stringByEvaluatingJavaScriptFromString: @"document.getElementByID('user_email').value"];
    NSString *inputPassword = [webView stringByEvaluatingJavaScriptFromString: @"document.getElenebtByName('user[password]')[0].value"];

    NSLog(@"email is %@\npassword is %@", inputEmail, inputPassword);

    //store values locally
    [[NSUserDefaults standardUserDefaults] setObject:inputEmail forKey:@"email"];
    [[NSUserDefaults standardUserDefaults] setObject:inputPassword forKey:@"password"];

    NSLog(@"email is %@\npassword is %@", inputEmail, inputPassword);

    //[SFHFKeychainUtils storeUsername:username andPassword:password forServiceName:@"MyService" updateExisting:YES error:nil];

  }    
  return YES;
}

When I click the submit button in the webView, there is no string retrieved from the website.

2017-02-23 11:19:46.732 Packaging School[441:5425] email is 
password is 
2017-02-23 11:19:46.755 Packaging School[441:5425] email is 
password is 

Please help me to solve the problem. Thank you very much!


Solution

  • Change

    NSString *inputEmail = [webView stringByEvaluatingJavaScriptFromString: @"document.getElementByID('user_email').value"];
    NSString *inputPassword = [webView stringByEvaluatingJavaScriptFromString: @"document.getElenebtByName('user[password]')[0].value"];
    

    To

    NSString *inputEmail = [webView stringByEvaluatingJavaScriptFromString: @"document.getElementById('user_email').value"];
    NSString *inputPassword = [webView stringByEvaluatingJavaScriptFromString: @"document.getElementsByName('user[password]')[0].value"];