skip to Main Content

In order to authenticate a user into our database, I need to capture their login and password, concatenate both strings and then convert it to base64. I am new to swift programming and have tried several things I googled to no avail.

Here is what I have so far, that doesn’t work:

import UIKit

class ViewController: UIViewController {
    @IBOutlet weak var userName: UITextField!    
    @IBOutlet weak var password: UITextField!

    @IBAction func loginButton(_ sender: Any) {
        let login = (userName.text != nil) && (password.text != nil)
        let encoded = login.data(using: .utf8)?.base64EncodedString()
    }        
}

I keep getting two errors regardless of my approach:

Cannot infer contextual base in reference to member ‘utf8’

and

Value of type ‘Bool’ has no member ‘data’

Any help is appreciated.

3

Answers


  1. What you need is this:

    let encoded = 
      [userName, password]
        .compactMap(.text) // Extract the text if there is not nil
        .filter { !$0.isEmpty } // filter out empty strings
        .joined(separator: " ") // concatenate them using a space (you can use any separator you like or just pass in an empty string)
        .data(using: .utf8)? // make it data
        .base64EncodedString() // make it base 64
    

    And no force unwrap at all!

    Login or Signup to reply.
  2. You might be looking at something like the following:

    @IBAction func loginButton(_ sender: Any) {
        guard let name = userName?.text else { return } // No user name
        guard let password = password?.text else { return } // No password
        
        let combinedText = name + password
    //    let combinedText = [name, password].joined(separator: "&") Use this if you need a separator between the two strings
        
        guard let encoded = combinedText.data(using: .utf8) else { return } // Could not encode to UTF8 for some reason
        let base64Encoded = encoded.base64EncodedString()
    }
    

    To understand where you got wrong

    1. Writing something like userName.text != nil means "Is text of username null?" and is a boolean value as in YES/NO or true/false as in either it is null or it isn’t.
    2. Then let login = (userName.text != nil) && (password.text != nil) does not combine any string. It basically say "Are both, username and password, non-null?". And again together they both either are non-null or at least one of them is null. So it evaluates to another boolean value as in YES/NO or true/false.
    3. You then try to encode that as base64 encoded UTF8 string data

    From the code I provided all the safety checks are added and code can exit as doing nothing due to incorrect data. But if you wish to do it unsafely (your app my crash) you can do a one-liner:

    let base64Encoded = (userName.text! + password.text!).data(using: .utf8)!.base64EncodedString()
    
    Login or Signup to reply.
  3. It looks like you have a few misunderstandings in your code that are causing these issues.

    Firstly, this line of code:

    let login = (userName.text != nil) && (password.text != nil)
    

    is checking if userName.text and password.text are not nil, returning a boolean value (true or false).

    Then, with this line:

    let encoded = login.data(using: .utf8)?.base64EncodedString()
    

    you’re trying to convert a Bool to Data and encode it as a base64 string. However, the data(using:) method and base64EncodedString() function expect a String, not a Bool.

    Instead, you should be checking if userName.text and password.text are not nil, and if they’re not, concatenate them and convert that to a base64 string. Here’s how you can correct your code:

     @IBAction func loginButton(_ sender: Any) {
            guard let login = userName.text, let pwd = password.text else {
                print("Username or password is missing")
                return
            }
    
            let combined = login + pwd
    
            let data = Data(combined.utf8)
    
            let encoded = data.base64EncodedString()
    
            print(encoded)
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search