diff --git a/CHANGELOG.md b/CHANGELOG.md index b51268911..a76c8f3d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ Bugsnag Notifiers on other platforms. ## Enhancements +* Add getter for `Bugsnag.context` + [#554](https://github.com/bugsnag/bugsnag-cocoa/pull/554) + * Make `BugsnagUser` properties readonly [#556](https://github.com/bugsnag/bugsnag-cocoa/pull/556) diff --git a/Source/Bugsnag.h b/Source/Bugsnag.h index bc608e341..c5ef92277 100644 --- a/Source/Bugsnag.h +++ b/Source/Bugsnag.h @@ -217,12 +217,15 @@ // ============================================================================= /** - * Replicates BugsnagConfiguration.context - * - * @param context A general summary of what was happening in the application + * Retrieves the context - a general summary of what was happening in the application */ + (void)setContext:(NSString *_Nullable)context; +/** + * Retrieves the context - a general summary of what was happening in the application + */ ++ (NSString *_Nullable)context; + // ============================================================================= // MARK: - User // ============================================================================= diff --git a/Source/Bugsnag.m b/Source/Bugsnag.m index 91d85220a..4019771fb 100644 --- a/Source/Bugsnag.m +++ b/Source/Bugsnag.m @@ -299,6 +299,13 @@ + (void)setContext:(NSString *_Nullable)context { } } ++ (NSString *_Nullable)context { + if ([self bugsnagStarted]) { + return self.client.context; + } + return nil; +} + + (BugsnagUser *)user { return self.client.user; } diff --git a/Source/BugsnagClient.h b/Source/BugsnagClient.h index 1ba215d88..50e31d69c 100644 --- a/Source/BugsnagClient.h +++ b/Source/BugsnagClient.h @@ -204,11 +204,9 @@ NS_SWIFT_NAME(leaveBreadcrumb(_:metadata:type:)); // ============================================================================= /** - * Replicates BugsnagConfiguration.context - * - * @param context A general summary of what was happening in the application + * Retrieves the context - a general summary of what was happening in the application */ -- (void)setContext:(NSString *_Nullable)context; + @property NSString *_Nullable context; /** * @return YES if Bugsnag has been started and the previous launch crashed diff --git a/Source/BugsnagClient.m b/Source/BugsnagClient.m index f57409388..5441091f8 100644 --- a/Source/BugsnagClient.m +++ b/Source/BugsnagClient.m @@ -748,6 +748,10 @@ - (void)setContext:(NSString *_Nullable)context { self.configuration.context = context; } +- (NSString *)context { + return self.configuration.context; +} + // MARK: - Notify - (void)notifyError:(NSError *_Nonnull)error { diff --git a/Tests/BugsnagClientMirrorTest.m b/Tests/BugsnagClientMirrorTest.m index 7819970ee..8e941be3d 100644 --- a/Tests/BugsnagClientMirrorTest.m +++ b/Tests/BugsnagClientMirrorTest.m @@ -89,7 +89,8 @@ - (void)setUp { @"setMetadata: v24@0:8@16", @"metadata @16@0:8", @"workspaceBreadcrumbStateEvents @16@0:8", - @"startListeningForWorkspaceStateChangeNotifications: v24@0:8@16" + @"startListeningForWorkspaceStateChangeNotifications: v24@0:8@16", + @"context @16@0:8" ]]; // the following methods are implemented on Bugsnag but do not need to @@ -103,7 +104,8 @@ - (void)setUp { @"client @16@0:8", @"bugsnagStarted B16@0:8", @"bugsnagStarted c16@0:8", - @"leaveBreadcrumbWithBlock: v24@0:8@?16" + @"leaveBreadcrumbWithBlock: v24@0:8@?16", + @"getContext @16@0:8" ]]; }