# Webviews with Web SDK 2.1.0+ on iOS source: https://developer.mastercard.com/open-finance-us/documentation/connect/integrating/sdk/webviews/ios-webviews-2-1-0/index.md Note: For Web SDK versions before 2.1.0, use the [earlier iOS WebView guide](https://developer.mastercard.com/open-finance-us/documentation/connect/integrating/sdk/webviews/ios-webviews/index.md). This page describes how to load your web app inside your iOS mobile app using a WebView via the Mastercard Data Connect Web SDK (version 2.1.0 or later). This guide covers OAuth authentication, universal links and deep links. It also explains how to return control to your app after authentication. This includes authentication through the financial institution's app or a Safari view controller (`SFSafariViewController`). The Data Connect flow follows these steps: 1. Your iOS app launches your web app, integrated with the Data Connect Web SDK, in a WebView. 2. The user selects an option or link to launch Data Connect in the WebView. Configure this option with `redirectUrl`. 3. After the user adds accounts through OAuth, control returns to your app. Your app must handle both these authentication scenarios: * If the financial institution's app is installed, authentication takes place in that app. * If the financial institution's app is not installed, OAuth login opens in a Safari view controller (`SFSafariViewController`). Note: To support app to app authentication, pass a `redirectUrl` to the Data Connect Web SDK. For configuration details, refer to [App to App Support](https://developer.mastercard.com/open-finance-us/documentation/connect/integrating/sdk/web-sdk/sdk/index.md#app-to-app-support). ## iOS Project Setup {#ios-project-setup} Configure your project in Xcode: 1. Select your project target, then open **Signing and Capabilities**. 2. Select **+Capability** , then **Associated Domains**. 3. Add your domain. 4. Under **Signing** , set your development team or leave it as **None**. Xcode creates the `YourProject.entitlements` file: ```xml com.apple.developer.associated-domains applinks:*.yourdomain.com ``` For additional setup details, refer to [iOS App to App Support](https://developer.mastercard.com/open-finance-us/documentation/connect/integrating/sdk/ios/ios-sdk/index.md#app-to-app-support). ## WebView Initialization {#webview-initialization} Create a `WKWebView` named `webView` in your view controller. Add the WebView setup to `viewDidAppear`. Set up `WKWebViewConfiguration`, `WKUserContentController`, `uiDelegate` and `navigationDelegate` as shown below. ```swift import UIKit import WebKit import SafariServices import Foundation class ViewController: UIViewController, SFSafariViewControllerDelegate { var webView: WKWebView! override func viewDidAppear(_ animated: Bool) { setupWebView() loadUrlInWebView() } // Add this function if not added already in basic XCode project installation steps. func setupWebView() { let config = WKWebViewConfiguration() let userContentController = WKUserContentController() userContentController.add(self, name: self.messageNameConnect) userContentController.add(self, name: "acmeHandler") // Configure the WKWebViewConfiguration instance with the WKUserContentController config.userContentController = userContentController self.webView = WKWebView(frame: view.bounds, configuration: configuration) self.webView.autoresizingMask = [.flexibleWidth, .flexibleHeight] self.webView.frame = self.view.frame self.webView.allowsBackForwardNavigationGestures = true self.webView.navigationDelegate = self self.webView.uiDelegate = self self.removeObserver = false self.webView.configuration.preferences.javaScriptEnabled = true self.view.addSubview(self.webView) } ``` ## Loading Your Web App In the WebView {#loading-your-web-app-in-the-webview} Call `loadUrlInWebView()` to load your web app's URL in the WebView. ```swift func loadUrlInWebView(){ if let url = URL(string: "https://yourdomain.com") { // Load domain URL where connect web sdk is already embeded let urlRequest = URLRequest(url: url) webView.load(urlRequest) } } ``` ## OAuth and App to App Authentication {#oauth-and-app-to-app-authentication} Add a `WKScriptMessageHandler` extension to receive Data Connect OAuth events. Use these events to open the financial institution's app or a Safari view controller. Add `SFSafariViewControllerDelegate` to handle the user closing the Safari view controller. ```swift extension WKWebContainerViewController: SFSafariViewControllerDelegate { public func safariViewControllerDidFinish(_ controller: SFSafariViewController) { controller.dismiss(animated: true, completion: nil) self.closeChildWebViewOrPopUp() } } extension ConnectViewController: WKUIDelegate { public func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? { // Triggered by window.open() if navigationAction.targetFrame == nil { self.loadChildWebView(url: (navigationAction.request.url)!) } return nil } public func webViewDidClose(_ webView: WKWebView) { self.handleConnectComplete(nil) } } // MARK: - Interface for receiving messages from JavaScript extension ConnectViewController: WKScriptMessageHandler { public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) { if let messageBody = message.body as? [String: Any], let data = messageBody["data"] as? [String: Any] { if let type = data["type"] as? String { if type == ConnectEvents.OPEN.rawValue{ if let url = URL(string: (data["url"] as? String)!) { self.openChildWebViewOrPopUp(url: url) } } else if type == ConnectEvents.CLOSE.rawValue{ self.closeChildWebViewOrPopUp() } } } } } ``` Add the following methods to the same view controller to open and close the Safari view controller. ```swift func openChildWebViewOrPopUp(url: URL) { OpenUniversalLink.inAnyNativeWay(url: url) { success in if !success { let sfSafariViewController:SFSafariViewController = SFSafariViewController(url:url) sfSafariViewController.delegate = self self.present(sfSafariViewController, animated: true, completion: nil) } } } func closeChildWebViewOrPopUp(){ if(self.presentedViewController != nil){ self.presentedViewController.dismiss(animated: true) } } ``` Create an `OpenUniversalLink` class with the following code. It opens the financial institution's app through a universal link when the app is installed. ```swift // Sample Open link class import Foundation import UIKit import SafariServices public class OpenUniversalLink { typealias completionHandler = (_ success:Bool) -> Void static func inAnyNativeWay(url: URL, dontPreferEmbeddedBrowser: Bool = false,completion: @escaping completionHandler) { // OPEN AS UNIVERSAL LINK IF EXISTS else : EMBEDDED or EXTERNAL if #available(iOS 10.0, *) { // Try to open with owner universal link app UIApplication.shared.open(url, options: [UIApplication.OpenExternalURLOptionsKey.universalLinksOnly : true]) { (success) in if !success { completion(false) } } } else { completion(false) } } } ```