Skip to main content

Overview

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

Create a view with a button

This guide creates a view with a button that launches 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 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)
        
        // Replace with your actual Subtotal Link URL
        // Add ?redirect_url=yourapp://home to handle redirects
        let linkURL = "https://link.subtotal.com/YOUR_CONNECTION_TOKEN"
        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 has Subtotal Link integrated. Users can link their retailer accounts through a simple button that opens the WebView.
I