Connecting to a mobile application on iOS
-
Download the VK Капча SDK:
Swift Package ManagerCocoaPods-
Add the library to the dependencies in the
Package.swiftfile:dependencies: \[.package(url: "https://github.com/VKCOM/vkid-captcha-ios-sdk", .upToNextMajor(from: "<VERSION>"))\]Here,
<VERSION>is the version of the VK Капча SDK for iOS Swift Package Manager (0.1.0) -
Download the ZIP archive and unpack it.
-
Add the
VKCaptchaSDKResources.bundlefile from the archive to your project.
-
-
Get the captcha widget launch link from the backend.
-
Create the captcha configuration. Use the configuration object VKCaptchaConfiguration(url:).
-
Display the captcha to the user. Use the SDK method VKCaptcha.getCaptchaViewController(completion:).
-
Handle the captcha completion result:
- If the method returned a successful captcha completion token, send it to the backend for validation.
- If the user closed the captcha, nothing is sent to the backend. Determine what will happen in the frontend in this case.
Swift code example:
import VKCaptchaSDKstruct APIResponse: Decodable {let captchaUrl: String?}func handleCaptcha(captchaUrl: URL) {let config = VKCaptchaConfiguration(url: captchaUrl) // The captcha widget launch link received from the backend contains a session tokenlet captcha = VKCaptcha(configuration: config)let vc = captcha.getCaptchaViewController { [weak self] result inswitch result {case .success(let token):self?.repeatRequest(with: token)case .failure:// Handling captcha closurebreak}}present(vc, animated: true)}func performAction() {URLSession.shared.dataTask(with: url) { data, _, _ inguard let data = data,let response = try? JSONDecoder().decode(APIResponse.self, from: data),let captchaUrlString = response.captchaUrl,let captchaUrl = URL(string: captchaUrlString) else {// Handling a successful response without captchareturn}DispatchQueue.main.async { [weak self] inself?.handleCaptcha(captchaUrl: captchaUrl)}}.resume()}
Was this article helpful?