Xamarin iOS – Integrating Fabric / Crashlytics

This article details how you can install Crashlytics to a Xamarin iOS applicaton and link it to Fabric. This content also appears in my Using Crashlytics With Xamarin Apps post where I walk through all the steps required to distribute Xamarin apps for beta testing with Fabric.

To complete this guide you will need an Enrolled Apple Developer Account and have created a valid app id & distribution certificate, find out how to do this here.

XAMARIN PROJECT SETUP (OPTIONAL)

Lets create our Xamarin project, if you already have a working app you can just ignore this step, just make sure the App Identifier in your info.plist is the same as what you made in your developer account.

Create a new Xamarin project, it can be a shared or pcl (or even prism) this bit doesn’t matter. Create the app with whatever name you want and copy across your bundle identifier.Screen Shot 2018-03-02 at 15.20.56.png

Once your project has been created you are ready to hop into Xcode and configure Fabric.

CREATING AN XCODE PROJECT WITH FABRIC

Create a new Xcode project, you will need to do this even if you have a fully functioning Xamarin App. What we are going to do is create an app with the same Bundle ID as our Xamarin App and trick fabric.

In Xcode go to File -> New -> Project and create a Single View Application. Call the project something and fill in your details, however what you call it at this point is irrelevant. We will need to change the Bundle ID manually anyway so call it whatever you want.

Note, It’s probably a good idea to label your app as iOS so that you don’t get confused which app is which later!

Screen Shot 2018-03-02 at 15.23.32.png

After your project has been created you will be greeted with the info.plist, change the Bundle ID to what you used in your developer account.Screen Shot 2018-03-02 at 15.27.50.png

You don’t need to worry about code signing in this project, only the bundle id.

Now you will need to open up the Fabric Mac app and sign in.Screen Shot 2018-03-02 at 15.29.39.png

Once you are logged in you should go onto the main menu and click + New App.

Screen Shot 2018-03-02 at 15.31.06.png

Now we just follow the installation instructions given to use by Fabric and integrate crashlytics with the Xcode project app. First select the Xcode project that you just created (it should be the top hit on the fabric browser but you could use the finder to be safe). Select the project and hit next.Screen Shot 2018-03-02 at 15.36.55.png

Now Fabric will ask you what products you wish to install, select Crashlytics.

Screen Shot 2018-03-02 at 15.39.20.png

The first thing you will need to do is add a run script to your Xcode project.

Screen Shot 2018-03-02 at 15.41.12.png

Copy the command that Fabric gives you and navigate to your Xcode project. In your Xcode project file navigate to Build Phases.

Screen Shot 2018-03-02 at 15.42.43.png

Click on the + button (top left, beneath the app icon) and add a new Run Script Phase

Screen Shot 2018-03-02 at 15.42.52.png

Now open up the new run script and copy in the text that Fabric gave you.

Screen Shot 2018-03-02 at 15.43.02.png

Now build your project (⌘+B) and Fabric should notice and prompt you to install some frameworks. Drag the frameworks into your Xcode project and accept any dialogue boxes that appear.

Screen Shot 2018-03-02 at 15.46.52.png

Now the frameworks we need should have been installed into our Xcode project, you should now see them in the Navigation Window.

Screen Shot 2018-03-02 at 15.47.04.png

Now Fabric wants you to add some code to the app delegate, copy this across.Screen Shot 2018-03-02 at 15.49.37.png

Your App Delegate should look like this:

Screen Shot 2018-03-02 at 15.50.50.png

Click next on the Fabric app and now run your Xcode project (⌘+R).

Once your app is running the Fabric app should notice and it will notify you that you are done. At this point we are done with our Xcode project, we have successfully created the Xamarin iOS app in our Fabric account. Any iOS builds will be distributed via this project.

LINKING YOUR XAMARIN PROJECT TO FABRIC

Open up your Xamarin project and go into the iOS folder (we only need to deal with this folder, not the pcl / shared / android / other folders). Once again CHECK that your Bundle ID is THE SAME as what you entered into your developer account. I cannot stress how important this is.

Once you are sure you have the right bundle id you should open the info.plist of your Xamarin app up in Xcode (which is probably the easiest way to edit this file).Screen Shot 2018-03-02 at 16.01.21.png

COPY FABRIC API KEY INTO YOUR XAMARIN INFO.PLIST

Next open up the info.plist of the Xcode project that we just linked to Fabric. The script we ran on the Xcode project added a reference to our fabrics API key, so we need to add this to our Xamarin app. You can simply copy and paste the row across from the Xcode project to your Xamarin project. Make sure you save this file and close it.

Screen Shot 2018-06-26 at 12.14.47.png

Now we are ready to integrate Fabric & Crashlytics into our Xamarin.iOS project. On packages add the following Nuget packages: Fabric, Crashlytics.

Screen Shot 2018-03-02 at 16.06.44.png

Both packages are published by an author named “Michel Moreira”, make sure you get the latest versions of each. You only need to install there packages to the iOS project (which is handy).

Open up the AppDelegate.cs and add the following import statements:

using FabricSdk;
using CrashlyticsKit;

And inside the DidFinishLaunchingWithOptions method add the following code:

Crashlytics.Instance.Initialize();
FabricSdk.Fabric.Instance.Initialize();

Your app delegate should now look something like this (if you made a blank project)

Screen Shot 2018-03-02 at 16.17.09.png

Run your app on a simulator and you should notice logs published by Crashlytics, Congratulations you have now linked Crashlytics to your Xamarin.iOS app!

Screen Shot 2018-03-02 at 16.19.49.pngAt this point you no longer need the Xcode project you made however I wouldn’t recommend deleting it (just in case). I would find a place to archive it and put it up on the shelf.

 

The End

If this article helped you, please let me know by commenting below.

You can find me on the following sites:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s