www.robkerr.com
www.robkerr.com

mobile development, cloud computing and building great software

Rob Kerr
Author

Addicted to coding since writing my first programs for the Commodore computers in the 1980s. Currently working as an independent contractor focused on native iOS development.

Share


Tags


Twitter


www.robkerr.com

Connecting a Native Swift iOS App to Salesforce.com Data

Rob KerrRob Kerr

Enterprise mobile apps most often need to integrate with broader platforms. This quickstart demonstrates how easy it is to integrate an iOS mobile app with the Salesforce.com platform.

Install Node.js and npm

If you haven't installed node on your system, you'll need to do this first. There are several methods for installing node. You can download node directly from the node.js web site, or use the Homebrew package manager to complete the installation for you.

After installing Node and npm, check that the software is installed before proceeding. An easy way to check that you'll be able to use these tools is to check versions at the command line.

node --version
npm -- version

Install forceios

The Salesforce SDK can automatically configure a new XCode project with development frameworks and correct settings. The SDK uses the Node npm command to create a new project. Npm is used to install the forceios package. Use the following command to install forceios on your development workstation globally:

sudo npm install forceios -g

Use Forceios to create your application

From the OSX command line.

forceios create

Respond to the various forceios commands to configure the XCode project correctly. Most of the responses to the project configuration will be obvious to iOS developers. The one that's specific to the salesforce org is the "Connected App Callback URI". This is a string you set within the Salesforce administration site when creating the Connected App this native iOS application will connect to.

The following is an example interactive session output from the foriceios configuration application.

Rob@robimac:~/Projects/blog/SalesForceSDKiOSExample$ forceios create
Enter your application type (native, native_swift, react_native, hybrid_remote,  or hybrid_local): native_swift
Enter your application name: MTRolodex
Enter the output directory for your app (defaults to the current directory): 
Enter the package name for your app (com.mycompany.my_app): com.mobiletoolworks.mtrolodex
Enter your organization name (Acme, Inc.): Mobile Toolworks, LLC
Enter your Connected App ID (defaults to the sample app's ID): 
Enter your Connected App Callback URI (defaults to the sample app's URI): sfdc://oauth-callback/salesforce
Creating app in /Users/Rob/Projects/blog/SalesForceSDKiOSExample/MTRolodex

Getting cocoapods dependencies

Updating local specs repositories


CocoaPods 1.0.0.beta.8 is available.
To update use: `gem install cocoapods --pre`
[!] This is a test version we'd love you to try.

For more information see http://blog.cocoapods.org
and the CHANGELOG for this version http://git.io/BaH8pQ.

Analyzing dependencies

Downloading dependencies
Installing CocoaLumberjack (2.2.0)
Installing SQLCipher (3.3.1)
Installing SalesforceNetwork (4.1.1)
Installing SalesforceRestAPI (4.1.1)
Installing SalesforceSDKCore (4.1.1)
Installing SmartStore (4.1.1)
Installing SmartSync (4.1.1)
Generating Pods project
Integrating client project

[!] Please close any current Xcode sessions and use `MTRolodex.xcworkspace` for this project from now on.

Sending stats

Sending stats
Pod installation complete! There are 5 dependencies from the Podfile and 7 total pods installed.

Successfully created native_swift app 'MTRolodex'.

Update the Consumer Key

After launching the XCode workspace -- in this example "MTRolodex.workspace", open the AppDelegate.swift file, and update the RemoteAccessConsumerKey and OAuthRedirectURI to match the values in your Salesforce.com connected app.

import Foundation
import UIKit
import SalesforceSDKCore

let RemoteAccessConsumerKey = "3MVG98SW_UPr.GCjfe8JFg6qFEHxBqp3EzFBBoCZsnc2zuq5rgVVPJUbBh3T6zLyp1k13Tj_csFpIobDVSQDf";
let OAuthRedirectURI        = "sfdc://oauth-callback/salesforce";

class AppDelegate : UIResponder, UIApplicationDelegate
.
.

These values identify the iOS native application and enable users to use the native app to access Salesforce resources.

Update the app to retrieve the account list

The default boilerplate app generated by forceios lists the users of the Salesforce org. Let's make a slight change to instead list the Accounts store in the org. To do this, change the loadView() method within the RootViewController.swift class file as follows:

override func loadView()
{
    super.loadView()
    self.title = "Mobile SDK Sample App"

    let soql = "SELECT Id, Name, BillingCity FROM Account"
    let request = SFRestAPI.sharedInstance().requestForQuery(soql);
    SFRestAPI.sharedInstance().send(request, delegate: self);
}

Build and run

Now build and run the application. After the app launches, the first thing you should see is a Salesforce login form via OAuth.

Salesforce.com iOS Login Form

As part of the login process, you probably will receive a 2-factor login verification as you would using Salesforce in the web browser. Complete the 2-factor login as usual.

Salesforce.com 2-factor login form

Finally, the Salesforce OAuth process will ask the user to permit the custom application to access Salesforce objects and functions on behalf of the user. These permissions are configured when creating the Connected App in the Salesforce Admin console.

Salesforce.com OAuth client authorization

Now that all that is done, the magic should happen! Our native Swift iOS app will send a SOQL query to the Salesforce back-end, and retrieve the account list, displaying it in a table view.

Salesforce.com Sample Account List iOS

If your account list displays, then congratulations! You've managed to create a native application that accesses Salesforce data.

Download the source code for the iOS app built for this article from my GitHub repository.

Rob Kerr
Author

Rob Kerr

Addicted to coding since writing my first programs for the Commodore computers in the 1980s. Currently working as an independent contractor focused on native iOS development.

Comments