One of the most common questions I get asked is how do you add the Salesforce Mobile SDK for iOS to an existing project? Admitedly, it was pretty tricky with earlier versions of the Mobile SDK, but Mobile SDK 3.x makes it super easy. This tutorial will show you how to do it. The examples will be in Swift v2, but will work exactly the same for Objective-C, and earlier versions of Swift, minus the obvious syntax difference (and the addition of a briding header for Swift apps...but we will get to that shortly)
If you don't already have the Salesforce Mobile SDK for iOS installed, go ahead and clone the Salesfore Mobile SDK for iOS from GitHub. Once you have the SDK on your local machine, do the following in a Terminal window
cd [path-where-you-cloned-the-repo]/SalesforceMobileSDK-iOS ./install.sh
Once complete, you have the entire SDK, and dependencies. Check!
The Mobile SDK supports CocoaPods for adding the required libraries to your project. From the command line, change directories to your project root.
pod 'SalesforceMobileSDK-iOS'Then run:
$ pod install
$ pod echo "pod 'SalesforceMobileSDK-iOS'" > Podfile $ pod install
Once completed, your Xcode project will have created a workspace file, and have the Mobile SDK libraries ready to go. From now on, use the *.xcworkspace file, and not the *.xcodeproj file.
Now that you have your bridging header file created, add the following code:
#import <SalesforceCommonUtils/SFLogger.h> #import <SalesforceSDKCore/SFUserAccountManager.h> #import <SalesforceSDKCore/SalesforceSDKManager.h> #import <SalesforceSDKCore/SFAuthenticationManager.h> #import <SalesforceSDKCore/SFPushNotificationManager.h> #import <SalesforceSDKCore/SFDefaultUserManagementViewController.h> #import <SalesforceRestAPI/SFRestAPI.h> #import <SalesforceRestAPI/SFRestAPISalesforceAction.h> #import <SalesforceRestAPI/SFRestAPI+Blocks.h> #import <SalesforceRestAPI/SFRestAPI+Files.h> #import <SalesforceOAuth/SFOAuthInfo.h>
let RemoteAccessConsumerKey = "your-connected-app-consumer-key"; let OAuthRedirectURI = "your-connected-app-redirect-uri"; let scopes = ["api"];Then add the following code into your init method:
SFLogger.setLogLevel(SFLogLevelDebug) SalesforceSDKManager.sharedManager().connectedAppId = RemoteAccessConsumerKey SalesforceSDKManager.sharedManager().connectedAppCallbackUri = OAuthRedirectURI SalesforceSDKManager.sharedManager().authScopes = scopes SalesforceSDKManager.sharedManager().postLaunchAction = { [unowned self] (launchActionList: SFSDKLaunchAction) in let launchActionString = SalesforceSDKManager.launchActionsStringRepresentation(launchActionList) self.log(SFLogLevelInfo, msg:"Post-launch: launch actions taken: \(launchActionString)"); } SalesforceSDKManager.sharedManager().launchErrorAction = { [unowned self] (error: NSError?, launchActionList: SFSDKLaunchAction) in if let actualError = error { self.log(SFLogLevelError, msg:"Error during SDK launch: \(actualError.localizedDescription)") } else { self.log(SFLogLevelError, msg:"Unknown error during SDK launch.") } } SalesforceSDKManager.sharedManager().postLogoutAction = { [unowned self] in self.handleSdkManagerLogout() } SalesforceSDKManager.sharedManager().switchUserAction = { [unowned self] (fromUser: SFUserAccount?, toUser: SFUserAccount?) -> () in self.handleUserSwitch(fromUser, toUser: toUser) } func handleSdkManagerLogout() { //todo: add segues from each page to logout to send back to login view self.performSegueWithIdentifier("logout", sender: nil) } func handleUserSwitch(fromUser: SFUserAccount?, toUser: SFUserAccount?) { //todo }
SalesforceSDKManager.sharedManager().launch()Then, to handle to end of the authentication flow, specify that your View Controller is a delegate for the MobileSDK:
class MyViewController: UIViewController, SFAuthenticationManagerDelegateand implement the Delegate's authManagerDidFinish function. I typically check if the username has been populated correctly. If it has, only then do I perform the segue to my HomeController.
SFAuthenticationManager.sharedManager().addDelegate(self)
func authManagerDidFinish(manager: SFAuthenticationManager!, info: SFOAuthInfo!) { if !!SFAuthenticationManager.sharedManager().haveValidSession { //call the segue with the name you give it in the storyboard editor self.performSegueWithIdentifier("loggedIn", sender: nil) //or simply dismiss the login view to return to where you came from // self.dismissViewControllerAnimated(true, completion: {}) } }