Add Crashlytics to your WatchOS app
Tags: watchos, firebase, crashlytics, swift
Published: 2021-06-02 14:15:31

# Introduction
I've been working on a WatchKit extension for an existing iOS application recently and i went through a problem: Firebase Crashlytics integration on WatchOS. Since it was quite difficult for me to find a clear documentation about this on the internet i thought it would be interesting to share a step by step guide on how i did it for my project.

> This guide assumes you already have configured your product in Firebase Console. If it's not the case, you can follow the guide at [this url](https://firebase.google.com/docs/crashlytics/get-started?platform=ios#setup-console). Also make sure you have the GoogleService-Info.plist copied at the root of your WatchExtension folder.

# Integration

## 1. Adding the dependency
You'll need to add the dependency to your Podfile as you would on a normal iOS application; make sure to add it under the WatchKit extension target.

Example:
```shell
target 'ExampleApp Watch Extension' do
    platform :watchos, '5.1'
    # other dependencies
    pod 'Firebase/Crashlytics', '7.10.0'
end
``` 

Then you'll need to run the two command below to update your dependencies and launch your app in XCode.
```shell
$ pod install
$ open example-app.xcworkspace
```

## 2. Adding Firebase Crashlytics SDK to your app
- In your `ExtensionDelegate.swift` file, import the Firebase module:
```swift
import Firebase
```
- Next step is to configure FirebaseApp. It's recommanded to do this in the [applicationDidFinishLaunching()](https://developer.apple.com/documentation/watchkit/wkextensiondelegate/1628241-applicationdidfinishlaunching) method:
```swift
func applicationDidFinishLaunching() {
    // Firebase configuration
    FirebaseApp.configure()
}
```

- Compile your app again.

## 3. Crashlytics initialisation
You'll need to initialize crashlytics by adding a Build Phase to your project so XCode will automatically download your project's DSYM file each time your application will crash, so that Crashlytics will be able to automatically generate crash reports.
- **Step 1:** Open XCode and select the project in the left navigation.
- **Step 2:** Click on the WatchKit Extension target and go to the Build Phases tab.
- **Step 3:** Click the plus button on the top left side, then choose the "New Run Script Phase" option
- **Step 4:** Add this code 
```shell
"${PODS_ROOT}/FirebaseCrashlytics/run"
```

You should then have something like this :
![SC 1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9lf0b9e7cdp12gqiw16f.png)
 
> make sure that your new generation phase is the last generation phase of the project, otherwise Crashlytics cannot initialize correctly.

- **Step 5:** Take some beer, you've just accomplished something great !

## 4. Test your integration
Now we have configured Crashlytics in our project, we need to make sure it works.
To do this, we will create a test crash and see how it's handled by Crashlytics and showed in our console.

Go to your project settings, choose the Watch target (**i said Watch, not Watch Extension**); then go the the `Build settings` tab and type `debug information format` in the search bar. the value is probably 'DWARF', change it to `DWARF with dSYM File`.

Why this you'll ask me (yeah, i can read your mind ;) )
You actually need to disconnect your simulator from your XCode debugger to be able to see crash reports in the crashlytics console. (don't ask me why, I am not employed at Google, even if I dream of it).

Go to your app (for example the ExtensionDelegate.swift) file and add the code below after Firebase configuration:

```swift
func applicationDidFinishLaunching() {
    // Firebase configuration
    FirebaseApp.configure()
    Crashlytics.crashlytics().record(error: NSError(domain: "Test crash", code: 00, userInfo: nil))
}
```

When you are done, launch your app and you normally should be able to see your first report in your Crashlytics console.

Thanks for reading, and don't hesitate to leave a comment if you have any suggestion or questions.