当应用程序从后台恢复时,UIWebView崩溃

时间:2013-05-14 10:28:29

标签: ios uiwebview nsrunloop

我注意到在UIWebView中发生的持续可重现的崩溃: 这里是堆栈跟踪:

Incident Identifier: 4729DA31-A946-436D-97AC-EB3C8746E0FF
CrashReporter Key:   92eabebd7d2b07f27dde132a3c28d6bb7cf92df4
Hardware Model:      iPad3,6
Process:         ... [3120]
Path:            /var/mobile/Applications/10173A06-48D0-48F7-A832-9AC6961B045D/...app/...
Identifier:      ...
Version:         ??? (???)
Code Type:       ARM (Native)
Parent Process:  launchd [1]

Date/Time:       2013-05-09 19:07:31.997 +0100
OS Version:      iOS 6.1.3 (10B329)
Report Version:  104

Exception Type:  EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x00000008
Crashed Thread:  2

...

Thread 2 name:  WebThread
    Thread 2 Crashed:
    0   WebCore                         0x3772a03a WebCore::ThreadTimers::sharedTimerFiredInternal() + 130
    1   WebCore                         0x37729f86 WebCore::timerFired(__CFRunLoopTimer*, void*) + 62
    2   CoreFoundation                  0x31741854 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 12
    3   CoreFoundation                  0x317414fe __CFRunLoopDoTimer + 270
    4   CoreFoundation                  0x31740172 __CFRunLoopRun + 1226
    5   CoreFoundation                  0x316b3238 CFRunLoopRunSpecific + 352
    6   CoreFoundation                  0x316b30c4 CFRunLoopRunInMode + 100
    7   WebCore                         0x37697390 RunWebThread(void*) + 440
    8   libsystem_c.dylib               0x39a530de _pthread_start + 306
    9   libsystem_c.dylib               0x39a52fa4 thread_start + 4

发生以下步骤:

  1. 让UIWebView加载请求(NSURLRequest)
  2. 在加载请求之前将应用程序发送到后台
  3. 等几分钟
  4. 从后台恢复应用程序将因示例堆栈跟踪而崩溃 信息。
  5. 有谁知道为什么会这样? 非常感谢任何帮助!

    修改 用于执行请求的代码

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
        [self.webView loadRequest:urlRequest];
    }
    

    我的UIViewController也是UIWebView的委托,我实现了以下方法:

    -(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
    -(void)webViewDidFinishLoad:(UIWebView *)webView
    

    在调用任何这些方法之前,应用程序崩溃了。

1 个答案:

答案 0 :(得分:3)

要解决此问题,您可以添加通知,当应用程序转到后台进程时,将nil值传递给Web视图。

[[NSNotificationCenter defaultCenter] addObserver: self
                                         selector: @selector(handleEnteredBackground:) 
                                             name: UIApplicationDidEnterBackgroundNotification
                                           object: nil];

或将代码放在applicationDidEnterBackground方法的代码下方,冻结所有任务,直到foreGround进程。

UIApplication *app = application;


    [app beginBackgroundTaskWithExpirationHandler:^{
        // Synchronize the cleanup call on the main thread in case
        // the task actually finishes at around the same time.
        dispatch_async(dispatch_get_main_queue(), ^{

            //            if (bgTask != UIBackgroundTaskInvalid)
            //            {
            //               // [app endBackgroundTask:bgTask];
            //               // bgTask = UIBackgroundTaskInvalid;
            //            }
        });
    }];

    // Start the long-running task and return immediately.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // Do the work associated with the task.


        // Synchronize the cleanup call on the main thread in case
        // the expiration handler is fired at the same time.
        dispatch_async(dispatch_get_main_queue(), ^{

        });
    });