Ionic V2 Stater App using Salesforce SDK

Ionic has released its current framework Ionic V2 which is build keeing AngularJS 2 and Typescript in mind. Ionic is one of the fastest growing mobile development framework and has become a choice of everyone nowadays.

I build apps using Ionic V1, but with the new release its time to upgrade because as per AngularJS it cannot be more faster for web because it is Typescript based. Anyways we will not go into detail about that now. Lets start setting up the basic template app.

Before we start I referred sample app on the same topic by Christophe which is here when I first started building Ionic V2 app.

Step 1 :- Create Ionic App

Make sure you have node installed on your system. Follow the steps in Getting Started in Ionic Getting Started Ionic. Once you have created the basic app, validate it by running it, go to the app folder open terminal run ionic serve if your app opens in browser and works fine then the step one is completed.

If you are new please run and check your app at each point because debugging will become difficult if something has broken at some point.

Step 2 :- Add Salesforce SDK

Go to you app folder add Salesforce Cordova Plugin

cordova plugin add https://github.com/forcedotcom/SalesforceMobileSDK-CordovaPlugin.git
// A suggestion here clone Salesforce Cordova Plugin in your system somewhere so that
// if you need to add it multiple time you don't need to clone from github
// You can directly add the plugin using path of your folder

You might get error here cordova ios or android version incompatible etc. Don’t worry about that just add version which is recommended for example :-

cordova platform add ios@4.x.x

After this create the bootconfig.json file as shown below add your connected app consumer key and redirect URL. Copy this file in YourApp -> www

{
  "remoteAccessConsumerKey": "CONSUMER_KEY",
  "oauthRedirectURI": "REDIRECT_URL",
  "oauthScopes": [
    "web",
    "api"
  ],
  "isLocal": true,
  "startPage": "index.html",
  "errorPage": "error.html",
  "shouldAuthenticate": true,
  "attemptOfflineLoad": false
}

Once this is done build the app.

ionic build ios // for android use android

Run again using xcode (open yourApp -> platform -> ios-> .xcodeproj). If you see salesforce login screen means the Salesforce Cordova plugin is working.
Wait not done yet …

Step 3 :- Add ForceJS

Please read the details about ForceJS 2 from the link above.

Now we already added the Salesforce Cordova Plugin. run the below commands in your app folder.

// This will install forcejs into you apps node_modues folder
npm install forcejs --save-dev
// This will install force server not required but your wish if need to test on browser
npm install force-server --save-dev

Once all this is done import forcejs into you app.component.ts file which will be in YourApp -> src -> app -> app.component.ts

import {OAuth, DataService} from 'forcejs';
// Inside constructor add the below code this will login to your Salesforce instance using Salesforce plugin via forcejs.
let oauth = OAuth.createInstance();
oauth.login().then(oauthResult => {
        DataService.createInstance(oauthResult);
        console.log("Logged Into Salesforce Successfully");
});

//  To get data from Salesforce add
let sfdc = DataService.getInstance();
sfdc.query().then(data => { console.log("DATA" + data) }).catch(error => console.log(error));

I will suggest run your app again. And check logs if you see the above message printed that means all the components are now in sync and connected to each other.

To get started copy carefully the src folder code from my template app Ionic Template App SF . The index.js file outside the src folder is the ForceJS2 minified version which I edited because I was getting some issues. If you also face issues in getting data from Salesforce then try replacing your forcejs file from this one. It will be in YourApp -> node_modules -> forcejs -> all – > index.js. Make sure you take backup of the original file first.

Some screen shots from the app

