Search code examples
iosswiftbraintree

iOS : Add a UIView returned by Braintree to the screen


I'm following Braintree's guidelines to add the dropIn functionality to an iOS app. In order to show the dropIn I use the following method:

func showDropIn(clientTokenOrTokenizationKey: String) {
    let request =  BTDropInRequest()
    let dropIn = BTDropInController(authorization: clientTokenOrTokenizationKey, request: request)
    { (controller, result, error) in
        if (error != nil) {
            print("ERROR")
        } else if (result?.isCancelled == true) {
            print("CANCELLED")
        } else if let result = result {
            let paymentMethod = result.paymentMethod!.nonce
            self.paymentNonce = paymentMethod

            self.paymentLabel.text = result.paymentDescription

            print(result.paymentIcon)
        }

        controller.dismiss(animated: true, completion: nil)
    }
    self.present(dropIn!, animated: true, completion: nil)
}

What I want to do after the user picks a specific form of payment is to show both the description ("ending in XX") and the card's icon on the screen. I have no problem with the description, but I don't know how to get the card icon to show.

The following line:

print(result.paymentIcon)

returns the following:

<BTUIKMasterCardVectorArtView: 0x7fd04ad15280; frame = (0 0; 0 0); opaque = NO; layer = <CALayer: 0x600000230c20>>

According to documentation, result.paymentIcon is a UIView.

@property(readonly, nonatomic) UIView *_Nonnull paymentIcon;

A UIView (BTUIKPaymentOptionCardView) that represents the payment option

I have tried setting up a UIView and then adding the paymentIcon as a subview, but still I can never get it to show.

let paymentIcon = result.paymentIcon
self.iconView.addSubview(paymentIcon)

Solution

  • You can use the BTUIKViewUtil.vectorArtView method to retrieve a image and show in UIImageView, like so:

    let size = CGSize(width: WIDTH_HERE, height: HEIGHT_HERE)
    let imageView = UIImageView(frame: CGRect(origin: .zero, size: size))
    imageView.image = BTUIKViewUtil.vectorArtView(for: result.paymentOptionType).image(of: size)
    

    Example-2, using paymentIcon view:

    let view = result.paymentIcon as? BTUIKVectorArtView
    imageView.image = view?.image(of: size)