Sample code for 30+ languages & platforms
Swift

HTTP Basic Authentication Test

Demonstrates how to do HTTP basic authentication using Chilkat.

Chilkat Swift Downloads

Swift

func chilkatTest() {
    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    let http = CkoHttp()!

    // To use HTTP Basic authentication:
    http.login = "myLogin"
    http.password = "myPassword"
    http.basicAuth = true

    // Run the test using this URL with the credentials above.  
    // (Works while httpbin.org keeps the test endpoint available.)
    var jsonResponse: String? = http.quickGetStr(url: "https://httpbin.org/basic-auth/myLogin/myPassword")
    if http.lastMethodSuccess == false {
        print("\(http.lastErrorText!)")
        return
    }

    print("Response status code: \(http.lastStatus.intValue)")

    print("\(jsonResponse!)")

    // Output:

    // Response status code: 200
    // {
    //   "authenticated": true, 
    //   "user": "myLogin"
    // }

}