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.
- Create constants for your ConsumerKey, ConsumerSecret, and CallbackUrl.
- Create a
SalesforceClient
object. -
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
- Open the MyCampaigns solution from the Exercise 1 subfolder Lab.Start.
- Verify that the Salesforce Component has been added to the Android and iOS projects.
-
Open MyCampaigns.cs. Find the property named
Platform
. Notice that its type isIAdapter
. 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. -
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. -
Open
MainActivity.cs
. Find the line inOnCreate
where the activity loads itself into the App'sPlatform
property. Note that the activity implements theIAdapter
interface and displays the passed UI in a typical way for Android, by starting an Activity. -
Open
AppDelegate.cs
. Find the line inFinishedLaunching
where the delegate loads itself into the App'sPlatform
property. Note that the delegate implements theIAdapter
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. -
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
-
Open
MyCampaigns.cs
. -
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
, andCallbackUrl
fields. -
In
OnStart
, create aSalesforceClient
object and assign it to theClient
property. - 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.