{"templateId":"markdown","sharedDataIds":{"sidebar":"sidebar-products/rest/elements/sidebars.yaml"},"props":{"metadata":{"markdoc":{"tagList":["partial","code-snippet","json-schema","login-link"]},"type":"markdown"},"seo":{"title":"Enabling Elements for Apple iOS","description":"Embed DailyPay On Demand Pay into your applications with our APIs and SDKs.","llmstxt":{"hide":false,"sections":[{"title":"Table of contents","includeFiles":["**/*"],"excludeFiles":[]}],"excludeFiles":[]}},"dynamicMarkdocComponents":["openapi"],"compilationErrors":[],"ast":{"$$mdtype":"Tag","name":"article","attributes":{},"children":[{"$$mdtype":"Tag","name":"Heading","attributes":{"level":1,"id":"enabling-elements-for-apple-ios","__idx":0},"children":["Enabling Elements for Apple iOS"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Like other platforms, iOS requires the same two or three simple steps: Add a web view, handle events/messages, and, in some cases, authenticate the user."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"1-set-up-a-web-view","__idx":1},"children":["1. Set Up a Web View"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Component used: ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://developer.apple.com/documentation/webkit/wkwebview"},"children":["WKWebView"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Note: iOS 26 introduced ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://developer.apple.com/documentation/webkit/webkit-for-swiftui"},"children":["WebKit for SwiftUI"]}," which has not yet been tested with Elements. No problems are expected, but implementation details may change."]},{"$$mdtype":"Tag","name":"Admonition","attributes":{"type":"info","name":"Required Web View Configuration"},"children":[{"$$mdtype":"Tag","name":"ul","attributes":{},"children":[{"$$mdtype":"Tag","name":"li","attributes":{},"children":["Enable JavaScript"]},{"$$mdtype":"Tag","name":"li","attributes":{},"children":["Enable Web Storage (DOM Storage)"]}]}]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":4,"id":"web-view-sample","__idx":2},"children":["Web View Sample"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"file":"../code-samples/ios/generic-example.swift","language":"swift","title":"elements-in-ios.swift","header":{"title":"elements-in-ios.swift","controls":{"copy":{}}},"lang":"swift","source":"                // Step 1: Set Up a Web View\n                WebView(\n                    url: url,\n                    onTokenReceived: { token in\n                        self.token = token\n                        self.showTokenAlert = true\n                    },\n                    onFullResponseReceived: { response in\n                        handleCardTokenizeResponse(response)\n                    }\n                )\n                .edgesIgnoringSafeArea(.all)\n                ","wrapContents":true},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"step-2-set-up-eventmessage-handling","__idx":3},"children":["Step 2. Set Up Event/Message Handling"]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"sending-events","__idx":4},"children":["Sending Events"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Component used: ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://developer.apple.com/documentation/webkit/wkusercontentcontroller"},"children":["WKUserContentController"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Note: iOS 26 introduced ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://developer.apple.com/documentation/webkit/webkit-for-swiftui"},"children":["WebKit for SwiftUI"]}," which has not yet been tested with Elements. No problems are expected, but implementation details may change."]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Please ",{"$$mdtype":"Tag","name":"MarkdocLoginLink","attributes":{},"children":[]}," to view this content."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":4,"id":"send-message-sample-code","__idx":5},"children":["Send Message Sample Code"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"file":"../code-samples/ios/generic-example.swift","language":"swift","title":"elements-in-ios.swift","header":{"title":"elements-in-ios.swift","controls":{"copy":{}}},"lang":"swift","source":"        // Step 2: Set Up Communications: Event/Message Handling\n        // Step 2a: Send Event to Elements\n        ","wrapContents":true},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":3,"id":"listening-for-events","__idx":6},"children":["Listening for Events"]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Component used: ",{"$$mdtype":"Tag","name":"MarkdownLink","attributes":{"href":"https://developer.apple.com/documentation/webkit/wkusercontentcontroller"},"children":["WKUserContentController"]}]},{"$$mdtype":"Tag","name":"p","attributes":{},"children":["Please ",{"$$mdtype":"Tag","name":"MarkdocLoginLink","attributes":{},"children":[]}," to view this content."]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":4,"id":"receive-message-sample-code","__idx":7},"children":["Receive Message Sample Code"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"file":"../code-samples/ios/generic-example.swift","language":"swift","title":"elements-in-ios.swift","header":{"title":"elements-in-ios.swift","controls":{"copy":{}}},"lang":"swift","source":"        // Step 2b: Listen for events from Elements\n        func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {\n            print(\"Received postMessage: \\(message.body)\")\n            var data: Data? = nil\n            if let dict = message.body as? [String: Any] {\n                data = try? JSONSerialization.data(withJSONObject: dict)\n            } else if let str = message.body as? String {\n                data = str.data(using: .utf8)\n            }\n            if let data, let msg = try? JSONDecoder().decode(DailypayCardTokenizeMessage.self, from: data),\n               msg.type == \"DAILYPAY_CARD_TOKENIZE_RESPONSE\" {\n                onIssuerReceived?(msg.response.issuer)\n                onTokenReceived?(msg.response.token)\n                onFullResponseReceived?(msg.response)\n            }\n        }\n        ","wrapContents":true},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"step-3-set-up-user-authentication-optional","__idx":8},"children":["Step 3. Set Up User Authentication (Optional)"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"file":"../code-samples/ios/generic-example.swift","language":"swift","title":"elements-in-ios.swift","header":{"title":"elements-in-ios.swift","controls":{"copy":{}}},"lang":"swift","source":"    // Step 3: Setup Authentication\n    //\n    // Code sample coming soon\n    //\n    ","wrapContents":true},"children":[]},{"$$mdtype":"Tag","name":"Heading","attributes":{"level":2,"id":"full-sample-code","__idx":9},"children":["Full Sample Code"]},{"$$mdtype":"Tag","name":"CodeBlock","attributes":{"file":"../code-samples/ios/generic-example.swift","language":"swift","title":"elements-in-ios.swift","header":{"title":"elements-in-ios.swift","controls":{"copy":{}}},"lang":"swift","source":"import SwiftUI\nimport WebKit\n\nstruct WebView: UIViewRepresentable {\n    let url: URL\n    let handlerName = \"iosListener\"\n    var onTokenReceived: ((String) -> Void)?\n    var onFullResponseReceived: ((DailypayCardTokenizeResponse) -> Void)?\n\n    class Coordinator: NSObject, WKScriptMessageHandler {\n        var onIssuerReceived: ((String) -> Void)?\n        var onTokenReceived: ((String) -> Void)?\n        var onFullResponseReceived: ((DailypayCardTokenizeResponse) -> Void)?\n        init(onTokenReceived: ((String) -> Void)?, onFullResponseReceived: ((DailypayCardTokenizeResponse) -> Void)?) {\n            self.onTokenReceived = onTokenReceived\n            self.onFullResponseReceived = onFullResponseReceived\n        }\n        // Step 2: Set Up Communications: Event/Message Handling\n        // Step 2a: Send Event to Elements\n        // Step 2b: Listen for events from Elements\n        func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {\n            print(\"Received postMessage: \\(message.body)\")\n            var data: Data? = nil\n            if let dict = message.body as? [String: Any] {\n                data = try? JSONSerialization.data(withJSONObject: dict)\n            } else if let str = message.body as? String {\n                data = str.data(using: .utf8)\n            }\n            if let data, let msg = try? JSONDecoder().decode(DailypayCardTokenizeMessage.self, from: data),\n               msg.type == \"DAILYPAY_CARD_TOKENIZE_RESPONSE\" {\n                onIssuerReceived?(msg.response.issuer)\n                onTokenReceived?(msg.response.token)\n                onFullResponseReceived?(msg.response)\n            }\n        }\n        // Step 2 End\n    }\n\n    // Step 3: Setup Authentication\n    //\n    // Code sample coming soon\n    //\n    // Step 3 End\n\n\n    func makeCoordinator() -> Coordinator {\n        Coordinator(onTokenReceived: onTokenReceived, onFullResponseReceived: onFullResponseReceived)\n    }\n\n    func makeUIView(context: Context) -> WKWebView {\n        let contentController = WKUserContentController()\n        contentController.add(context.coordinator, name: handlerName)\n        let config = WKWebViewConfiguration()\n        config.userContentController = contentController\n\n        // Forward window.postMessage to iOS\n        let js = \"\"\"\n        window.postMessage = function(data) {\n            window.webkit.messageHandlers.\\(handlerName).postMessage(data);\n        };\n        \"\"\"\n        let userScript = WKUserScript(source: js, injectionTime: .atDocumentStart, forMainFrameOnly: false)\n        contentController.addUserScript(userScript)\n\n        let webView = WKWebView(frame: .zero, configuration: config)\n        let request = URLRequest(url: url)\n        webView.load(request)\n        return webView\n    }\n\n    func updateUIView(_ uiView: WKWebView, context: Context) {\n        // No-op\n    }\n}\n\nstruct DailypayCardTokenizeResponse: Decodable {\n    let firstName: String\n    let lastName: String\n    let addressLineOne: String\n    let addressLineTwo: String\n    let addressCity: String\n    let addressState: String\n    let addressZipCode: String\n    let expirationMonth: String\n    let expirationYear: String\n    let addressCountry: String\n    let issuer: String\n    let token: String\n\n    enum CodingKeys: String, CodingKey {\n        case firstName = \"first_name\"\n        case lastName = \"last_name\"\n        case addressLineOne = \"address_line_one\"\n        case addressLineTwo = \"address_line_two\"\n        case addressCity = \"address_city\"\n        case addressState = \"address_state\"\n        case addressZipCode = \"address_zip_code\"\n        case expirationMonth = \"expiration_month\"\n        case expirationYear = \"expiration_year\"\n        case addressCountry = \"address_country\"\n        case issuer\n        case token\n    }\n}\n\nstruct DailypayCardTokenizeMessage: Decodable {\n    let type: String\n    let response: DailypayCardTokenizeResponse\n    let success: Bool\n}\n\nstruct ContentView: View {\n    // Replace client_id and implementation_id with values supplied by DailyPay\n    // that are unique to you and your application\n    @State private var urlString: String = \"https://elements.uat.dailypay.com/v1/debit-card-tokenization?client_id=REPLACE_WITH_YOUR_CLIENT_ID&implementation_id=REPLACE_WITH_YOUR_IMPLEMENTATION_ID\"\n    @State private var showAlert = false\n    @State private var issuer: String? = nil\n    @State private var showTokenAlert = false\n    @State private var token: String? = nil\n\n    func handleCardTokenizeResponse(_ response: DailypayCardTokenizeResponse) {\n        // Add card to DailyPay using POST /accounts\n        // See https://developer.dailypay.com/tag/Accounts#operation/createAccount\n        print(\"Ready to send to the DailyPay REST API: \\(response)\")\n    }\n\n    var body: some View {\n        VStack {\n            TextField(\"Enter URL\", text: $urlString)\n                .textFieldStyle(RoundedBorderTextFieldStyle())\n                .padding()\n            if let url = URL(string: urlString) {\n                // Step 1: Set Up a Web View\n                WebView(\n                    url: url,\n                    onTokenReceived: { token in\n                        self.token = token\n                        self.showTokenAlert = true\n                    },\n                    onFullResponseReceived: { response in\n                        handleCardTokenizeResponse(response)\n                    }\n                )\n                .edgesIgnoringSafeArea(.all)\n                // Step 1 End\n            } else {\n                Text(\"Invalid URL\")\n            }\n        }\n        .alert(isPresented: $showTokenAlert) {\n            Alert(\n                title: Text(\"Token Received\"),\n                message: Text(token ?? \"No token\"),\n                dismissButton: .default(Text(\"OK\"))\n            )\n        }\n    }\n}\n\n#Preview {\n    ContentView()\n}\n","wrapContents":true},"children":[]}]},"headings":[{"value":"Enabling Elements for Apple iOS","id":"enabling-elements-for-apple-ios","depth":1},{"value":"1. Set Up a Web View","id":"1-set-up-a-web-view","depth":2},{"value":"Web View Sample","id":"web-view-sample","depth":4},{"value":"Step 2. Set Up Event/Message Handling","id":"step-2-set-up-eventmessage-handling","depth":2},{"value":"Sending Events","id":"sending-events","depth":3},{"value":"Send Message Sample Code","id":"send-message-sample-code","depth":4},{"value":"Listening for Events","id":"listening-for-events","depth":3},{"value":"Receive Message Sample Code","id":"receive-message-sample-code","depth":4},{"value":"Step 3. Set Up User Authentication (Optional)","id":"step-3-set-up-user-authentication-optional","depth":2},{"value":"Full Sample Code","id":"full-sample-code","depth":2}],"frontmatter":{"seo":{"title":"Enabling Elements for Apple iOS"}},"lastModified":"2026-07-14T14:30:25.000Z","pagePropGetterError":{"message":"","name":""}},"slug":"/products/rest/elements/developer-guide/ios","userData":{"isAuthenticated":false,"teams":["anonymous"]},"isPublic":true}