Skip to content

AppAmbit Crashes iOS

AppAmbit Crashes automatically records a crash log whenever your application crashes. Initially, the log is saved to the device’s local storage. Once the app is reopened, the crash report is sent to AppAmbit, offering valuable insights to help diagnose and fix issues.

Generate a Test Crash

To simplify SDK testing, AppAmbit Crashes provides an API for generating a test crash.

Swift

Crashes.GenerateTestCrash();

Objective-C

[Crashes generateTestCrash];

Handled Errors

AppAmbit also supports tracking non-fatal errors by logging handled exceptions:

Swift

do {
    throw NSError(domain: "com.appambit", code: -1, userInfo: [NSLocalizedDescriptionKey: "Division by zero"])
}catch let error {
    Crashes.logError(exception: error)
}

Objective-C

@try {
        NSArray *array = @[];
        NSLog(@"%@", array[1]);
}
@catch (NSException *exception) {
    NSDictionary *userInfo = @{ NSLocalizedDescriptionKey: exception.reason };
    NSError *error = [NSError errorWithDomain:exception.name
                                          code:0
                                      userInfo:userInfo];

    [Crashes logErrorWithException:error properties:nil classFqn:nil fileName:nil lineNumber:0 completion:nil];
}

Besides, an application can attach properties to a controlled error report to provide more context. Pass the properties as a map of key-value pairs (strings only) as shown in the following example.

Swift

do {
    throw NSError(domain: "com.appambit", code: -1, userInfo: [NSLocalizedDescriptionKey: "Division by zero"])
} catch let error {
    let properties: [String: String] = ["user_id": "1"]
    let message = "Error NullPointerException"
    Crashes.logError(message: message, properties: properties, exception: error)
}

Objective-C

@try {
        NSArray *array = @[];
        NSLog(@"%@", array[1]);
}
@catch (NSException *exception) {
    NSMutableDictionary *props = [NSMutableDictionary dictionary];
    [props setObject:@"123" forKey:@"userId"];
    NSDictionary *userInfo = @{ NSLocalizedDescriptionKey: exception.reason };
    NSError *error = [NSError errorWithDomain:exception.name code:0 userInfo:userInfo];

    [Crashes logErrorWithMessage:(@"Error ArrayIndex") properties:props classFqn:nil exception:nil fileName:nil lineNumber:0 createdAt:nil completion:nil];
}

Additionally, you can log custom error messages for better visibility during unexpected situations:

Swift

Crashes.logError(message: "This is a test crash")

Objective-C

[Crashes logErrorWithMessage:(@"This is a test crash") properties:nil classFqn:nil exception:nil fileName:nil lineNumber:0 createdAt:nil completion:nil];

Even log the exception with properties and use the default message of the exception

Swift

do {
    throw NSError(domain: "com.appambit", code: -1, userInfo: [NSLocalizedDescriptionKey: "Division by zero"])
} catch let error {
    let properties: [String: String] = ["user_id": "1"]
    Crashes.logError(exception: error, properties: properties)
}

Objective-C

@try {
  NSArray *array = @[];
  NSLog(@"%@", array[1]);
}
@catch (NSException *exception) {
  NSMutableDictionary *props = [NSMutableDictionary dictionary];
  [props setObject:@"123" forKey:@"userId"];
  NSDictionary *userInfo = @{ NSLocalizedDescriptionKey: exception.reason };
  NSError *error = [NSError errorWithDomain:exception.name code:0 userInfo:userInfo];

  [Crashes logErrorWithException:error properties:props classFqn:nil fileName:nil lineNumber:0 completion:nil];
}

Details about the last crash

If the app has previously crashed, the function will return a boolean

Swift

Crashes.didCrashInLastSession { didCrash in
    // your code 
    ...
}

Objective-C

[Crashes didCrashInLastSessionWithCompletion:^(BOOL didCrash) {
    // your code
    ...
}];