# iOS

SPIDX Button is an iOS library conceived to be integrated into an iOS Application. It provides a Button View to be used in the application and classes that communicate with SPIDX server to retrieve a Dynamic Link and ID for the transaction defined by a Use Case.

## Requirements

* Minimum IOS Version

  > IOS 13.0
* Development environment

  > Xcode IDE v11+

## Import and configuration

1. Drag the `SpidxButton.framework` folder into the Frameworks folder.

   > If the Frameworks folder does not exist, drag the file into the project folder.
2. Open the project settings by clicking on the top item in the file tree.
3. Go to the `__Geral__` (or `__General__`) tab of your `__target__`.
4. Click and drag the `.framework` from the project’s file tree to the “`__Frameworks, Libraries and etc__`” section.
5. Change the configuration of the frameworks from **Do not embed** to **Embed & Sign**.

## Collection Types

* Collection: Standard biometric collection.
* Verification: Biometric collection to check if it matches an existing account (SpidxAccount)
* Registration: Standard biometric collection that reuses data from a past collection already carried out.

## Classes and Methods

### SpidxButton Class

* **collection():**

  > Method that uses the variables `apiKey` and `useCaseName` to retrieve the dynamic link for a collection-type link. The same method can be used to redeem registration-type links.
* **verification():**

  > Method that uses the variables `apiKey`, `useCaseName`, and `spidxAccount` to retrieve the verification dynamic link.
* **setAuth(apiKey: String, useCaseName: String, spidxAccount: String? = nil):**

  > Method required to populate the variables that will be used by the functions listed above. Both apiKey and useCaseName are mandatory parameters. SpidxAccount is optional and if it is not defined in the method call it will have a null value.

### Responses

The methods **collection ()** and **verification ()** have a callback that returns an object of type `UseCaseData`. This object has the variables: `dynamicLink`, `transactionID`, `errorDescription`, and `rawError`. If an error occurs, the error variables will be populated and the others will be null. In case of success, the variables related to the link and the transaction are populated and the others are null.

## Usage

You can visually add the button to `.storyboard` or `.xib` files as it follows:

* Add a common UIButton and change its class to **SpidxButton**, as well as its module.
* Change the type of the button to custom, if not.
* Create a reference and action outlet for the button:

```swift
import SpidxButton
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var spidxButton: SpidxButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.spidxButton.setAuth(apiKey: <String>, useCaseName: <String>, spidxAccount: <String>)
    }

    @IBAction func spidxButtonTapped(_  sender: Any) {

        self.spidxButton.verification() { data in
            //Access UseCaseData
            print("dynamicLink", data?.dynamicLink)
            print("transactionID", data?.transactionID)
            print("error", data?.rawError)
            print("errorDesc", data?.errorDescription)
        }
    }
}
```

You can also add the button to a view programmatically as it follows:

```swift
import SpidxButton
import UIKit

class ViewController: UIViewController {

    var spidxButton: SpidxButton!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.spidxButton = SpidxButton(frame: CGRect(x: <CGFloat>, y: <CGFloat>, width: <CGFloat>, height: <CGFloat>))
        self.view.addSubview(spidxButton)

        self.spidxButton.setAuth(apiKey: <String>, useCaseName: <String>, spidxAccount: <String>)

        spidxButton.addTarget(self, action: #selector(spidxButtonTapped(_:)), for: .touchUpInside)
    }

    @objc func spidxButtonTapped(_ sender: SpidxButton) {
        self.spidxButton.verification() { data in
            //Access UseCaseData
            print("dynamicLink", data?.dynamicLink)
            print("transactionID", data?.transactionID)
            print("error", data?.rawError)
            print("errorDesc", data?.errorDescription)
        }
    }

}
```

Note that the proportions of the button will always be respected in any of the cases mentioned above, that is, the height of the button will always be 1/4 of the width. Even if another value is passed in the constructor of the button it will be overwritten.

After generating the link it is recommended that it should be opened using Firebase’s DynamicLink service, available through cocoa pods (<https://firebase.google.com/docs/dynamic-links/ios/receive>), or using something like:

```swift
self.spidxButton.verification() { data in

	//Open DynamicLink on Browser
	guard data?.dynamicLink != nil, let url = URL(string: (data?.dynamicLink)!) else {return}

	DispatchQueue.main.sync {
		UIApplication.shared.open(url)
	}
}
```

which will open the SPIDX app itself (if the user has it installed) or the link in question in a browser, which will then redirect the user to SPIDX.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.griaule.com/sdks/en/spidxbutton/spidxbuttonios.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
