URLSession integration

How to use the DataDome SDK with URLSession

VersionVersion LicenseLicense PlatformPlatform

For each request to protect with DataDome, use the following methods to create the data task

func protectedDataTask(with url: URL,
                     captchaDelegate: CaptchaDelegate?,
                     completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) 
-> URLSessionDataTask
func protectedDataTask(with request: URLSessionRequest,
                     captchaDelegate: CaptchaDelegate?,
                     completionHandler: @escaping (Data?, URLResponse?, Error?) -> Void) 
-> URLSessionDataTask

Only the URLSessionDataTasks created by the above two methods are monitored and protected by DataDome.

Here a sample code to create and execute a data task

import DataDomeSDK


guard let url = URL(string: "https://datadome.co") else {
    return
}

let task = URLSession.shared.protectedDataTask(with: url, captchaDelegate: self, completionHandler: { _, _, _ in
    print("Request completed")
})
        
task.resume()

To manually manage the presentation of the captcha view controller, implement the CaptchaDelegate protocol

extension MyViewController: CaptchaDelegate {
    func present(captchaController controller: UIViewController) {
        self.navigationController?.present(controller, animated: true, completion: nil)
    }
    
    func dismiss(captchaController controller: UIViewController) {
        controller.dismiss(animated: true, completion: nil)
    }
}

Alternatively, you can pass nil to the captchaDelegate, the SDK will automatically manage navigation for you and will display the CaptchaViewController modally on top of the visible view controller.