Crash when calling openURL

Hi,

my app is crashing when a UIWebView tries to open a URL in the UIApplication. This happens only on a device and only with an ipa that was compiled with iOS8 SDK (it's an app that is on the appstore). The crashlog looks somehow wierd though because it says "SIMULATED (this is NOT a crash)"



Exception Type:  00000020
Exception Codes: 0x000000008badf00d
Exception Note:  SIMULATED (this is NOT a crash)
Highlighted by Thread:  0


Application Specific Information:
net.tequilaapps.PStatsApp failed to scene-update after 10.00s


Elapsed total CPU time (seconds): 145.770 (user 145.770, system 0.000), 45% CPU
Elapsed application CPU time (seconds): 31.125, 10% CPU


Filtered syslog:
None found


Thread 0 name:  Dispatch queue: com.apple.main-thread
Thread 0:
0   libsystem_kernel.dylib         0x39b5ff58 semaphore_wait_trap + 8
1   libdispatch.dylib             0x39a6c4e2 _dispatch_semaphore_wait_slow + 186
2   libdispatch.dylib             0x39a6c4e2 _dispatch_semaphore_wait_slow + 186
3   libxpc.dylib                   0x39c33a26 xpc_connection_send_message_with_reply_sync + 174
4   MobileCoreServices             0x2a1526d6 _LSStartOpenOperation + 178
5   MobileCoreServices             0x2a171d00 -[LSOpenOperation main] + 1416
6   MobileCoreServices             0x2a158a80 -[LSApplicationWorkspace openURL:withOptions:error:] + 356
7   MobileCoreServices             0x2a158912 -[LSApplicationWorkspace openURL:withOptions:] + 26
8   UIKit                         0x2c396218 -[UIApplication openURL:] + 356
9   PStatsApp                   0x005b3f20 0xa000 + 5938976
10  PStatsApp                   0x005e9c90 0xa000 + 6159504
11  PStatsApp                   0x005e90f8 0xa000 + 6156536
12  PStatsApp                   0x005e8b86 0xa000 + 6155142
13  PStatsApp                   0x005eb696 0xa000 + 6166166
14  UIKit                         0x2c351736 -[UIWebView webView:decidePolicyForNavigationAction:request:frame:decisionListener:] + 294



The code causing this is this


#pragma mark - UIWebViewDelegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
  NSLog(@"Dismissing");
  [self.presentingViewController dismissViewControllerAnimated:YES
  completion:^{
  NSLog(@"Dismissed");
  }];

  [[UIApplication sharedApplication] openURL:request.URL];

  return NO;
}


I also see this log message in the console:


void SendDelegateMessage(NSInvocation *): delegate (webView:decidePolicyForNavigationAction:request:frame:decisionListener:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode


Any ideas what this crash means ?


cheers

Maybe try wrapping the openURL call in a dispatch_async() to ensure that you are able to return from the delegate method before pulling the rug out from under yourself.

In fact, maybe the dismiss should be in the dispatch_async and the openURL in the completion block?

The exception code "8badf00d" (ate bad food) means that the system killed your app because it took too long to startup. In addition, openURL is calling a synchronous method under the hood to get the url, looks like it took too long (> 10 seconds).

I forgot to mention that this happens on iOS 9 only. It looks indeed like the openURL takes a lot of time when executed in the same time as the dismissViewController. I tried opening the URL in the dispatch_async but the app still freezes. Opening the URL on the dismiss completion block works but I need to 1st open the URL and only then dismiss the view controller.

It looks like this happens only when the view controller has this


- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}
Accepted Answer

Does the openURL has to be called on the main thread ? Using dispatch_async on a background thread seems to work

So when I call the openURL in a background thread, it works but is it safe to call it in a background thread


dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  NSLog(@"Application will follow offer url: %@", offerURL);
  [[UIApplication sharedApplication] openURL:offerURL];
});

Here is a sample project that reproduces the issue. Open the webview and type 'apple' in Google. it will trigger the code that dismisses the webview and freezes the app


https://www.dropbox.com/s/i0ga4t43hnyhnl6/TestWebViewDismiss.zip?dl=0

Crash when calling openURL
 
 
Q