Sample code for 30+ languages & platforms
Objective-C

Configure HTTP Basic Authentication with SecureString

See more REST Examples

Demonstrates Rest.SetAuthBasicSecure, which configures HTTP Basic authentication using SecureString objects instead of plaintext strings.

Background. A SecureString keeps sensitive values encrypted in memory, reducing the window in which credentials appear as readable plaintext. This is otherwise equivalent to SetAuthBasic.

Chilkat Objective-C Downloads

Objective-C
#import <CkoRest.h>
#import <CkoSecureString.h>
#import <NSString.h>

BOOL success = NO;

CkoRest *rest = [[CkoRest alloc] init];
BOOL bTls = YES;
BOOL bAutoReconnect = YES;
success = [rest Connect: @"example.com" port: [NSNumber numberWithInt: 443] tls: bTls autoReconnect: bAutoReconnect];
if (success == NO) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

//  SetAuthBasicSecure configures HTTP Basic authentication using SecureString objects, which keep
//  the credentials encrypted in memory.
//  Normally you would not hard-code secrets in source.  You should instead obtain them from an
//  interactive prompt, environment variable, or a secrets vault.
CkoSecureString *ssUsername = [[CkoSecureString alloc] init];
[ssUsername Append: @"myUsername"];
CkoSecureString *ssPassword = [[CkoSecureString alloc] init];
[ssPassword Append: @"myPassword"];

success = [rest SetAuthBasicSecure: ssUsername password: ssPassword];
if (success == NO) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

NSString *responseText = [rest FullRequestNoBody: @"GET" uriPath: @"/api/protected"];
if (rest.LastMethodSuccess == NO) {
    NSLog(@"%@",rest.LastErrorText);
    return;
}

NSLog(@"%@",responseText);