23 thoughts on “Ionic V2 Stater App using Salesforce SDK

  1. Can you please help me?
    I have followed this article and I am working with ionic-V4. I have run my app on browser and ios simulator. I have an issue with the authentication part. I am able to log in and after popup URL I can see the access token and other details like instance URL, But when the popup comes to callback URL it does not close automatically and also callback function is not getting called as I am not able to use authentication result. Also in the browser, though I provided the cosumer key for my application created in the connected app, still application asking for force js sample app permissions instead of application I created in the connected app.

    Like

  2. Hi Vivek,

    Can you please help me?
    I have created an app with Ionic 2 and Angular 2. Salesforce Mobile SDK is also being used. I don’t want to login on the app start. I want to login on a later point in the app on a different page.
    In the bootconfig.json file, I changed the shouldAuthenticate to false. I am also calling the oauth login when I need to login. But the problem is I am getting the “never authenticated” error when I try to login and the login does not open up.

    My login code:

    let oauth = OAuth.createInstance(appId, loginURL, oauthCallbackURL);
    oauth.login().then((oauthData) => {
    DataService.createInstance(oauthData, { proxyURL: “https://dev-cors-proxy.herokuapp.com/” });
    });

    The whole error log is below:

    TIMING com.salesforce.oauth:getAuthCredentials: 141.196ms com.salesforce.util.exec.js:43 TIMING com.salesforce.oauth:getAuthCredentials failed(anonymous function) @ com.salesforce.util.exec.js:43callbackFromNative @ cordova.js:293(anonymous function) @ VM330:1 vendor.js:36880 Never authenticated vendor.js:54799 EXCEPTION: Uncaught (in promise): Never authenticatedErrorHandler.handleError @ vendor.js:54799IonicErrorHandler.handleError @ vendor.js:135311next @ vendor.js:38484schedulerFn @ vendor.js:39367SafeSubscriber.__tryOrUnsub @ vendor.js:1330SafeSubscriber.next @ vendor.js:1279Subscriber._next @ vendor.js:1232Subscriber.next @ vendor.js:1196Subject.next @ vendor.js:5794EventEmitter.emit @ vendor.js:39353NgZone.triggerError @ vendor.js:27544onHandleError @ vendor.js:27505t.handleError @ polyfills.js:3e.runGuarded @ polyfills.js:3r @ polyfills.js:3i @ polyfills.js:3 vendor.js:54804 ORIGINAL STACKTRACE:ErrorHandler.handleError @ vendor.js:54804IonicErrorHandler.handleError @ vendor.js:135311next @ vendor.js:38484schedulerFn @ vendor.js:39367SafeSubscriber.__tryOrUnsub @ vendor.js:1330SafeSubscriber.next @ vendor.js:1279Subscriber._next @ vendor.js:1232Subscriber.next @ vendor.js:1196Subject.next @ vendor.js:5794EventEmitter.emit @ vendor.js:39353NgZone.triggerError @ vendor.js:27544onHandleError @ vendor.js:27505t.handleError @ polyfills.js:3e.runGuarded @ polyfills.js:3r @ polyfills.js:3i @ polyfills.js:3 vendor.js:54805 Error: Uncaught (in promise): Never authenticated at s (polyfills.js:3) at s (polyfills.js:3) at polyfills.js:3 at t.invokeTask (polyfills.js:3) at Object.onInvokeTask (vendor.js:27475) at t.invokeTask (polyfills.js:3) at e.runTask (polyfills.js:3) at i (polyfills.js:3)ErrorHandler.handleError @ vendor.js:54805IonicErrorHandler.handleError @ vendor.js:135311next @ vendor.js:38484schedulerFn @ vendor.js:39367SafeSubscriber.__tryOrUnsub @ vendor.js:1330SafeSubscriber.next @ vendor.js:1279Subscriber._next @ vendor.js:1232Subscriber.next @ vendor.js:1196Subject.next @ vendor.js:5794EventEmitter.emit @ vendor.js:39353NgZone.triggerError @ vendor.js:27544onHandleError @ vendor.js:27505t.handleError @ polyfills.js:3e.runGuarded @ polyfills.js:3r @ polyfills.js:3i @ polyfills.js:3

    Please help me out with this.

    Like

  3. Hi Vivek, I have an issue in authentication part. After authentication, my callback is not being called. I have done it as following:

    let appId = ‘value of Consumer Key’;
    let loginURL = ‘https://login.salesforce.com/’;
    let oauthCallbackURL = ‘http://localhost:8100/callback’;

    let oauth = OAuth.createInstance(appId, loginURL, oauthCallbackURL);
    console.log(‘=============oauth: ‘, oauth)

    oauth.login().then(oauthResult => {
    //this part is not called after authentication
    DataService.createInstance(oauthResult);
    console.log(“Logged Into Salesforce Successfully”);
    });

    In pop-up URL i can see the access token but not able to use that because it is not going to callback part. Please suggest where i am missing. I am able to login through pop-up, but pop-up is not closing after successful/unsuccessful authentication.

    Like

  4. . I was able to get your excellent article working with Ionic 3 but encountered errors when adding both iOS and Android platform onto the same project using (eg ionic cordova platform add ios … then ionic cordova platform add android).

    Like

  5. In your post, you mentioned that made some updates to the index.js file for the forcejs library. Can you tell us why? I do notice that sometimes the forcejs library will work as is and other times I have to use your modified file. Thanks

    Like

    1. Yes, In the index.js files there was some issue with the way the JSON data being sent to SF. That is the reason I am using the modified version.

      Like

  6. I was able to get your excellent article working with Ionic 3 but encountered errors when adding both iOS and Android platform onto the same project using (eg ionic cordova platform add ios … then ionic cordova platform add android).
    Error Message:
    There was a conflict trying to modify attributes with in plugin com.

    Like

    1. Try adding compatible version of cordova ios, the easiest way to check that is check the config.xml in salesforce cordova plugin.. Currently I guess it is 4.3.0 so.. ionic cordova platform add ios@4.3.0 will work

      Like

  7. I was able to get your excellent article working with Ionic 3 but encountered errors when adding both iOS and Android platform onto the same project using (eg ionic cordova platform add ios … then ionic cordova platform add android). Have you encountered this?

    Error Message:
    There was a conflict trying to modify attributes with in plugin com.salesforce. The conflicting plugin, undefined, already modified the same attributes. The conflict must be resolved before com.salesforce can be added. You may use –force to add the plugin and overwrite the conflicting attributes.

    Like

    1. I got it working for Ionic V3, the only change I did is to downgrade cordova version to 6.5.0. When you install ionic 3 it install cordova v7.0+ which does not allow Salesforce Cordova plugin to be added because of some specification upgrade.

      Like

  8. Great article. Do you think you are going to do one for Ionic 3? Since the Salesforce mobile sdk was changed to forceios and forcedroid in version 5.x, the steps to get the sdk to work with Ionic 3 is no longer valid.

    Like

    1. I got it working for Ionic V3, the only change I did is to downgrade cordova version to 6.5.0. When you install ionic 3 it install cordova v7.0+ which does not allow Salesforce Cordova plugin to be added because of some specification upgrade.

      Like

      1. Have you had any luck with installing Android? iOS works fine but Android install always give errors. “There was a conflict trying to modify attributes with in plugin com.salesforce. The conflicting plugin, undefined, already modified the same attributes”

        Like

Leave a comment