Using Firebase in a Share Extension

Today I will be showing you how to create a share extension that works with Firebase! If you are unfamiliar with Firebase and how to use it, check out my tutorials below!

What is a Share Extension

If you use an iOS device you most likely have used a share extension before. They are the different apps listed in the image below.

img_0166-png

They allow you to share links, images, and pretty much any file type that our app supports!

We can create a share extension for our app and this will allow users to share to our app or service!

Creating a Share Extension

Creating a Share Extension is very easy in XCode!

Just navigate to File -> New -> Target and under the iOS tab, click “Share Extension”

Screen Shot 2016-11-19 at 12.41.57 PM.png

Then name your Share Extension. (It is important to note that the name you give your Share Extension in this part is exactly what the user will see when sharing an item)

Screen Shot 2016-11-19 at 12.42.10 PM.png

Configuring your Share Extension

Recall that when you were setting up Firebase for your app there was some configuring that needed to be done before you could use the Firebase library.

  1. Add the GoogleService-Info.plist file to the project
  2. Configure the Podfile to install the Firebase library
  3. Call the FIRApp.configure() method before Firebase usage

Even if you have done these three steps in the core, it needs to also be done in the Share Extension. This is because the Share Extension is treated as a separate app since it can run on its own. For example, you can share links to Facebook but the Facebook app doesn’t have to be open on the phone when sharing to it. Therefore Share Extensions are like mini apps!

After creating the Share Extension, there should be a new folder in your project with the name of your share extension. You will see a View Controller in it. This View Controller has the method didSelectPost() that is called when the user selects “Post” in the UI.

Screen Shot 2016-11-19 at 1.43.26 PM.png

Configuring the Share Extension with Firebase is exactly the same as doing it for the core app.

First you need to download the GoogleService-Info.plist file again from your Firebase console under general settings. Then drag it into the same directory as your Share Extension View Controller. (Make sure to drag it into XCode. I have gotten weird behavior if you just drag it into the OSX finder directory)

The next step is to configure your Podfile for the Share Extension. To do this, just open the Podfile that you are currently using for the base app and add another target for the Share Extension. You can set pods globally for all of your targets. All you need to do is add the line platform :ios, ‘9.0’ at the top of the Podfile and add the pods for Firebase outside of the targets. Then the pods will apply to all of the targets. Lastly run the pod install command.

Problem with “per target” Pods

If you include the same pods within each target it can cause problems with installing them because you will be attempting to install duplicate libraries at the same time. Therefore it is better to define global pods when multiple targets share the same ones.

Screen Shot 2016-11-22 at 3.43.24 PM.png

The last step is to import Firebase into the View Controller and call the FIRApp.configure() method.
Screen Shot 2016-11-19 at 2.26.51 PM.png

The line at the end of the didSelectPost() method dismisses the view.

Testing the Share Extension

You can run the Share Extension to test it. To run it, click on the dropdown menu next to the play button at the top left of XCode and select your share extension and which device to run it on.

Screen Shot 2016-11-19 at 2.30.03 PM.png

Then you can click which application to test it on. Safari is a nice and simple one.

Now you can navigate to the iOS share icon at the bottom of Safari and you should see your Share Extension there!

Screen Shot 2016-11-19 at 2.34.14 PM.png

More on Share Extensions

That’s it for the Firebase configuration! Share Extensions are a really cool way to allow users to post content to your service and interact with the app in a different way!

If you would like to read more about Share Extensions you can look at Apple’s Documentation.