DataDomeSDK is available on Swift Package Manager. To get the SDK integrated to your project:
https://github.com/DataDome/datadome-ios-package
DataDomeSDK
and press Add
.Alternatively, you can get the DataDomeSDK on CocoaPods. Simply add the following line to your Podfile:
pod "DataDomeSDK"
Run pod install
to download and integrate the framework to your project.
Fatal error: [DataDome] Missing DataDomeKey (Your client side key) in your Info.plist
[DataDome] Version x.y.z
Congrats, the DataDome framework is well integrated
If you need to see the logs produced by the framework, you can set the log level to control the detail of logs you get
import DataDomeSDK
DataDomeSDK.setLogLevel(level: .verbose)
By default, the framework is completely silent.
The following table contains different logging levels that you may consider using
Level | Description |
---|---|
verbose | Everything is logged |
info | Info messages, warnings and errors are shown |
warning | Only warning and errors messages are printed |
error | Only errors are printed |
none | Silent mode (default) |
You can simulate a captcha display using the framework by providing a user agent with the value BLOCKUA
To do so:
The DataDome framework will inject the specified user agent in the requests the app will be sending. Using the BLOCKUA user agent value will hint our remote protection module installed on your servers to treat this request as if it is coming from a bot. Which will block it with a captcha response.
Since the DataDome framework retains the cookies after resolving the captcha, this test can be done only the first time you used the BLOCKUA user agent. To reproduce the test case, you can use the following code snippet to manually clear the cookies stored in your app
for cookie in HTTPCookieStorage.shared.cookies ?? [] {
HTTPCookieStorage.shared.deleteCookie(cookie)
}
The DataDome Framework is using method swizzling to setup a proxy
listening to all URLRequests
performed by the app. The framework will then detect automatically blocked requests, lookup dynamically the top view controller in your app hierarchy and attach a captcha view controller to it. Once resolved, the framework will dismiss the captcha view controller and retry the blocked request with a new generate cookie. All this is done under the hood.
If you want to disable the method swizzling and manually tell the framework which request to monitor instead of monitoring all the traffic or / and you want to manually present and dismiss the captcha controller, please consider the following steps:
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)
}
}
Visit the official documentation for more details.
link |
Stars: 3 |
Last commit: 1 week ago |
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics