0

我注意到 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. 从后台恢复应用程序会因示例堆栈跟踪信息而崩溃。

有谁知道为什么会这样?很感谢任何形式的帮助!

编辑: 用于执行请求的代码

- (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

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

4

1 回答 1

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(), ^{

        });
    });
于 2013-05-14T10:49:19.963 回答