Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when hostViewController is set to a UINavController #786

Merged
merged 1 commit into from
Sep 7, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions Stripe/STPPaymentContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ @interface STPPaymentContext()<STPPaymentMethodsViewControllerDelegate, STPShipp
@property (nonatomic) STPPaymentContextAmountModel *paymentAmountModel;
@property (nonatomic) BOOL shippingAddressNeedsVerification;

// If hostViewController was set to a nav controller, the original VC on top of the stack
@property (nonatomic, weak) UIViewController *originalTopViewController;

@end

@implementation STPPaymentContext
Expand Down Expand Up @@ -176,6 +179,9 @@ - (BOOL)loading {
- (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];
Expand Down Expand Up @@ -377,7 +383,12 @@ - (void)appropriatelyDismissPaymentMethodsViewController:(STPPaymentMethodsViewC
}];
} else {
// otherwise, we've been pushed onto the stack.
[viewController.navigationController stp_popToViewController:self.hostViewController animated:YES completion:^{
UIViewController *destinationViewController = self.hostViewController;
// If hostViewController is a nav controller, pop to the original VC on top of the stack.
if ([self.hostViewController isKindOfClass:[UINavigationController class]]) {
destinationViewController = self.originalTopViewController;
}
[viewController.navigationController stp_popToViewController:destinationViewController animated:YES completion:^{
self.paymentMethodsViewController = nil;
if (completion) {
completion();
Expand Down Expand Up @@ -492,7 +503,12 @@ - (void)appropriatelyDismissViewController:(UIViewController *)viewController
}];
} else {
// otherwise, we've been pushed onto the stack.
[viewController.navigationController stp_popToViewController:self.hostViewController animated:YES completion:^{
UIViewController *destinationViewController = self.hostViewController;
// If hostViewController is a nav controller, pop to the original VC on top of the stack.
if ([self.hostViewController isKindOfClass:[UINavigationController class]]) {
destinationViewController = self.originalTopViewController;
}
[viewController.navigationController stp_popToViewController:destinationViewController animated:YES completion:^{
if (completion) {
completion();
}
Expand Down