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.
Initialization
1func scene(_ scene: UIScene, willConnectTo session: UISceneSession,
2 options connectionOptions: UIScene.ConnectionOptions) {
3
4 guard let windowScene = (scene as? UIWindowScene) else { return }
5 self.window = UIWindow(frame: windowScene.coordinateSpace.bounds)
6 self.window?.windowScene = windowScene
7
8 AuthHelper.registerBlock(forCurrentUserChangeNotifications: {
9 self.resetViewState {
10 self.setupRootViewController()
11 }
12 })
13}1func setupRootViewController() {
2 // Setup store based on config userstore.json
3 MobileSyncSDKManager.shared.setupUserStoreFromDefaultConfig()
4 // Setup syncs based on config usersyncs.json
5 MobileSyncSDKManager.shared.setupUserSyncsFromDefaultConfig()
6
7 self.window?.rootViewController = UIHostingController(
8 rootView: AccountsListView()
9 )
10}1func sceneWillEnterForeground(_ scene: UIScene) {
2 // Called as the scene transitions from the background to the foreground.
3 // Use this method to undo the changes made on entering the background.
4 self.initializeAppViewState()
5 AuthHelper.loginIfRequired {
6 self.setupRootViewController()
7 }
8}1func customizeLoginView() {
2 let loginViewConfig = SalesforceLoginViewControllerConfig()
3
4 // Set showSettingsIcon to false if you want to hide the settings
5 // icon on the nav bar
6 loginViewConfig.showsSettingsIcon = false
7
8 // Set showNavBar to false if you want to hide the top bar
9 loginViewConfig.showsNavigationBar = true
10 loginViewConfig.navigationBarColor = UIColor(red: 0.051, green: 0.765, blue: 0.733, alpha: 1.0)
11 loginViewConfig.navigationTitleColor = UIColor.white
12 loginViewConfig.navigationBarFont = UIFont(name: "Helvetica", size: 16.0)
13 UserAccountManager.shared.loginViewControllerConfig = loginViewConfig
14}The loginIfRequired method takes a block argument 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.
About Deferred Login
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’re not required to call this method in your SceneDelegate implementation. You can defer login to any point after app initialization is completed. To defer authentication:
- In the Xcode editor, open the SceneDelegate class.
- In sceneWillEnterForeground(_:) method,
remove the loginIfRequired call, but
keep the code that’s inside the block:
1//AuthHelper.loginIfRequired { 2 self.setupRootViewController() // Keep this call in sceneWillEnterForeground(_:) 3//} - Call AuthHelper.loginIfRequired { } at the desired point of deferred login. To ensure that operations occur in the proper order, put the code that requires authentication in the closure parameter of loginIfRequired:.