Manual integration
  • 02 May 2023
  • Dark
    Light

Manual integration

  • Dark
    Light

Article Summary

v3.0

Understanding the flow

SDK's HTTP headers must be included in your URL requests. To your delight, the SDK automatically intercepts URL requests from your app which based href="https://developer.apple.com/documentation/foundation/urlsession">URL Session (including Alamofire). By doing so, the SDK takes care of adding those headers to your URL requests and handling blocks response from your server, which means you don't have to do anything else.

What is included in the SDK's HTTP headers 
Those HTTP headers include a token that allows your server to verify that URL requests comes from your app, which running the SDK. In addition to the token, those headers include information about the device (OS name, OS version, device model, SDK version) and identifiers which generated by the SDK.

However, you may choose to take care of them manually.

Adding SDK's HTTP headers to your URL requests

  1. Disable requests interception by setting the PXPolicy/urlRequestInterceptionType to PXPolicyUrlRequestInterceptionType/none.
policy.urlRequestInterceptionType = .none
policy.urlRequestInterceptionType = PXPolicyUrlRequestInterceptionTypeNone
  1. Before sending your URL request, take HTTP headers from the SDK and add them to your request.
let headers = PerimeterX.headersForURLRequest()
// add those headers to your URL request
NSDictionary<NSString *, NSString *> *headers = [PerimeterX headersForURLRequestForAppId:nil];
// add those headers to your URL request

🚧Headers should not be empty
When no headers are returned, it means that something went wrong with the SDK integration.
❗️Don't cache headers from the headersForURLRequest function!
You should not cache those headers from the PerimeterX/headersForURLRequest(forAppId:) function. Those headers contain a token with expiration date. The SDK manages this token to be up-to-date.<
👍Cache headers from the perimeterxHeadersWereUpdated delegate function
You may use the PerimeterXDelegate/perimeterxHeadersWereUpdated(headers:forAppId:) function to cache those HTTP headers. However, you must ensure that you update your cache every time this function is called.

Handle block responses from your server

After receiving an error in the server's response, pass the information to SDK with the PerimeterX/handleResponse(response:data:forAppId:callback:) function.

let isHandledBySDK = PerimeterX.handleResponse(response: response, data: data) { result in
    print("challenge result = \(result)")
}
BOOL isHandledByPX = [PerimeterX handleResponseWithResponse:response data:data forAppId:nil callback:^(enum PerimeterXChallengeResult result) {
    NSLog(@"challenge result = %ld", result);
}];

If the response cannot be handled by the SDK, this function will return false. This function returns true when the response was handled by the SDK. That means that a challenge will be presented to the user. After the user solved/cancelled the challenge, the handler code will be called with the proper result. You may use this handler to retry your request.


Was this article helpful?