Saving username/password in UIWebView for future auto login

6k Views Asked by At

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!

2

There are 2 best solutions below

3
Peter Cheng On BEST ANSWER

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"];
1
SAM On
import UIKit
import WebKit
import WKCookieWebView
import CoreLocation
import SwiftKeychainWrapper

class MoviesViewController: UIViewController, WKUIDelegate, WKNavigationDelegate, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
var currentLocaton: CLLocation!

lazy var webView: WKCookieWebView = {
    let webView: WKCookieWebView = WKCookieWebView(frame: self.view.bounds)
    webView.translatesAutoresizingMaskIntoConstraints = false
    webView.navigationDelegate = self
    webView.wkNavigationDelegate = self
    return webView

}()

@IBOutlet weak var Activity: UIActivityIndicatorView!
@IBOutlet weak var SAMBACK: UIBarButtonItem!

@IBAction func SAMBACK(_ sender: Any) {
    if webView.canGoBack{
        webView.goBack()
    }
}

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    Activity.stopAnimating()
    SAMBACK.isEnabled = webView.canGoBack
}

func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
    Activity.stopAnimating()
}


override func viewDidLoad() {
    super.viewDidLoad()

    ArgAppUpdater.getSingleton().showUpdateWithForce()

    // SAM - Load Background webView to Black Color
    webView.backgroundColor = UIColor(red: 51/255, green: 51/255, blue: 51/255, alpha: 1)
    webView.isOpaque = false

    webView.allowsBackForwardNavigationGestures = true

    let webConfiguration = WKWebViewConfiguration()

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.stopMonitoringSignificantLocationChanges()

    // Do any additional setup after loading the view, typically from a nib.

    if CheckInternet.Connection(){

        setupWebView()
        webView.onDecidePolicyForNavigationAction = { (webView, navigationAction, decisionHandler) in
            decisionHandler(.allow)
        }

        webView.onDecidePolicyForNavigationResponse = { (webView, navigationResponse, decisionHandler) in
            decisionHandler(.allow)
        }

        webView.onUpdateCookieStorage = { [weak self] (webView) in
            self?.printCookie()
        }

        // add activity
        self.webView.addSubview(self.Activity)
        self.Activity.startAnimating()
        self.webView.navigationDelegate = self
        self.Activity.hidesWhenStopped = true
        webView.load(URLRequest(url: URL(string: "http://www.gmail.com")!))

    }
    else{
        self.Alert(Message: "Your Device is not connect to internet, Please check your connection")
    }

    func locationAuthStatus(){
        if CLLocationManager.authorizationStatus() == .authorizedWhenInUse{

            currentLocaton = locationManager.location
            print(currentLocaton.coordinate.latitude)
            print(currentLocaton.coordinate.longitude)
        } else {

            locationManager.requestWhenInUseAuthorization()
            locationAuthStatus()
        }
    }
}

func Alert (Message: String){
    let alert = UIAlertController (title: "Prime Cineplex", message: "Your Device is not connect to internet, Please check your connection", preferredStyle: .alert)

    let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
        guard let settingsUrl = URL(string: UIApplication.openSettingsURLString) else {
            return
        }

        if UIApplication.shared.canOpenURL(settingsUrl) {
            UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
                print("Settings opened: \(success)") // Prints true
            })
        }
    }
    alert.addAction(settingsAction)
    //let cancelAction = UIAlertAction(title: "Exit", style: .cancel, handler: nil)
    //alert.addAction(cancelAction)
    present(alert, animated: true, completion: nil)
}

// MARK: - Private
private func setupWebView() {
    view.addSubview(webView)

    let views: [String: Any] = ["webView": webView]

    view.addConstraints(NSLayoutConstraint.constraints(
        withVisualFormat: "H:|-0-[webView]-0-|",
        options: [],
        metrics: nil,
        views: views))
    view.addConstraints(NSLayoutConstraint.constraints(
        withVisualFormat: "V:|-0-[webView]-0-|",
        options: [],
        metrics: nil,
        views: views))
}

private func printCookie() {
    print("=====================Cookies=====================")
    HTTPCookieStorage.shared.cookies?.forEach {
        print($0)
    }
    print("=================================================")
}

}