iOS SDK (Swift)

A native Swift SDK for iOS and macOS applications with automatic batching, background delivery, offline log protection, and SSL pinning.

Offline Log Protection

When the API is unavailable (server maintenance, network errors, or outages), logs are automatically buffered in UserDefaults and delivered when connectivity is restored. Buffer capacity depends on your plan. No configuration needed — the SDK handles everything transparently.

Installation

Swift Package Manager

Add the package to your Xcode project:

  1. File → Add Packages
  2. Enter the repository URL: https://github.com/grendowdev/grenlogger-ios
  3. Select version and add to your target

Or add to your Package.swift:

dependencies: [
    .package(url: "https://github.com/grendowdev/grenlogger-ios", from: "1.0.0")
]

Initialization

import GrenLogger

// In AppDelegate or App struct
@main
struct MyApp: App {
    init() {
        // Basic initialization
        GrenLogger.initialize(
            apiKey: "your_api_key",
            userId: "user-123" // optional, required for remote activation
        )
    }
    
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

// Privacy-first: no logs until remote activation
GrenLogger.initialize(
    apiKey: "your_api_key",
    userId: "user-123",
    remote: false,
    local: true
)

Logging Methods

// Debug level
GrenLogger.debug("View appeared", metadata: ["viewName": "HomeView"])

// Info level
GrenLogger.info("User logged in", metadata: ["method": "apple"])

// Warning level
GrenLogger.warning("Network slow", metadata: ["latency": 3500])

// Error level
GrenLogger.error("API call failed", metadata: ["endpoint": "/users"])

// Error with NSError/Error
GrenLogger.error("Request failed", error: networkError)

// Fatal level (auto-flushes)
GrenLogger.fatal("Crash detected", error: fatalError)

User Management

// Set user ID after login
GrenLogger.setUserId("user-abc123")

// Get current user ID
if let userId = GrenLogger.getUserId() {
    print("Current user: \(userId)")
}

// Clear user ID on logout
GrenLogger.setUserId(nil)

Control Methods

// Manual flush
GrenLogger.flush()

// Refresh config from server (e.g. after push for remote activation)
Task {
    let success = await GrenLogger.sync()
}

// Shutdown (use in applicationWillTerminate)
GrenLogger.shutdown()

Call sync() when you receive a push notification to fetch the latest configuration immediately (e.g. after remote log activation).

SwiftUI Integration

struct ContentView: View {
    var body: some View {
        Button("Log Event") {
            GrenLogger.info("Button tapped", metadata: [
                "screen": "ContentView",
                "action": "tap"
            ])
        }
        .onAppear {
            GrenLogger.debug("ContentView appeared")
        }
        .onDisappear {
            GrenLogger.debug("ContentView disappeared")
        }
    }
}

App Lifecycle

// In SceneDelegate or AppDelegate
func sceneDidEnterBackground(_ scene: UIScene) {
    // Flush logs when entering background
    GrenLogger.flush()
}

func applicationWillTerminate(_ application: UIApplication) {
    // Shutdown SDK on termination
    GrenLogger.shutdown()
}

Objective-C Support

The SDK is fully compatible with Objective-C:

@import GrenLogger;

// Initialize
[GrenLogger initializeWithApiKey:@"your_api_key"
                       projectId:@"your_project_id"
                          userId:@"user-123"];

// Log
[GrenLogger info:@"User logged in" metadata:@{@"method": @"apple"}];

// Set user
[GrenLogger setUserId:@"new-user-id"];

Requirements

  • iOS 13.0+ / macOS 10.15+
  • Swift 5.5+
  • Xcode 13.0+