Stripe-ios: setting hostviewcontroller to something else

Created on 28 Nov 2017  Â·  4Comments  Â·  Source: stripe/stripe-ios

When I try to set hostViewController to a different viewcontroller my app crash with below assertion in the stripe iOS library. I made a singleton class that handles stripe things, and I want to present PaymentMethodsViewController in two unique viewcontrollers by just updating the hostViewController property of STPPaymentContext. When I just deleted the assertion below and ran the code, the code seems to be working fine. Is this okay? Why can't I change the hostViewController on an STPPaymentContext after it's already been set?

- (void)setHostViewController:(UIViewController *)hostViewController {
    NSCAssert(_hostViewController == nil, @"You cannot change the hostViewController on an STPPaymentContext after it's already been set.");
    _hostViewController = hostViewController;
    if ([hostViewController isKindOfClass:[UINavigationController class]]) {
        self.originalTopViewController = ((UINavigationController *)hostViewController).topViewController;
    }
    [self artificiallyRetain:hostViewController];
    [self.willAppearPromise voidCompleteWith:hostViewController.stp_willAppearPromise];
    [self.didAppearPromise voidCompleteWith:hostViewController.stp_didAppearPromise];
}

I tried to use two STPPaymentContext in 2 different viewControllers with ephemeral keys but the problem with this is that the payment methods are not synced one to the another when you add, deleted a method.

Most helpful comment

Hi @coolcool1994,

STPPaymentContext is not designed to be long lived. It is made to handle one individual payment, and you need to make a new instance of it for each payment that occurs.

If you want to present payment methods view controller outside of a checkout flow (eg from a settings screen), you can instantiate STPPaymentMethodsViewController directly with your shared STPCustomerContext and then directly push or present it where you need to.

The overall architecture of the classes are: The Stripe and STPAPIClient objects are designed to be your long lived singleton-style instances that contain global configuration shared for the entire life of your app (like your publishable key). STPCustomerContext should last for the duration of time during which that customer is logged in. STPPaymentContext should last for the duration of a single payment/checkout flow.

I hope this clears things up for you. If you have some other use case you think does not fit into this existing flow, please let us know and we'll see what we can do to support you.

Brian

All 4 comments

Hey @coolcool1994,
If you're having trouble integrating the Stripe iOS SDK, I suggest writing in to [email protected] – they'll be able to help you with any questions you might have.
Best,
Ben

I contacted [email protected] and they told me to ask it here to make this a feature request. IT would be so nice if I can use Stripe API as one singleton and just change the hostViewController to #which I am presenting the PaymentViewController to.

Hi @coolcool1994,

STPPaymentContext is not designed to be long lived. It is made to handle one individual payment, and you need to make a new instance of it for each payment that occurs.

If you want to present payment methods view controller outside of a checkout flow (eg from a settings screen), you can instantiate STPPaymentMethodsViewController directly with your shared STPCustomerContext and then directly push or present it where you need to.

The overall architecture of the classes are: The Stripe and STPAPIClient objects are designed to be your long lived singleton-style instances that contain global configuration shared for the entire life of your app (like your publishable key). STPCustomerContext should last for the duration of time during which that customer is logged in. STPPaymentContext should last for the duration of a single payment/checkout flow.

I hope this clears things up for you. If you have some other use case you think does not fit into this existing flow, please let us know and we'll see what we can do to support you.

Brian

FYI, adding a new hostViewController is allowed when the SDK is in Debug mode.....we had a bit of confusion facing this when we switched to production.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

levisbotio picture levisbotio  Â·  6Comments

odedharth picture odedharth  Â·  7Comments

Andrewangeta picture Andrewangeta  Â·  6Comments

paynd picture paynd  Â·  3Comments

mansoorshahsaid picture mansoorshahsaid  Â·  4Comments