Connecting to a mobile application on iOS
-
Download 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 VK Капча SDK version for iOS Swift Package Manager (0.1.0) -
Download the ZIP archive and extract it.
-
Add the
VKCaptchaSDKResources.bundlefile from the archive to your project.
-
-
Get the link for launching the captcha widget from the backend.
-
Create a 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 should happen on 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 link for launching the captcha widget, obtained 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()}