Sample code for 30+ languages & platforms
Unicode C

Save Email Credentials in Apple Keychain or Windows Credentials Manager

See more Apple Keychain Examples

This example demonstrates how to save (or update) email credentials in the Apple Keychain.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkSecretsW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSecretsW secrets;
    HCkJsonObjectW json;

    success = FALSE;

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

    secrets = CkSecretsW_Create();

    // On Windows, this is the Windows Credentials Manager
    // On MacOS/iOS, it is the Apple Keychain
    CkSecretsW_putLocation(secrets,L"local_manager");

    // Specify the name of the secret.
    // service and username are required.
    // appName and domain are optional.
    // Note: The values are arbitrary and can be anything you want.
    json = CkJsonObjectW_Create();
    CkJsonObjectW_UpdateString(json,L"appName",L"MyEmailApp");
    CkJsonObjectW_UpdateString(json,L"service",L"SMTP");
    CkJsonObjectW_UpdateString(json,L"domain",L"example.com");
    CkJsonObjectW_UpdateString(json,L"username",L"joe@example.com");

    // Create or update the secret.
    // This secret is the SMTP password for the above email account.
    success = CkSecretsW_UpdateSecretStr(secrets,json,L"joes_password");
    if (success == FALSE) {
        wprintf(L"%s\n",CkSecretsW_lastErrorText(secrets));
        CkSecretsW_Dispose(secrets);
        CkJsonObjectW_Dispose(json);
        return;
    }

    // Let's save the IMAP secret for an account we'll read using IMAP..
    CkJsonObjectW_UpdateString(json,L"appName",L"MyEmailApp");
    CkJsonObjectW_UpdateString(json,L"service",L"IMAP");
    CkJsonObjectW_UpdateString(json,L"domain",L"example2.com");
    CkJsonObjectW_UpdateString(json,L"username",L"jane@example2.com");

    success = CkSecretsW_UpdateSecretStr(secrets,json,L"janes_password");
    if (success == FALSE) {
        wprintf(L"%s\n",CkSecretsW_lastErrorText(secrets));
        CkSecretsW_Dispose(secrets);
        CkJsonObjectW_Dispose(json);
        return;
    }

    wprintf(L"Success.\n");


    CkSecretsW_Dispose(secrets);
    CkJsonObjectW_Dispose(json);

    }