Skip to content

Commit

Permalink
setting up SDK_TARGET_VERSION
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiomartins8 committed Mar 8, 2024
1 parent 985b447 commit dc19a74
Show file tree
Hide file tree
Showing 14 changed files with 31 additions and 76 deletions.
Binary file removed .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
Env.xcconfig
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# WebViewRN
A sample iOS navtive app integration with Onfido’s [Web SDK](https://documentation.onfido.com/sdk/web/), using [WKWebView] (https://developer.apple.com/documentation/webkit/wkwebview) component.
# WebViewiOS

A sample iOS navtive app integration with Onfido’s [Web SDK](https://documentation.onfido.com/sdk/web/), using [WKWebView](https://developer.apple.com/documentation/webkit/wkwebview) component.

## Summary

This app is a simple demonstration of the minimum configurations that are required to integrate with [onfido-sdk-ui](https://documentation.onfido.com/sdk/web/) using a iOS native WKWebView component. The example uses [Smart Capture Link](https://developers.onfido.com/guide/smart-capture-link).

You can find more detailed documentation here:

- [WKWebView](https://docs.usercentrics.com/cmp_in_app_sdk/latest/features/webview-continuity/)

- [onfido-sdk-ui](https://documentation.onfido.com/sdk/web/)

- [Smart Capture Link](https://developers.onfido.com/guide/smart-capture-link)



## Permissions

### iOS

You will need to enable Camera, Mic, Photo Library and Location Access in your `info.plist` file:
You will need to enable Camera, Mic, Photo Library and Location Access in your `info.plist` file:

```
<key>NSCameraUsageDescription</key>
Expand All @@ -29,6 +29,4 @@ You will need to enable Camera, Mic, Photo Library and Location Access in your `
<string>Mic Access</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Photo Library Access</string>
```


```
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,6 @@
ReferencedContainer = "container:SmartCaptureDemo.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<EnvironmentVariables>
<EnvironmentVariable
key = "API_KEY"
value = "api_live.7ISao_IrDvX.RRXBTJ651LY1w4Nb7WED1wUOEKhjjFWP"
isEnabled = "YES">
</EnvironmentVariable>
<EnvironmentVariable
key = "WORKFLOW_ID"
value = "9cdf2a2c-57dd-47db-a53e-b33988c540b8"
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
Expand Down
Binary file removed SmartCaptureDemo/.DS_Store
Binary file not shown.
10 changes: 4 additions & 6 deletions SmartCaptureDemo/EnvironmentVars.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ enum EnvironmentVars {
return id
}()

static let sdkTargetVersion: String = {
guard let version = EnvironmentVars.infoDict["SDK_TARGET_VERSION"] as? String else {
fatalError("SDK version id not found")
}
return version
}()
static var sdkTargetVersion: String {
let version = (EnvironmentVars.infoDict["SDK_TARGET_VERSION"] as? String) ?? ""
return version.isEmpty ? "latest" : version
}
}
6 changes: 4 additions & 2 deletions SmartCaptureDemo/Extension/WebView+Ext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
import WebKit

extension WKWebView {
func load(_ htmlFileName: String?, version: WebSDKVersion, onError: (ApiManagerError) -> Void) {
func load(_ htmlFileName: String?, sdkTargetVersion: String, onError: (ApiManagerError) -> Void) {

guard let filePath = Bundle.main.path(forResource: htmlFileName, ofType: "html") else {
return onError(.invalidFilePath)
}

do {
let htmlString = try String(contentsOfFile: filePath, encoding: .utf8)
loadHTMLString(htmlString, baseURL: URL(fileURLWithPath: filePath))
.replacingOccurrences(of: "$SDK_TARGET_VERSION", with: sdkTargetVersion)

loadHTMLString(htmlString, baseURL: URL(fileURLWithPath: filePath))
} catch let error {
onError(.contentConversion(error.localizedDescription))
}
Expand Down
2 changes: 1 addition & 1 deletion SmartCaptureDemo/MainVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class MainVC: UIViewController {
// MARK: - Actions

@objc func loadWebView() {
navigationController?.pushViewController(WebViewViewController(webSDKVersion: .cdn), animated: true)
navigationController?.pushViewController(WebViewViewController(sdkTargetVersion: EnvironmentVars.sdkTargetVersion), animated: true)
}
}

5 changes: 1 addition & 4 deletions SmartCaptureDemo/Network/ApiEndpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ enum ApiEndpoint {
case applicantApi
case sdkTokenApi(applicantID: String)
case workFlowRunApi(applicantID: String)
case sclWebView


var path: String {
switch self {
case .applicantApi:
Expand All @@ -21,8 +20,6 @@ enum ApiEndpoint {
return "v3.3/sdk_token"
case .workFlowRunApi:
return "v3.6/workflow_runs"
case .sclWebView:
return "https://crowd-testing.eu.onfido.app/f/755350ab-4ed2-4e4f-8834-d57c98513658/"
}
}
}
2 changes: 0 additions & 2 deletions SmartCaptureDemo/Network/ApiManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ final class ApiManager {
"applicant_id": "\(id)",
"workflow_id": "\(workflowId)"
]
case .sclWebView:
break
}

let urlString = "\(baseURL)\(endpoint.path)"
Expand Down
5 changes: 0 additions & 5 deletions SmartCaptureDemo/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@

import Foundation

enum WebSDKVersion {
case scl
case cdn
}

struct ApplicantResponse: Decodable {
let id: String
}
Expand Down
46 changes: 12 additions & 34 deletions SmartCaptureDemo/WebViewViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ final class WebViewViewController: UIViewController {

// MARK: - Properties

private let webSDKVersion: WebSDKVersion
private let sdkTargetVersion: String
private var webView: WKWebView?

// MARK: - Initialization

init(webSDKVersion: WebSDKVersion) {
self.webSDKVersion = webSDKVersion
init(sdkTargetVersion: String) {
self.sdkTargetVersion = sdkTargetVersion
super.init(nibName: nil, bundle: nil)

Task {
Expand All @@ -31,9 +31,11 @@ final class WebViewViewController: UIViewController {
let workflowRunResponse: WorkFlowRunResponse = try await ApiManager.shared.getData(
from: .workFlowRunApi(applicantID: applicantResponse.id)
)


print("ℹ️ Integrating with Web SDK version: \(sdkTargetVersion)")

let config = setupWebConfiguration(token: sdkTokenResponse.token, workflowRunId: workflowRunResponse.id)
setupWebView(config: config)
setupWebView(config: config, sdkTargetVersion: sdkTargetVersion)
} catch {
print("⚠️: \(error)")
}
Expand All @@ -50,38 +52,14 @@ final class WebViewViewController: UIViewController {
}

// MARK: - Private Methods

@MainActor private func setupWebConfiguration(token: String?, workflowRunId: String?) -> WKWebViewConfiguration {
/// Load script
var scriptPath: String?
var scriptSource: String?
var scriptTag: String?

guard let token, let workflowRunId else { return WKWebViewConfiguration() }

// htmlString = "<head><script src='https://sdk.onfido.com/v14'></script></head>"

// //let metaTag = "<meta name=\"viewport\" content=\"user-scalable=no, width=device-width\">"
// let scriptTag = "<script src=\"https://sdk.onfido.com/v14\" charset=\"utf-8\" ></script>"
// let html = "\(scriptTag)"

// // Load HTML string to web view in main thread
// webView.loadHTMLString(html, baseURL: nil)

scriptTag = """
var script = document.createElement('script');
script.src = 'https://sdk.onfido.com/v14';
script.type = 'text/javascript';
script.charset = 'utf-8';
document.getElementsByTagName('head')[0].appendChild(script);
"""

guard let scriptTag else { return WKWebViewConfiguration() }


let contentController = WKUserContentController()
let userScript = WKUserScript(source: scriptTag , injectionTime: .atDocumentStart, forMainFrameOnly: true)
contentController.addUserScript(userScript)


scriptSource = """
Onfido.init({
token: '\(token)',
Expand All @@ -107,7 +85,7 @@ final class WebViewViewController: UIViewController {
return webConfiguration
}

private func setupWebView(config: WKWebViewConfiguration) {
private func setupWebView(config: WKWebViewConfiguration, sdkTargetVersion: String) {
let webView = WKWebView(frame: CGRect.zero, configuration: config)
webView.navigationDelegate = self

Expand All @@ -120,8 +98,8 @@ final class WebViewViewController: UIViewController {
webView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
webView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -12)
])

webView.load("index", version: .cdn) { err in
webView.load("index", sdkTargetVersion: sdkTargetVersion) { err in
print("🚨🚨🚨: \(err)")
}

Expand Down
4 changes: 2 additions & 2 deletions SmartCaptureDemo/index.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<!DOCTYPE html>
<html lang="en-us">
<head>
<script src="https://sdk.onfido.com/v14" charset="utf-8"></script>
<script src="https://sdk.onfido.com/capture/core/$SDK_TARGET_VERSION/Onfido.iife.js" charset="utf-8"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Hello Onfido</title>
</head>
<body>
<div id="onfido-mount"></div>
</body>
</html>
</html>

0 comments on commit dc19a74

Please sign in to comment.