You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

信用卡交易APP调用错误处理器后屏幕冻结问题咨询

Troubleshooting Screen Freeze in Payment Error Handler

Let's break down why your screen is freezing when the transaction error handler is triggered, and fix it step by step.

First, Spot the Obvious Gaps in Your Code

Looking at your error handler snippet:

dispatch_async(dispatch_get_main_queue(), ^{ 
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Your transaction is decl..."]; 
});

You’re creating a UIAlertView but never calling [alert show]; to display it. On top of that, UIAlertView has been deprecated since iOS 9 — using outdated APIs can lead to unexpected UI glitches, including hangs.

Common Causes of the Freeze

  • Unpresented Alert + Missing Navigation: Users see no feedback because the alert doesn’t appear, and you’re not triggering the navigation back to the previous screen. This makes the app feel frozen.
  • Incomplete Alert Initialization: Your UIAlertView init call is missing critical parameters (message, delegate, button titles), which could cause silent initialization failures under the hood.
  • Potential Main Queue Blocking: Even though you’re dispatching to the main queue, if there’s a long-running task (like heavy computation or unoptimized UI updates) blocking the main queue elsewhere in PaymentController, it’ll lock up the screen.

Fixes to Resolve the Issue

1. Replace Deprecated UIAlertView with UIAlertController

UIAlertController is the modern, supported replacement. Here’s how to implement it correctly, including the required navigation back action:

errorHandler:^(NSError* error) {
    dispatch_async(dispatch_get_main_queue(), ^{
        // Create a complete alert with clear messaging
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Transaction Declined"
                                                                       message:@"Please try a different credit card."
                                                                preferredStyle:UIAlertControllerStyleAlert];
        
        // Add an OK button that navigates back after tapping
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK"
                                                           style:UIAlertActionStyleDefault
                                                         handler:^(UIAlertAction * _Nonnull action) {
                                                             // Return to the previous screen
                                                             [self.navigationController popViewControllerAnimated:YES];
                                                         }];
        [alert addAction:okAction];
        
        // Present the alert to the user
        [self presentViewController:alert animated:YES completion:nil];
    });
}

2. Ensure No Main Queue Blocking

Check if any code in saleRequestComplete: or other parts of PaymentController is running long tasks (like network calls, data parsing) on the main queue. Move those tasks to a background queue using:

dispatch_async(dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0), ^{
    // Long-running task here
    dispatch_async(dispatch_get_main_queue(), ^{
        // Update UI after task completes
    });
});

3. Verify Thread Safety of sharedVtp

Confirm that sharedVtp’s error handler isn’t already running on the main queue (your dispatch_async is safe either way, but it’s good to double-check). All UI operations must run on the main queue, so your current dispatch is the right approach if the handler fires on a background thread.

Additional Checks

  • Simulate a declined transaction directly to test if the alert appears and navigation works as expected.
  • Use Xcode’s Debug Navigator to inspect the main thread during the error scenario — look for long-running tasks that might be blocking the queue.

内容的提问来源于stack exchange,提问作者ravi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:50:02