VK Cloud logo

Connecting to a mobile application on iOS

  1. Download the 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 version of the VK Капча SDK for iOS Swift Package Manager (0.1.0)

    2. Download the ZIP archive and unpack it.

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

  2. Get the captcha widget launch link from the backend.

  3. Create the 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 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 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()}
Was this article helpful?