Alamofire / Moya Integration
How to integration the DataDome SDK with Alamofire / Moya
Installation
Swift Package Manager
DataDomeAlamofire is available on Swift Package Manager. To get the SDK integrated in your project, follow the steps below:
- 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
DataDomeAlamofire is available on CocoaPods. Simply add the following line in your Podfile:
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
binary "https://package.datadome.co/ios/DataDomeAlamofire.json"
- Run the following command to fetch and integrate the package
carthage update --use-xcframeworks
Setup
Deactivate the automtic interception
When using the DataDomeAlamofire SDK, you will need to deactivate the automatic interception.
To do so, add the following to your info.plist
file.
<key>DataDomeProxyEnabled</key>
<false/>
Usage
The Alamofire Session
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.
Request validation
Alamofire
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
}
Moya
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.
The Captcha delegate
You can conform to the CaptchaDelegate protocol and handle manually the navigation of the Captcha View Controller
let dataDome = AlamofireInterceptor(captchaDelegate: self)
Implement conformence to 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")
}
}
}
Cookies and URLRequest
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()
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()
More
SFCC support
When calling an SFCC endpoint, the captcha will be displayed according to different rules.
Please follow those 3 steps to get started:
- set
DataDomeSDK.bypassHTTPAccept
totrue
- create a
Redirector
to manage redirections- if the response's
Location
header contains a URL with a path containingDDUser-Challenge
, cancel it
- if the response's
- add the redirector the the Alamofire
Session
Example:
/// Stop the DataDome SDK from forcing the `Accept` header to `application/json`
DataDomeSDK.bypassHTTPAccept = true
/// An Alamofire Redirector will "intercept" all redirections
/// If the redirection is caused by a DataDome response, block it (aka. return `nil`)
let redirector = Redirector(behavior: .modify({ _, request, response in
if let location = response.value(forHTTPHeaderField: "location"),
let url = URL(string: location),
url.pathComponents.contains("DDUser-Challenge") {
return nil
}
return request
}))
/// Add the redirector to the Alamofire Session
let alamofireSessionManager = Session(configuration: sessionManager.sessionConfiguration,
interceptor: interceptor,
redirectHandler: redirector)
Updated about 2 months ago