Skip to content

Commit ff1085d

Browse files
feat(apple): Using Sentry within an SDK (#14973)
Similar to what we already have on Android, this docs page explains how users can use Sentry within an SDK. Fixes GH-14968
1 parent 6451768 commit ff1085d

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
title: Using Sentry Within an SDK
3+
description: "Learn how to use the Sentry SDK within a shared environment, such as another SDK."
4+
sidebar_order: 2000
5+
---
6+
7+
<Alert>
8+
9+
Using the Sentry SDK within another SDK is [discouraged](/platforms/). This can lead to unexpected behaviour and potential data leakage. The SentrySDK uses global state and can therefore **not** be initialized multiple times in parallel. When using the SentrySDK in your SDK, this will collide with apps using Sentry and your SDK. If you need to use Sentry within another SDK, please follow the best practices outlined below.
10+
11+
</Alert>
12+
13+
<Alert>
14+
15+
When setting up Sentry inside a library, the consuming app could use the Sentry SDK as well, thus you should **not use `SentrySDK.start()`**, as this will pollute the global state.
16+
17+
</Alert>
18+
19+
In order to not conflict with other Sentry instances, you should use the `Hub` API to create a new instance of Sentry. The Hub API works the similarly as the global Sentry instance, but it is not global and can be used within your component.
20+
21+
```swift
22+
import Sentry
23+
24+
let options = Options()
25+
options.dsn = "___PUBLIC_DSN___"
26+
27+
let client = SentryClient(options: options)
28+
29+
// You don't need to pass a scope. The hub will create a new scope for you automatically.
30+
// Furthermore, the SDK enriches it with some contexts automatically, such as app, OS and
31+
// device context.
32+
let hub = SentryHub(client: client, andScope: nil)
33+
34+
hub.capture(message: "Hello from your SDK!")
35+
```
36+
37+
If your SDK can be opened and closed multiple times, you should also close the `Hub` when you are done:
38+
39+
```swift
40+
hub.close()
41+
```
42+
43+
Capturing crashes, watchdog terminations or app hangs aren't supported by the `Hub` API, because they depend on the global Sentry instance.
44+
45+
To symbolicate stacktraces of your events, Sentry requires dSYMs (debug information files). Depending on your setup, you might not be able to get these, but if you can, visit <PlatformLink to="/dsym/">Uploading Debug Symbols</PlatformLink> for more information on how to upload them.

0 commit comments

Comments
 (0)