VK Cloud logo

Connecting to a mobile application on iOS

  1. Download VK Капча SDK:

    1. Add the library to the dependencies in the Package.swift file:

      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)

    2. Download the ZIP archive and extract it.

    3. Add the VKCaptchaSDKResources.bundle file from the archive to your project.

  2. Get the link for launching the captcha widget from the backend.

  3. Create a captcha configuration. Use the configuration object VKCaptchaConfiguration(url:).

  4. Display the captcha to the user. Use the SDK method VKCaptcha.getCaptchaViewController(completion:).

  5. 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 token   let captcha = VKCaptcha(configuration: config)   let vc = captcha.getCaptchaViewController { [weak self] result in      switch result {         case .success(let token):            self?.repeatRequest(with: token)         case .failure:                 // Handling captcha closure                 break      }   }   present(vc, animated: true)}func performAction() {   URLSession.shared.dataTask(with: url) { data, _, _ in   guard 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 captcha         return      }      DispatchQueue.main.async { [weak self] in         self?.handleCaptcha(captchaUrl: captchaUrl)      }   }.resume()}