Swift4 iOS集成Stripe SDK添加卡片时加载卡顿求助
Hey there, let's figure out why your "Add Card" loading spinner is stuck! Based on the code you shared, here are the most likely fixes to get your payment flow working smoothly:
1. Ensure You're Implementing the Required Delegate Methods
The biggest culprit for a stuck spinner is forgetting to call the completion handler in the STPAddCardViewControllerDelegate method. When Stripe finishes creating the payment method, you must invoke the completion block to dismiss the loading state.
Double-check that you've added these delegate methods to your view controller:
extension YourViewController: STPAddCardViewControllerDelegate { func addCardViewControllerDidCancel(_ addCardViewController: STPAddCardViewController) { // Dismiss the card view controller when user cancels addCardViewController.dismiss(animated: true, completion: nil) } func addCardViewController(_ addCardViewController: STPAddCardViewController, didCreatePaymentMethod paymentMethod: STPPaymentMethod, completion: @escaping STPErrorBlock) { // Handle the created payment method here (e.g., send it to your backend) // Critical: Call the completion handler to end the loading state! // Pass nil if successful, or an error if something went wrong completion(nil) // Then dismiss the view controller addCardViewController.dismiss(animated: true, completion: nil) } }
If you skip calling completion(nil) or completion(error), the spinner will keep spinning indefinitely.
2. Clean Up Duplicate Publishable Key Configurations
In your AppDelegate, you're setting the publishable key in two places, plus initializing Terminal which might not be necessary if you're only handling standard card payments (Terminal is designed for in-person/terminal-based transactions). This can cause configuration conflicts:
// Remove this line if you're not using Stripe Terminal for in-person payments Terminal.setTokenProvider(StripeAPIClient()) // Pick one way to set the publishable key (preferably using STPPaymentConfiguration) Stripe.setDefaultPublishableKey("pk_test_******") // Remove this if using the shared config STPPaymentConfiguration.shared().publishableKey = "pk_test_*****"
Stick to a single configuration method. For most card payment flows, using STPPaymentConfiguration.shared() is sufficient.
3. Verify Network Access and App Transport Security
Make sure your app has permission to access the internet:
- Check that your
Info.plistincludes theNSAppTransportSecuritydictionary, and that it allows HTTPS connections (Stripe's APIs use HTTPS, so this should be enabled by default). You shouldn't need exceptions, but if you're testing in a restricted environment, ensureNSAllowsArbitraryLoadsisn't set toNOin a way that blocks Stripe's domains.
4. Confirm Valid Sandbox Card Details
Double-check that you're using a valid Stripe sandbox test card. The most reliable one is:
- Card number:
4242 4242 4242 4242 - Expiry date: Any future date (e.g.,
12/30) - CVC: Any 3-digit number (e.g.,
123) - ZIP code: Any valid code (e.g.,
90210)
Using an invalid test card can cause silent failures that leave the spinner running.
Try these steps one by one—most likely, missing the completion handler call is the root issue. Let me know if you still run into problems!
内容的提问来源于stack exchange,提问作者Adnan Afzal

