Skip to main content

Overview

We’ll use WKWebView to add Subtotal Link into your iOS application.

Create a view with a button

Add a button that to launch Subtotal Link in a WebView. The WebView handles the entire account linking process.
struct ContentView: View {
    @State private var showWebView = false
    
    var body: some View {
        NavigationStack {
            Button("Link your account") {
                showWebView = true
            }
            .navigationDestination(isPresented: $showWebView) {
                SubtotalLinkView()
            }
        }
    }
}
Create a WKWebView to display Subtotal Link. The WKWebViewConfiguration shown below is required for Subtotal Link to work properly.
import SwiftUI
import WebKit

struct SubtotalLinkView: View {
    var body: some View {
        SubtotalWebView()
    }
}

struct SubtotalWebView: UIViewRepresentable {
    func makeUIView(context: Context) -> WKWebView {
        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.allowsInlineMediaPlayback = true
        webConfiguration.applicationNameForUserAgent = "Subtotal Custom WebView User Agent"
        
        let webView = WKWebView(frame: .zero, configuration: webConfiguration)
        
        // Copy and paste a Link URL from the Subtotal Dashboard
        let linkURL = "https://link.subtotal.com/tWGE4TJM"
        if let url = URL(string: linkURL) {
            webView.load(URLRequest(url: url))
        }
        
        return webView
    }
    
    func updateUIView(_ webView: WKWebView, context: Context) {
        // Required by UIViewRepresentable protocol - URL is already loaded in makeUIView
    }
}
Note: To handle the redirect_url when users complete or exit linking, add a Coordinator with WKNavigationDelegate and implement webView(_:decidePolicyFor:decisionHandler:) to detect your URL scheme and dismiss the WebView.

That’s It!

Your iOS app now is now using Subtotal Link to link retail accounts.