Observing States and Submitting Data

State

An object you use for observing VGSTextField parameters that depends on input

Declaration

class State

You should not create State object by itself, instead get access to it from VGSTextField.state attribute

State attributes

Attribute Type/Default value
isRequired Bool = false
isValid Bool = false
isEmpty Bool = false

CardState

An object you use for observing VGSTextField parameters when VGSConfiguration.type defined as FieldType.cardNumber

Declaration

class CardState: State

CardState extends default State with more specific attributes related to credit card numbers

CardState attributes

Attribute Type/Default value
last4 String = “”
bin String = “”
cardBrand CardType = .unknown

VGSCollect

An object you use for observing VGSTextField states and submitting data

Declaration

class VGSCollect

Creating a VGSCollect

init(id: String, environment: Environment = .sandbox)
  • id: your organization <vault-id>
  • environment: your organization environment. Should use .sandbox for testing and .live for production.

Note

  • All View Controllers that use VGS Collect UI components need VGSCollect instance initialization.
  • All VGSCollect instances will be work separately.
  • All VGSCollect instances can be configured differently.

Catching States from text fields

You can observe states every time VGSTextfield input is changed

/// Observe only focused text field
var observeFieldState: ((_ textField: VGSTextField) -> Void)?

/// Observe all text fields registered in a VGSCollect instance
var observeStates: ((_ form: [VGSTextField]) -> Void)?

Code example

vgsCollect.observeStates = { textFields in
    textFields.forEach({ textField in
           print(textField.state.isValid)
           if let cardState = textField.state as? CardState {
                print(cardState.last4)
            }
    })
}

Securely Collecting and Submitting data

Send data to your organization vault

func submit(path: String, method: HTTPMethod = .post,
                        extraData: [String: Any]? = nil,
                 completion block:@escaping (_ data: JsonData?, _ error: Error?) -> Void)
  • path: String, inbound rout path
  • method: HTTPMethod, default is .post
  • extraData: [String: Any]?, any data you want to send together with data from VGSTextFields, default is nil
  • completion: response completion block, returns JsonData as [String: Any]? `` or Error? from Server

Note

  • Data will be collected from all text fields that registered to current VGSCollect instance and send to your organization <vault-id>.

Code example

func sendData(_ sender: UIButton) {
    /// extra information can be sent together with all sensitive data from VGSTextFields
    var extraData = [String: Any]()
    extraData["cardHolderName"] = "Joe Business"

    /// submit and send data
    vgsCollect.submit(path: "/post", extraData: extraData, completion: { (json, error) in
        if error == nil, let json = json {
            // parse incoming data
        } else {
            // handle error
        }
    })
}

Advanced Settings

Setting Custom API Headers

In case if you need additional headers in submit request, there is possibility to add them

var customHeaders: [String: String]?

Code example

/// set custom headers
vgsCollect.customHeaders = [
    "custom header": "custom data"
]