Using the Xamarin Salesforce Component

Duration

20 minutes

Goals

The goal of this lab is to instantiate a SalesforceClient and use it to display the Salesforce login UI.

Required assets

The provided Resources folder for this exercise contains a subfolder named Start that contains the required starter project you will use here. There is also a Completed folder with a solution you can use to check your work.

Exercise overview

You will create a SalesforceClient object and display the login UI. To let you spend your time on Salesforce-related code rather than Xamarin.Forms UI, the provided starter solution contains the majority of the platform-specific code needed to display the login UI on both Android and iOS.

  1. Create constants for your ConsumerKey, ConsumerSecret, and CallbackUrl.
  2. Create a SalesforceClient object.
  3. Obtain the login UI from the SalesforceClient and pass it to the pre-written, platform-specific code for display.

Steps

Below are the step-by-step instructions to implement the exercise.

Explore the starter code

  1. Open the MyCampaigns solution from the Exercise 1 subfolder Lab.Start.
  2. Verify that the Salesforce Component has been added to the Android and iOS projects.
  3. Open MyCampaigns.cs. Find the property named Platform. Notice that its type is IAdapter. This will be set from the platform-specific code. When run on Android, it will be your main activity. When run on iOS, it will be your application delegate. This is what you will use to diplay the login UI.
  4. Open IAdapter.cs. Notice that there are two methods, one to display some UI and one to clean up when you are finished with the UI.
  5. Open MainActivity.cs. Find the line in OnCreate where the activity loads itself into the App's Platform property. Note that the activity implements the IAdapter interface and displays the passed UI in a typical way for Android, by starting an Activity.
  6. Open AppDelegate.cs. Find the line in FinishedLaunching where the delegate loads itself into the App's Platform property. Note that the delegate implements the IAdapter interface. The code to display the passed UI is more complex than normal for iOS because the app's main window is inaccessible; therefore, we had to create a new window to display the login UI. This window is only temporary and will be disposed when the login UI is no longer needed.
  7. Open MyCampaigns.cs. Take a moment to examine the control flow through the OnStart method. In particular, notice the logic that handles the two cases of whether a user is already authenticated or not. If they are not authenticated, we display the login UI, then show our data page after they finish logging in. If they are already authenticated, we go to our data page immediately.

Complete the implementation

  1. Open MyCampaigns.cs.
  2. Retrieve your Connected App's ConsumerKey, ConsumerSecret, and CallbackUrl from your Salesforce account (the Salesforce login page is here). Add your Connected App's values to the ConsumerKey, ConsumerSecret, and CallbackUrl fields.
  3. In OnStart, create a SalesforceClient object and assign it to the Client property.
  4. Run your app on at least one platform to test your work. The first time you run, you should see the Salesforce login UI displayed. If you run the app again, you should bypass the login screen and go straight to the data page (which will be empty at this point).

Summary

This exercise dealt with all the platform-specific details you needed to handle to display your Salesforce login UI on multiple platforms. You now have a SalesforceClient object with an authenticated user so you are ready to start querying Salesforce data.

Go Back