From 8551020be9334f61cd9f27d39a7b4e7d2733d4b0 Mon Sep 17 00:00:00 2001 From: vlussenburg Date: Thu, 25 Aug 2022 09:42:26 -0600 Subject: [PATCH] Updated exampleApp code to be used for documentation --- Examples/Example-iOS-ObjC/AppDelegate.m | 32 +++++++++--------- Examples/Example-iOS-ObjC/ViewController.m | 2 +- Examples/Example-iOS/AppDelegate.swift | 39 +++++++++------------- 3 files changed, 31 insertions(+), 42 deletions(-) diff --git a/Examples/Example-iOS-ObjC/AppDelegate.m b/Examples/Example-iOS-ObjC/AppDelegate.m index 974c6f9c..a59f8f78 100644 --- a/Examples/Example-iOS-ObjC/AppDelegate.m +++ b/Examples/Example-iOS-ObjC/AppDelegate.m @@ -10,17 +10,18 @@ @interface AppDelegate () @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 @@ -28,16 +29,15 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( 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 { @@ -45,14 +45,12 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( } //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 diff --git a/Examples/Example-iOS-ObjC/ViewController.m b/Examples/Example-iOS-ObjC/ViewController.m index 9bbe1267..ae575573 100644 --- a/Examples/Example-iOS-ObjC/ViewController.m +++ b/Examples/Example-iOS-ObjC/ViewController.m @@ -33,7 +33,7 @@ - (IBAction) liveReportAction: (id) sender { - (IBAction) crashAction: (id) sender { NSArray *array = @[]; - NSObject *o = array[1]; + array[1]; } diff --git a/Examples/Example-iOS/AppDelegate.swift b/Examples/Example-iOS/AppDelegate.swift index c2bcad5a..27943cca 100644 --- a/Examples/Example-iOS/AppDelegate.swift +++ b/Examples/Example-iOS/AppDelegate.swift @@ -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, @@ -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, @@ -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, @@ -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 } }