Alamofire / Moya Integration
How to integration the DataDome SDK with Alamofire / Moya
If you are using Alamofire or Moya to build your app's networking layer, we have you covered. Install the DataDome Alamofire plugin
to protect your requests using the DataDome SDK.
Swift package manager
- Go to "Xcode > File > Swift Packages > Add Package Dependency", select the target where to integrate DataDomeAlamofire
- Paste the following git URL in the search bar
https://github.com/DataDome/datadome-alamofire-package
- Select
DataDomeAlamofire
and pressAdd
.
Cocoapods
To install the plugin, use the following Cocoapods instruction:
pod "DataDomeAlamofire"
Carthage
DataDomeAlamofire is available on Carthage. To get the SDK integrated in your project, follow the steps below:
- Create a Cartfile if not already created.
- Add the following dependency in your project Cartfile
github "DataDome/datadome-alamofire-package"
- Run the following command to fetch and integrate the package
carthage update
Create your Alamofire Session Manager as shows in the example below:
import DataDomeAlamofire
let configuration = URLSessionConfiguration.default
configuration.headers = .default
configuration.httpCookieStorage = HTTPCookieStorage.shared
configuration.httpShouldSetCookies = true
let dataDome = AlamofireInterceptor()
let interceptor = Interceptor(adapter: dataDome.sessionAdapter,
retrier: dataDome.sessionRetrier)
let alamofireSessionManager = Session(configuration: configuration,
interceptor: interceptor)
The remainder of your app won't change. Only make sure to use the created alamofireSessionManager
to perform your requests.
Alternatively, you can conform to the CaptchaDelegate protocol and handle manually the navigation of the Captcha View Controller
let dataDome = AlamofireInterceptor(captchaDelegate: self)
Implement the CaptchaDelegate protocol
import DataDomeSDK
extension AlamofireViewController: CaptchaDelegate {
func present(captchaController controller: UIViewController) {
present(controller, animated: true) {
print("Captcha displayed")
}
}
func dismiss(captchaController controller: UIViewController) {
controller.dismiss(animated: true) {
print("Captcha dismissed")
}
}
}
Important
When using Alamofire, make sure you call .validate()
for each request to make sure Alamofire does call the retry function and hands the execution to the DataDome SDK in case of a 403 response with a Captcha Challenge.
self.alamofireSessionManager
.request(endpoint)
//validate here is mandatory
.validate()
.responseData { response in
}
When using Moya, make sure you explicitly implement validationType
in your service implementation
extension MyService: TargetType {
var validationType: ValidationType {
let type = ValidationType.successAndRedirectCodes
return type
}
}
The validationType attribute will make Moya call the .validate()
function when handing the request to Alamofire.
Important
If you are creating an instance of URLRequest and passing it to Alamofire Session Manager, make sure to hint iOS to handle cookies otherwise, you may experience captcha loops in the app.
var request = URLRequest(url: url)
request.httpShouldHandleCookies = true
session.request(request).validate()
Updated about 1 year ago