Skip to content

Commit

Permalink
Updated exampleApp code to be used for documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
vlussenburg committed Aug 25, 2022
1 parent afcee7f commit 8551020
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 42 deletions.
32 changes: 15 additions & 17 deletions Examples/Example-iOS-ObjC/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,47 @@ @interface AppDelegate () <BacktraceClientDelegate>
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSArray *paths = @[[[NSBundle mainBundle] pathForResource: @"test" ofType: @"txt"]];
NSString *fileName = @"myCustomFile.txt";
NSURL *libraryUrl = [[[NSFileManager defaultManager] URLsForDirectory:NSLibraryDirectory
inDomains:NSUserDomainMask] lastObject];
NSURL *fileUrl = [libraryUrl URLByAppendingPathComponent:fileName];

BacktraceCredentials *credentials = [[BacktraceCredentials alloc]
initWithEndpoint: [NSURL URLWithString: Keys.backtraceUrl]
token: [Keys backtraceToken]];
BacktraceDatabaseSettings *backtraceDatabaseSettings = [[BacktraceDatabaseSettings alloc] init];
backtraceDatabaseSettings.maxRecordCount = 1000;
backtraceDatabaseSettings.maxDatabaseSize = 10;
backtraceDatabaseSettings.retryInterval = 5;
backtraceDatabaseSettings.retryLimit = 3;
backtraceDatabaseSettings.retryBehaviour = RetryBehaviourInterval;
backtraceDatabaseSettings.retryOrder = RetryOrderStack;

backtraceDatabaseSettings.maxRecordCount = 10;

BacktraceClientConfiguration *configuration = [[BacktraceClientConfiguration alloc]
initWithCredentials: credentials
dbSettings: backtraceDatabaseSettings
reportsPerMin: 3
allowsAttachingDebugger: TRUE
detectOOM: TRUE];
BacktraceClient.shared = [[BacktraceClient alloc] initWithConfiguration: configuration error: nil];
BacktraceClient.shared.delegate = self;
[BacktraceClient.shared enableBreadcrumbs];
BacktraceClient.shared.attributes = @{@"foo": @"bar", @"testing": @YES};
BacktraceClient.shared.attachments = [NSArray arrayWithObjects:fileUrl, nil];

// sending NSException
@try {
NSArray *array = @[];
NSObject *object = array[1]; // will throw exception
array[1]; // will throw exception
} @catch (NSException *exception) {
NSArray *paths = @[[[NSBundle mainBundle] pathForResource: @"test" ofType: @"txt"]];
[[BacktraceClient shared] sendWithAttachmentPaths: paths completion: ^(BacktraceResult * _Nonnull result) {
[[BacktraceClient shared] sendWithAttachmentPaths: [NSArray init] completion: ^(BacktraceResult * _Nonnull result) {
NSLog(@"%@", result);
}];
} @finally {

}

//sending NSError
NSError *error = [NSError errorWithDomain: @"backtrace.domain" code: 100 userInfo: @{}];
NSArray *paths = @[[[NSBundle mainBundle] pathForResource: @"test" ofType: @"txt"]];
[[BacktraceClient shared] sendWithAttachmentPaths: paths completion: ^(BacktraceResult * _Nonnull result) {
NSLog(@"%@", result);
}];



BacktraceClient.shared.delegate = self;
[BacktraceClient.shared enableBreadcrumbs];
NSDictionary *attributes = @{@"My Attribute":@"My Attribute Value"};
[[BacktraceClient shared] addBreadcrumb:@"My Native Breadcrumb"
attributes:attributes
Expand Down
2 changes: 1 addition & 1 deletion Examples/Example-iOS-ObjC/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ - (IBAction) liveReportAction: (id) sender {

- (IBAction) crashAction: (id) sender {
NSArray *array = @[];
NSObject *o = array[1];
array[1];
}


Expand Down
39 changes: 15 additions & 24 deletions Examples/Example-iOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ func throwingFunc() throws {

@UIApplicationMain
final class AppDelegate: UIResponder, UIApplicationDelegate {


let fileUrl = createAndWriteFile("sample.txt")

var window: UIWindow?

func application(_ application: UIApplication,
Expand All @@ -20,38 +22,29 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
token: Keys.backtraceToken as String)

let backtraceDatabaseSettings = BacktraceDatabaseSettings()
backtraceDatabaseSettings.maxRecordCount = 1000
backtraceDatabaseSettings.maxDatabaseSize = 10
backtraceDatabaseSettings.retryInterval = 5
backtraceDatabaseSettings.retryLimit = 3
backtraceDatabaseSettings.retryBehaviour = RetryBehaviour.interval
backtraceDatabaseSettings.retryOrder = RetryOrder.queue
backtraceDatabaseSettings.maxRecordCount = 10
let backtraceConfiguration = BacktraceClientConfiguration(credentials: backtraceCredentials,
dbSettings: backtraceDatabaseSettings,
reportsPerMin: 10,
allowsAttachingDebugger: true,
detectOOM: true)
BacktraceClient.shared = try? BacktraceClient(configuration: backtraceConfiguration)
BacktraceClient.shared?.delegate = self
BacktraceClient.shared?.attributes = ["foo": "bar", "testing": true]
BacktraceClient.shared?.enableBreadcrumbs()

let fileName = "sample.txt"
guard let fileUrl = try? createAndWriteFile(fileName) else {
print("Could not create the file attachment")
return false
}
BacktraceClient.shared?.attachments.append(fileUrl)

BacktraceClient.shared?.loggingDestinations = [BacktraceBaseDestination(level: .debug)]
do {
try throwingFunc()
} catch {
let filePath = Bundle.main.path(forResource: "test", ofType: "txt")!
BacktraceClient.shared?.send(attachmentPaths: [filePath]) { (result) in
BacktraceClient.shared?.send(attachmentPaths: []) { (result) in
print("AppDelegate:Result:\(result)")
}
}


BacktraceClient.shared?.delegate = self
BacktraceClient.shared?.loggingDestinations = [BacktraceBaseDestination(level: .debug)]

BacktraceClient.shared?.enableBreadcrumbs()
let attributes = ["My Attribute":"My Attribute Value"]
_ = BacktraceClient.shared?.addBreadcrumb("My Breadcrumb",
attributes: attributes,
Expand All @@ -60,12 +53,10 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
return true
}

func createAndWriteFile(_ fileName: String) throws -> URL {
static func createAndWriteFile(_ fileName: String) -> URL {
let dirName = "directory"
guard let libraryDirectoryUrl = try? FileManager.default.url(
for: .libraryDirectory, in: .userDomainMask, appropriateFor: nil, create: true) else {
throw CustomError.runtimeError
}
let libraryDirectoryUrl = try! FileManager.default.url(
for: .libraryDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let directoryUrl = libraryDirectoryUrl.appendingPathComponent(dirName)
try? FileManager().createDirectory(
at: directoryUrl,
Expand All @@ -76,7 +67,7 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
let formatter = DateFormatter()
formatter.timeStyle = .medium
let myData = formatter.string(from: Date())
try myData.write(to: fileUrl, atomically: true, encoding: .utf8)
try! myData.write(to: fileUrl, atomically: true, encoding: .utf8)
return fileUrl
}
}
Expand Down

0 comments on commit 8551020

Please sign in to comment.