xcode - linking login screen inputs to website inputs

Viewed 51

I am building an IOS App through XCode that has a login screen with username and password inputs. I also have a website completed with a login screen that can take you to my database. I do not want to talk to the database from the IOS App. When I hit the login button on the IOS App, I want the information in the username and password fields to fill themselves in on the website. I am essentially making a better looking GUI on the IOS app that just reads all its information from the website, just as the website looks for information on the database. I'm not sure how this can be done, but if you can point me in the right direction it would be helpful.

EDIT: here is a modified version of my IOS swift code. Just wondering where you would add the code linking the IOS app inputs to the websites.

import UIKit

class LoginController: UIViewController {
    //defining labels for username and password
    let usernameLabel: UILabel = {
        let ULabel = UILabel()
        ULabel.text = NSLocalizedString("Username", comment: "")
        ULabel.textColor = .gray
        ULabel.textAlignment = .center
        ULabel.font = ULabel.font.withSize(25)
        return ULabel
    }()
    let passwordLabel: UILabel = {
        let PLabel = UILabel()
        PLabel.text = NSLocalizedString("Password", comment: "")
        PLabel.textColor = .gray
        PLabel.textAlignment = .center
        PLabel.font = PLabel.font.withSize(25)
        return PLabel
    }()

    //defining text fields for username and password
    let usernameTextField: UITextField = {
        let UTextFieldDesign = UITextField()
        UTextFieldDesign.setBottomBorder(backGroundColor: BackColor, borderColor: LoginButtonColor)
        UTextFieldDesign.textColor = .white
        UTextFieldDesign.layer.cornerRadius = 3.0
        //UTextFieldDesign.placeholder = "  Username"
        UTextFieldDesign.textColor = .white
        //UTextFieldDesign.backgroundColor = .lightGray
        return UTextFieldDesign
    }()
    let passwordTextField: UITextField = {
        let PTextFieldDesign = UITextField()

        PTextFieldDesign.setBottomBorder(backGroundColor: BackColor, borderColor: LoginButtonColor)
        PTextFieldDesign.textColor = .white
        PTextFieldDesign.isSecureTextEntry = true
        PTextFieldDesign.layer.cornerRadius = 3.0
        //PTextFieldDesign.backgroundColor = .lightGray
        return PTextFieldDesign
    }()

    //designing login button for login screen
    let loginButton: UIButton = {
        let LButton = UIButton(type: .system)
        LButton.layer.cornerRadius = 25.0
        LButton.setTitleColor(.white, for: .normal)
        LButton.setTitle(NSLocalizedString("Sign In", comment: ""), for: .normal)
        LButton.titleLabel?.font = .systemFont(ofSize: 20)
        LButton.backgroundColor = LoginButtonColor
        LButton.addTarget(self, action: #selector(loginAction), for: .touchUpInside)
        return LButton
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.hideKeyboardWhenTap()
        navigationController?.isNavigationBarHidden = true
        navigationController?.isToolbarHidden = true
        view.backgroundColor = UIColor(red: 203/255, green: 215/255, blue: 242/255, alpha: 1.0)
        setupTextFieldComponents()
        setupLoginButton()
    }
    override func viewWillAppear(_ animated: Bool) {
        navigationController?.isNavigationBarHidden = true
        navigationController?.isToolbarHidden = true
        super.viewWillAppear(animated)
    }


    @objc func loginAction() {
        let selectioncontroller = SelectionController()
        navigationController?.pushViewController(selectioncontroller, animated: true)
    }

    fileprivate func setupTextFieldComponents() {
        setupUsernameText()
        setupUsernameField()
        setupPasswordText()
        setupPasswordField()
    }
    fileprivate func setupUsernameText() {

        view.addSubview(usernameLabel)
        usernameLabel.translatesAutoresizingMaskIntoConstraints = false
        usernameLabel.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 20).isActive = true
        usernameLabel.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20).isActive = true
        usernameLabel.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -20).isActive = true


    }
    fileprivate func setupPasswordText() {
        view.addSubview(passwordLabel)
        passwordLabel.translatesAutoresizingMaskIntoConstraints = false
        passwordLabel.topAnchor.constraint(equalTo: usernameTextField.bottomAnchor, constant: 15).isActive = true
        passwordLabel.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20).isActive = true
        passwordLabel.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -20).isActive = true


    }
    fileprivate func setupLoginButton() {
        view.addSubview(loginButton)
        loginButton.anchors(top: passwordTextField.bottomAnchor, topPad: 15, bottom: nil, bottomPad: 0,
                            left: passwordTextField.leftAnchor, leftPad: 0, right: passwordTextField.rightAnchor,
                            rightPad: 0, height: 50, width: 0)
        loginButton.translatesAutoresizingMaskIntoConstraints = false
    }


    fileprivate func setupUsernameField() {
        view.addSubview(usernameTextField)
        usernameTextField.anchors(top: usernameLabel.bottomAnchor, topPad: 12, bottom: nil, bottomPad: 0,
                                  left: view.leftAnchor, leftPad: 24, right: view.rightAnchor,
                                  rightPad: 24, height: 30, width: 0)
        usernameTextField.translatesAutoresizingMaskIntoConstraints = false
    }
    fileprivate func setupPasswordField() {
        view.addSubview(passwordTextField)
        passwordTextField.anchors(top: passwordLabel.bottomAnchor, topPad: 12, bottom: nil, bottomPad: 0,
                                  left: view.leftAnchor, leftPad: 24, right: view.rightAnchor,
                                  rightPad: 24, height: 30, width: 0)
        passwordTextField.translatesAutoresizingMaskIntoConstraints = false
    }

}
1 Answers

You can create hidden WKWebview on ViewController and url of WK is your Web User Login url, and post the values to Webinterface over View Controller inputs.

NSString *scriptSource = [NSString stringWithFormat:@" document.queryString('#username').value='@%';document.queryString('#password').value='@%';document.querySelector('form').submit;",[inputUsername text],[inputPassword     
WKUserScript *userScript = [[WKUserScript alloc] initWithSource:scriptSource
                                                      injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
                                                   forMainFrameOnly:YES];
[wkWebView.configuration.userContentController addUserScript:userScript];
[wkWebView reload];
Related