SceneDelegate Class

The SceneDelegate class handles setup and life-cycle management for scenes. In the Mobile SDK Swift template, SceneDelegate also handles Salesforce logins and sets up your app’s root view controller. Mobile SDK currently doesn’t use SceneDelegate in its Objective-C template.

OAuth resources reside in an independent module. This separation makes it possible for you to use Salesforce authentication on demand. You can start the login process from within your SceneDelegate implementation, or you can defer it until it’s actually required—for example, you can call OAuth from a scustom subview.

In the following code, you can see how the template app sets up a handler for current user change event notifications.

AuthHelper, a Mobile SDK shared object, does two things in this method. First, it registers the enclosed block to handle user change events. When this type of change occurs, Mobile SDK discards the former user’s data. In ‘AuthHelper’, the registered handler block calls resetViewState() to unwind any existing view hierarchy, then calls setupRootViewController() to restore the app’s original state. Second, it calls AuthHelper.loginIfRequired to start the login flow if the user has no valid OAuth tokens.

Both calls pass the connected scene. Always use the scene-scoped AuthHelper overloads and make these calls from scene(_:willConnectTo:options:), which runs only after the scene has connected. Passing the scene ties the login flow to the window it belongs to, so that Mobile SDK can correctly track authentication for each window.

Start the login flow from scene(_:willConnectTo:options:), and pass the connected scene to AuthHelper.loginIfRequired. Use the scene-scoped overload rather than the scene-less AuthHelper.loginIfRequired { }, and don’t initiate login before a scene has connected—for example, from AppDelegate.

Here, the template takes advantage of this execution point to set up SmartStore and Mobile Sync configurations—a one-time event per user. It then sets AccountsListView as the root view and creates a UIHostingController object based on it. This new object becomes the root view controller.

Before the call to setupRootViewController(), you can customize the look and functionality of the login page’s navigation bar. For example, create a method that customizes the public properties of the SalesforceLoginViewControllerConfig class.

The AuthHelper.loginIfRequired method takes the connected scene and a completion block that returns void and doesn’t have parameters. Use this block, as shown, to set up your root view controller. Here, the setupRootViewController method sets the rootViewController property of self.window to your app’s first custom view, thus launching your app’s custom behavior. In this template, that view is the Accounts list view.

Beginning in Mobile SDK 7.0, you let Mobile SDK decide when it’s necessary to show the login dialog. By calling AuthHelper.loginIfRequired, you nudge the SDK to consider whether the user has valid OAuth tokens. If not, Mobile SDK takes your advice and prompts the user to log in.

You can defer login to any point after the scene has connected. To defer authentication:

  1. In the Xcode editor, open the SceneDelegate class.

  2. In the scene(_:willConnectTo:options:) method, remove the AuthHelper.loginIfRequired call, but keep the code that’s inside the completion block:

  3. Call AuthHelper.loginIfRequired(scene) { } at the desired point of deferred login, passing the connected scene. To ensure that operations occur in the proper order, put the code that requires authentication in the completion block.