Integration with Ionic

v3.0

The SDK can be integrated into Ionic projects.

Start the SDK

The automatic interceptor is not supported in Ionic, so any request from the JavaScript code has to be handled manually. Here is an example:

1do {
2 let policy = PXPolicy()
3 policy.urlRequestInterceptionType = .none
4 try PerimeterX.start(appId: "<APP_ID>", delegate: self, policy: policy)
5}
6catch {
7 print("error: \(error)")
8}

Setup a plugin

Configure the plugin in JavaScript. Here is an example:

1export interface PerimeterxPlugin {
2 getHttpHeaders(): Promise<{ value: string }>;
3 handleResponse():Promise<{ value: string }>;
4}
5
6const PerimeterX = registerPlugin<PerimeterxPlugin>('PerimeterX');

Setup a plugin in order to handle calls from the JavaScript code. Here is an example:

HUMANManager.swift:

1import Foundation
2import Capacitor
3import PerimeterX_SDK
4
5@objc(PerimeterxManagerPlugin)
6class PerimeterxManagerPlugin: CAPPlugin {
7
8 static let shared = PerimeterxManagerPlugin()
9
10 func start() {
11 do {
12 let policy = PXPolicy()
13 policy.urlRequestInterceptionType = .none
14 policy.doctorCheckEnabled = false
15 try PerimeterX.start(appId: "PXj9y4Q8Em", delegate: nil, policy: policy)
16 }
17 catch {
18 print("error: \(error)")
19 }
20 }
21
22 @objc func getHttpHeaders(_ call: CAPPluginCall) {
23 call.resolve(PerimeterX.headersForURLRequest()!)
24 }
25
26 @objc func handleResponse(_ call: CAPPluginCall) {
27 let response = call.getString("value") ?? ""
28 let data = response.data(using: .utf8)
29 let httpURLResponse = HTTPURLResponse(url: URL(string: "https://fake.url.com")!, statusCode: 403, httpVersion: nil, headerFields: nil)
30 let handled = PerimeterX.handleResponse(response: httpURLResponse!, data: data!) { challengeResult in
31 call.resolve(["value": challengeResult == .solved ? "solved" : "cancelled"])
32 }
33 if !handled {
34 call.resolve(["value": "false"])
35 }
36 }
37}

HUMANManagerPlugin.m:

1#import <Foundation/Foundation.h>
2#import <Capacitor/Capacitor.h>
3
4CAP_PLUGIN(PerimeterxManagerPlugin, "PerimeterX",
5 CAP_PLUGIN_METHOD(getHttpHeaders, CAPPluginReturnPromise);
6 CAP_PLUGIN_METHOD(handleResponse, CAPPluginReturnPromise);
7)

Pass the SDK’s HTTP headers to the JavaScript code and handle the response

In your JavaScript code, call those functions for every URL request to add those HTTP headers and handle the response. Here is an example:

1const sendUrlRequest = async () => {
2 const result = await PerimeterX.getHttpHeaders()
3 const json = JSON.parse(JSON.stringify(result))
4 const map = new Map(Object.entries(json))
5 console.log(map)
6
7 var xhr = new XMLHttpRequest()
8 xhr.addEventListener('load', async () => {
9 const challengeResult = await PerimeterX.handleResponse({ value: xhr.responseText })
10 console.log("result = ", JSON.parse(JSON.stringify(challengeResult)))
11 })
12 xhr.open('GET', 'https://sample-ios.pxchk.net/login')
13 for (const [key, value] of map) {
14 xhr.setRequestHeader(key, value as string)
15 }
16 xhr.send()
17};