Crashlytics and Firebase are usually installed with CocoaPods, but what if you were using Carthage as your dependency management system? Tutorial to the rescue! I’m going to explain this as simply as possible as it was a bit of a pain hunting all the info down so hopefully this can help you out :-]

Getting Started

Firstly assuming you have Firebase installed, go ahead and add the dependencies to your Cartfile:

binary "https://dl.google.com/dl/firebase/ios/carthage/FirebaseAnalyticsBinary.json"
binary "https://dl.google.com/dl/firebase/ios/carthage/FirebaseCrashlyticsBinary.json"
binary "https://dl.google.com/dl/firebase/ios/carthage/FirebaseProtobufBinary.json"

Firebase and Crashlytics aren’t technically supported without using CocoaPods but it just requires a few steps so hang in there.

Go ahead and do a Carthage Update – note, this can take a while.

Once this is complete head to your project folder and inside the Carthage directory drag in all of the Google and Crashlytics Frameworks. You do this in the General tab inside Frameworks, Libraries & Embedded Content.

You’ll want to select Do Not Embed as we’re not worried about signing and linking before building the project. This way our dependencies will be linked at build time just fine.

Note – if you’re updating your dependencies its easier to just delete all of them from the project and drag them in once again into your dependencies in Xcode.

Build Errors

Once your update finishes your project might not build (as mine did). This is due to the fact Firebase has changed significantly of late and promises are now used throughout.

If you navigate back to your Carthage build directory where the other SDKs are, you’ll notice a new PromisesObjc.Framework file there. This is what we’re missing! Just drag that in alongside the other two frameworks we just imported.

Initialising the Framework

Fabric is now deprecated so the below will no longer work for you. Doing the above will give you the new Crashlytics library anyway so simply use import FirebaseCrashlytics and you’re good to go!

    /// Configures the app to use the correct plist for each environment
    private static func configureFirebase(for environment: BuildConfig.EnvironmentType) {
        
        let filePath = Bundle.main.path(forResource: plistName, ofType: "plist")
        guard let fileopts = FirebaseOptions(contentsOfFile: filePath!)
            else {
                assertionFailure("Couldn't load config file")
                return
            }
        FirebaseApp.configure(options: fileopts)

    }

Crashlytics will respect this plist that you’re swapping and it will talk to that target on your Firebase console!

Getting Detailed Reports

Whenever you build an app its either built for debug or production. Each build, lets say version 1.0.1, has a unique ID associated with it and any crash report will tie in perfectly with that ID. The thing is crash reports aren’t super-helpful if you need to know which line, or which file the crash happened. For that you’ll need to enable debug symbols (symbols being the functions, variables etc unique to your code) in the Build Settings for your project in Xcode.

Head to Build Settings and make sure All is toggled to see all options. Type Debug Information Format and make sure DWARF with dSYM File is enabled for any build you want to monitor for detailed crashes.

View those Crashes

Next head to the Firebase console and then Crashlytics. You’ll be prompted to enable Crashlytics and it will ask you to crash your app to view it.

Once that’s listening for crashes head back to your AppDelegate and import the library and crash that baby!

import FirebaseCrashlytics

...

 func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// run your configure code above first
Crashlytics.crashlytics().setCrashlyticsCollectionEnabled(true)
fatalError()

Follow these steps for a headache-free crash-reporting experience, trust me:The above just makes sure you get automatic crash logging enabled – without setting this to true sometimes it worked on a simulator for me but not device despite documentation saying otherwise

Now for the final tricky part – you need to run crashlytics manually. Cocoapods can do this for you but with Carthage its a little more involved. Fret not follow this below:

chmod +x upload-symbols
chmod +x run

This makes the two files executables. Finally head to Xcode and add a new Run Script in your Build Phases with the following:

"${PROJECT_DIR}/scripts/run"
  • Don’t forget to add input files to help with the symbols:




  • Build the production version of your app to a device
  • Detach the device from your machine
  • Run the app from the device and make sure it crashes
  • Attach it again and comment out your crash
  • Run the app once more to give it a chance to upload your crash
  • Wait 10 minutes or so and you’ll see your first crash in the Firebase console

Tada

Simple as that! All you did was add the library, make sure debug symbols were generated on a crash, upload the reports for each target and console environment on Firebase and make sure you forced a crash on an actual device.

If this helped you feel free to share it and have a crash-free day!

code-disciple

Author code-disciple

More posts by code-disciple

Leave a Reply