Skip to content
LogZuno

Objective-C event tracking

Send events to LogZuno from Objective-C, with code that is tested against the API.

This example sends one event to LogZuno's ingestion API from Objective-C. It is the exact file our snippet tests send to the API before the docs are published.

Requirements

macOS or iOS with Foundation; clang with ARC.

Send an event

// Objective-C with Foundation. Build: clang -fobjc-arc -framework Foundation logzuno.m -o logzuno
#import <Foundation/Foundation.h>

int main(void) {
    @autoreleasepool {
        NSDictionary *env = NSProcessInfo.processInfo.environment;
        NSString *endpoint = env[@"LOGZUNO_ENDPOINT"] ?: @"https://api.logzuno.app/v1";

        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[endpoint stringByAppendingString:@"/log"]]];
        request.HTTPMethod = @"POST";
        request.timeoutInterval = 10;
        [request setValue:[@"Bearer " stringByAppendingString:env[@"LOGZUNO_API_KEY"] ?: @""] forHTTPHeaderField:@"Authorization"];
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        request.HTTPBody = [NSJSONSerialization dataWithJSONObject:@{
            @"channel": @"signups",
            @"event": @"User signed up",
            @"user_id": @"user-123",
            @"icon": @"🎉",
            @"notify": @YES,
            @"tags": @{@"plan": @"growth", @"source": @"website"},
        } options:0 error:nil];

        dispatch_semaphore_t done = dispatch_semaphore_create(0);
        [[NSURLSession.sharedSession dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
            if (error) {
                NSLog(@"request failed: %@", error);
            } else {
                printf("%ld %s\n", (long)((NSHTTPURLResponse *)response).statusCode, [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding].UTF8String);
            }
            dispatch_semaphore_signal(done);
        }] resume];
        dispatch_semaphore_wait(done, DISPATCH_TIME_FOREVER);
    }
    return 0;
}

Build with clang -fobjc-arc -framework Foundation logzuno.m -o logzuno, export LOGZUNO_API_KEY, and run ./logzuno. LogZuno answers 202 and the event appears in your project's live feed within seconds. Keep the key in an environment variable or your secrets manager, not in source control.

Notes for Objective-C

In apps, don't wait. The semaphore in the example keeps a command-line program alive until the response arrives. In an app, let the completion handler run on its own and never block the main thread.

Keys in apps. Ship only an ingest key, with New channels and New groups turned off.

Batch. Collect events and send up to 100 at a time to /v1/batch.

What the request contains

The body is the same in every language: a channel and an event, and optionally user_id, group_id, description, icon, notify and tags. All fields, limits and response codes are in sending events and the ingestion API reference.