Skip to content

Commit f3c2960

Browse files
feat(apple):Using Sentry withing an SDK
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 f129e57 commit f3c2960

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
title: Using Sentry within an SDK
3+
description: "Learn how to use the Sentry SDK within a shared environment, e.g. 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. 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+
38+
If your SDK can be opened and closed multiple times, you should also close the `Hub` when you are done:
39+
40+
```swift
41+
hub.close()
42+
```
43+
44+
Capturing crashes, watchdog terminations or app hangs aren't supported by the `Hub` API, because they depend on the global Sentry instance.
45+
46+
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)