Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook iOS SDK: Multiple Facebook apps for different environments

We are building an app that uses the newest Facebook iOS SDK (3.15) for iOS 7 on Xcode 5. There is an associated web product, and we use 3 environments for deployment: production, staging, and development. We have a separate Facebook app for each of these environments, which is well documented and works well on web. However, Facebook's iOS SDK requires specifying FacebookAppID, FacebookDisplayName, and URL Schemes in our Info.plist file. Our mobile app is also set up with the 3 environments, using a different Configuration for each environment. I haven't been able to get the Facebook SDK to play nice with Configuration-specific values in our Info.plist file.

Can anyone help point me in the right direction for setting up multiple iOS app environments with multiple Facebook apps?

like image 229
hunteros Avatar asked Jul 09 '14 18:07

hunteros


1 Answers

I used a different strategy than Azat, which worked for iOS 7/8 and Facebook iOS SDK v3.21. Because I had multiple settings beyond just Facebook that were environment-specific, I created a generic Singleton to handle environment configurations. Here are the steps:

1. Create Configurations for App

Go to your Project Settings, Info tab, and create configurations for each different environment. I personally used Development, Staging, Release, and Debug (last 2 both for Production environment).

2. Expose Configuration Name in Info.plist

In your app's Info.plist file, create a key called Configuration and set the value to ${CONFIGURATION}.

3. Build Singleton to Handle Environment Configuration

The full code can be found in this gist, but here's the gist (no pun intended) of it. You'll create an additional property list Environments.plist with a key for each configuration whose value is a dictionary containing all of your environment-specific settings. Next you'll build an Environment class that will have a singleton instance through which you can expose those settings. Essentially you can call [[Environment sharedInstance] facebookAppId] from anywhere in your app that you've imported Environment.h, which we will do in the next step.

4. Explicitly choose Facebook appId and displayName in Your App Delegate

In your AppDelegate.m's application:didFinishLaunchingWithOptions method, the very first lines should read:

[FBSettings setDefaultAppID:[[Environment sharedInstance] facebookAppId]];
[FBSettings setDefaultDisplayName:[[Environment sharedInstance] facebookDisplayName]];

5. Add Facebook URL Schemes to Info.plist

In your Info.plist, you'll need to add the URL Schemes for each Facebook app you are using for all of your environments. See the instructions here, but instead of just 1 item under URL Schemes, you'll have 1 for each environment.

6. Choose Configuration in Scheme

To run using a certain configuration, edit your scheme (⌘+< shortcut on Mac) and choose your configuration. To make it easier to not have to edit every time I ran, I created a scheme for each of my environments with the proper configuration already chosen for Run, Test, Profile, and Archive.


This was last tested on iOS 8 and Facebook iOS SDK v3.21, but I'm hoping the general strategy here can apply to iOS 9 and later versions of the Facebook iOS SDK. Hope this helps!

like image 87
hunteros Avatar answered Oct 04 '22 12:10

hunteros