WebViewPresenter

WebViewPresenter


Web views are required by some authentication steps, as they need to present a login form for user authentication.

This SDK supports three web view types:

  • WKWebView
  • SFSafariViewController
  • ASWebAuthenticationSession

There are presenter protocols for each supported web view type:

  • WKWebViewPresenting
  • SFSafariViewControllerPresenting
  • ASWebAuthenticationSessionPresenting

And one default implementation for each:

  • WKWebViewPresenter
  • SFSafariViewControllerPresenter
  • ASWebAuthenticationSessionPresenter

Using ASWebAuthenticationSessionPresenter

Client apps can make use of ASWebAuthenticationSession for OAuth2 authentication, in place of WKWebView or SFSafariViewController web view presenters.

Refer to Configuring OAuth2 authentication with ASWebAuthenticationSession section in the help documentation for more details.

Usage

Usage scenario 1: Pass presenter to SDK components

  1. Create an instance of one of the default presenters (or implement your own).
  2. Set it as webViewPresenter for authenticator components.

The following listing is an example of how to use this component in combination with WKWebViewPresenter which is one of the provided default implementations:

let authenticatorParams = SAMLAuthenticationParameters(authorizationEndpointURL: URL(string:<##>)!, finishEndpointURL: URL(string:<##>)!)
let authenticator = SAMLAuthenticator(using: authenticatorParams)
authenticator.webViewPresenter = WKWebViewPresenter()

The example above assumes you have an SAMLAuthenticator component that you want to use with your own presenter.

Usage scenario 2: Use presenter directly in your app

  1. Create an instance of one of the default presenters (or implement your own).
  2. Use it for your own purposes.
  3. Call dismissWebView() once you finish.
let presenter = WKWebViewPresenter()

presenter.presentWebView { webView, error in
    if let error = error {
        // error handling
        return
    }

    wkWebView.load(request: request)

    // Dismiss the web view when you don't need it anymore
    presenter.dismissWebView()
}

Notes:

You must call the present and dismiss method of the presenter on the main thread!

View hierarchy

By default, the default web view presenters present the web view on top of the top-most UIViewController. You can change this by setting the webViewPresentingViewController property (on the default presenters) to your own UIViewController. For example:

let wkWebViewPresenter = WKWebViewPresenter()
wkWebViewPresenter.webViewPresentingViewController = <presenting UIViewController of your choice>

Custom implementation

You can define your own implementation of the presenter:

  1. Implement one of the supported web view presenter protocols.
  2. Supply one of the system web views in the presentWebView’s completionHandler.

You must call the completionHandler on the main thread!

  • This protocol represents a delegate which can be called by one of the web view presenting implementation.

    See more

    Declaration

    Swift

    public protocol WebViewPresenterDelegate : AnyObject
  • This protocol represents an API which is capable of presenting a ASWebAuthenticationSession.

    See more

    Declaration

    Swift

    @available(iOS 12.0, *)
    public protocol ASWebAuthenticationSessionPresenting : AnyObject
  • This protocol represents an API which is capable of presenting a SFSafariViewController.

    See more

    Declaration

    Swift

    public protocol SFSafariViewControllerPresenting : AnyObject
  • This protocol represents an API which is capable of presenting a WKWebView.

    See more

    Declaration

    Swift

    public protocol WKWebViewPresenting : AnyObject
  • This protocol extends the WKNavigationDelegate with SAP specific functionality.

    See more

    Declaration

    Swift

    public protocol SAPWKNavigationDelegate : AnyObject