DEV Community

Cover image for Add Crashlytics to your WatchOS app
Steve
Steve

Posted on • Edited on

Add Crashlytics to your WatchOS app

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. 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:

target 'ExampleApp Watch Extension' do
    platform :watchos, '5.1'
    # other dependencies
    pod 'Firebase/Crashlytics', '7.10.0'
end
Enter fullscreen mode Exit fullscreen mode

Then you'll need to run the two command below to update your dependencies and launch your app in XCode.

$ pod install
$ open example-app.xcworkspace
Enter fullscreen mode Exit fullscreen mode

2. Adding Firebase Crashlytics SDK to your app

  • In your ExtensionDelegate.swift file, import the Firebase module:
import Firebase
Enter fullscreen mode Exit fullscreen mode
func applicationDidFinishLaunching() {
    // Firebase configuration
    FirebaseApp.configure()
}
Enter fullscreen mode Exit fullscreen mode
  • 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
"${PODS_ROOT}/FirebaseCrashlytics/run"
Enter fullscreen mode Exit fullscreen mode

You should then have something like this :
SC 1

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:

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

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.

Top comments (0)