Example Login with basic auth authentication using Alamofire?

0

I'm trying to make a login screen where I use Alamofire to make the request, but with the examples from the Alamofire website itself, it's not working.

If someone can help with a sample code, I would appreciate it a lot !!

What's the problem? I try to use the basic auth from Alamofire to perform authentication, but I'm not trying to succeed.

Sample code I'm using and testing:

import UIKit

import Alamofire import SwiftyJSON

class LoginViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}


@IBOutlet weak var txtEmail: UITextField!

@IBOutlet weak var txtSenha: UITextField!


@IBAction func bntLogin(_ sender: Any) {



    //let user = txtEmail.text
    //let password = txtSenha.text
    //let credentialData = "\(user):\(password)".data(using: <#T##String.Encoding#>)
    //let base64Credentials = credentialData?.base64EncodedData(options: <#T##Data.Base64EncodingOptions#>)
    //let headers = ["Authorization": "Basic \(base64Credentials)"]


    //let user = txtEmail.text
    //let password = txtSenha.text
    /*
    var headers: HTTPHeaders = [:]

    if let authorizationHeader = Request.authorizationHeader(user: user!, password: password!) {
        headers[authorizationHeader.key] = authorizationHeader.value
    }

    Alamofire.request("https://www.url.com.br/wp-json/wp/v2/users", headers: headers).responseJSON {

        response in debugPrint(response)

    }
    */

    let username = txtEmail.text
    let password = txtSenha.text

    let loginString = String(format: "%@:%@", username!, password!)
    let loginData = loginString.data(using: String.Encoding.utf8)!
    let base64LoginString = loginData.base64EncodedString()

    //Cria uma requisição
    let url = URL(string: "https://www.url.com.br/wp-json/wp/v2/users")
    var request = URLRequest(url:url!)
    request.httpMethod = "POST"
    request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")

    // executa
    let urlConnection = NSURLConnection(request: request, delegate: self)


}







override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

    
asked by anonymous 06.03.2017 / 22:58

0 answers