Add Mobile SDK to an Existing Project
Learning Objectives
After completing this unit, you'll be able to:
- Use CocoaPods to add Mobile SDK to an existing native iOS app.
- Use CocoaPods to update an existing Mobile SDK app for native iOS.
- Understand how Mobile SDK pods depend on each other.
Follow Along with Trail Together
Want to follow along with an expert as you work through this step? Take a look at this video, part of the Trail Together series.
(This clip starts at the 22:11 minute mark, in case you want to rewind and watch the beginning of the step again.)
Using CocoaPods with Mobile SDK
In Mobile SDK 4.0 and later, forceios uses CocoaPods to create projects. Developers can also use CocoaPods manually to add Mobile SDK to existing iOS apps.
Mobile SDK provides CocoaPods pod specifications, or podspecs, for each Mobile SDK module.
-
SalesforceSDKCore
—Implements OAuth, passcodes, networking, and REST APIs. -
SmartStore
—Implements secure offline storage. Depends onSalesforceSDKCore
. -
MobileSync
—Implements offline synchronization. Depends onSmartStore
. -
SalesforceAnalytics
—Implements a reporting mechanism that sends Salesforce anonymous statistics on Mobile SDK feature usage and popularity. -
SalesforceSDKCommon
—Utilities shared throughout the SDK.
MobileSync
, you automatically get the SmartStore
and SalesforceSDKCore
pods. This shortcut applies only to production pods.To use CocoaPods with the current Mobile SDK release, follow these steps.
- Be sure you’ve installed the cocoapods Ruby gem as described in Set Up Your iOS Development Environment in the Set Up Your Mobile SDK Developer Tools project.
- In your project's Podfile, add the SalesforceMobileSDK-iOS-Specs repo as a source. Make sure that you put this entry first, before the CocoaPods source path.
target 'YourAppName' do source 'https://github.com/forcedotcom/SalesforceMobileSDK-iOS-Specs.git' # needs to be first source 'https://github.com/CocoaPods/Specs.git' ...
- Add
use_frameworks!
to support dynamic frameworks.target 'YourAppName' do source 'https://github.com/forcedotcom/SalesforceMobileSDK-iOS-Specs.git' # needs to be first source 'https://github.com/CocoaPods/Specs.git' use_frameworks! ...
- Reference the Mobile SDK podspec that you intend to merge into your app. For example, to add OAuth and passcode modules to your app, declare the
SalesforceSDKCore
pod in your Podfile. For example:target 'YourAppName' do source 'https://github.com/forcedotcom/SalesforceMobileSDK-iOS-Specs.git' # needs to be first source 'https://github.com/CocoaPods/Specs.git' use_frameworks! pod 'SalesforceSDKCore' end
This pod configuration is the minimum for a Mobile SDK app. - To add other modules, replace
SalesforceSDKCore
with a different pod declaration. For example, to useMobileSync
:target 'YourAppName' do source 'https://github.com/forcedotcom/SalesforceMobileSDK-iOS-Specs.git' # needs to be first source 'https://github.com/CocoaPods/Specs.git' use_frameworks! pod 'MobileSync' end
Since theMobileSync
pod depends onSmartStore
andSalesforceSDKCore
, you don’t need to declare those pods explicitly. - (Alternate method) To work with the upcoming release of Mobile SDK, you clone the SalesforceMobileSDK-iOS repo, check out the dev branch, and then pull resources from it. In this case, you must declare each pre-release dependency explicitly so you can indicate its repo path. If you omit a dependency declaration, CocoaPods loads its production version.
- At the Terminal prompt, clone github.com/forcedotcom/SalesforceMobileSDK-iOS locally at the desired commit.
- Change directories to your new clone. (If you didn't specify a custom path, use
cd SalesforceMobileSDK-iOS
.) - Run
git checkout dev
to switch to the development branch. - Run
./install.sh
in the root directory of your clone. - To each pod call in your Podfile, add a
:path
parameter that points to your clone.
- Here's the previous example repurposed to pull resources from a local clone:
target 'YourAppName' do source 'https://github.com/forcedotcom/SalesforceMobileSDK-iOS-Specs.git' # need to be first source 'https://github.com/CocoaPods/Specs.git' use_frameworks! # Specify each pre-release pod pod 'SalesforceSDKCore', :path => '/<path-to-clone-of>/SalesforceMobileSDK-iOS/' pod 'SalesforceAnalytics', :path => '/<path-to-clone-of>/SalesforceMobileSDK-iOS/' pod 'SmartStore', :path => '/<path-to-clone-of>/SalesforceMobileSDK-iOS/' pod 'MobileSync', :path => '/<path-to-clone-of>/SalesforceMobileSDK-iOS/' end
- In a Terminal window, run
pod install
from your project directory. CocoaPods downloads the dependencies for your requested pods, merges them into your project, and creates a workspace containing the newly merged project.
Note: After running CocoaPods, always access your project only from the workspace thatpod install
creates. For example, instead of opening MyProject.xcodeproj, open MyProject.xcworkspace. - To use Mobile SDK APIs in Objective-C apps, import header files using angle brackets (< and >) rather than double quotes. For example:
#import <SalesforceSDKCore/SFRestAPI.h>