Integrating Firebase into Ionic + CocoPods Google Toolbox workaround

October 7, 2018

Since the Mobile SDK for Google Analytics is officially deprecated, Google is now recommending Firebase Analytics. When creating your Mobile Application using Ionic, Firebase is fairly easy to integrate using the native Firebase Plugin. However, there are a few workarounds needed to get the project to build correctly.

Installation

  1. Create Firebase Account and App Profile at: https://console.firebase.google.com

  2. Download the config files and save to your project root folder. GoogleService-Info.plist for iOS. google-services.json for Android.

  3. Install plugin to project
    $ ionic cordova plugin add cordova-plugin-firebase
    $ npm install @ionic-native/firebase --save
    
  4. Add reference to config.xml file (if not element not present)
        <platform name="ios">
            <resource-file src="GoogleService-Info.plist" />
    

iOS Build Errors

You may encounter the error “ld: library not found for -lGoogleToolboxForMac” when building the project. This is caused by the CocoaPod dependencies and Ionic.

  1. Open a terminal to the platform/ios directory and run the following
    $ pod install
    
  2. When opening the project in XCode, use the .xcworkspace file instead of .xcodeproj

To streamline this process, create the following bash files to run before and after a platform is added (source: Ionic KB Article)

  1. Create the scripts

    • update_pods.sh
        #!/bin/bash
        pod repo update
      
    • run_pods.sh

        #!/bin/bash
        pod install --project-directory='./platforms/ios/'
      
  2. Reference the scripts in config.xml

    <platform name="ios">
        <hook src="update_pods.sh" type="before_platform_add" />
        <hook src="run_pods.sh" type="after_platform_add" />
    ...
    
  3. Add execute permissions to your scripts

    $ chmod +x update_pods.sh
    $ chmod +x run_pods.sh
    

Photo by Krivec Ales from Pexels