16

Hey all, I am presenting a modal view controller and loading a webpage on that view controller in a UIWebView:

- (void)viewWillAppear:(BOOL)animated
{
     self.requestObj = [NSURLRequest requestWithURL:[NSURL URLWithString:[MPServerPrefs serverPrefs].url_of_sandwich]];
     [self.helpWebView loadRequest:self.requestObj];
}

Everything works fine if I let the webpage load and then dismiss the view. If I dismiss the view while the request is loading, I get this stacktrace:

#0  0x31a94466 in objc_msgSend
#1  0x35ebcb70 in -[UIWebView webView:identifierForInitialRequest:fromDataSource:]
#2  0x35ebc1c0 in -[UIWebViewWebViewDelegate webView:identifierForInitialRequest:fromDataSource:]
#3  0x36130d04 in __invoking___
#4  0x36130bd4 in -[NSInvocation invoke]
#5  0x36130730 in -[NSInvocation invokeWithTarget:]
#6  0x329fc2f4 in -[_WebSafeForwarder forwardInvocation:]

I did some searching and can't figure out what's going on. Any ideas? Do I need to cancel my request when dismissing the view controller?

Many thanks!

2 Answers 2

42

Silly me - you just need to cancel the request and nil out the delegate!

[self.helpWebView setDelegate:nil];
[self.helpWebView stopLoading];
3
  • Where do you do this? dealloc? viewWillDisappear?
    – Bill
    Commented Mar 31, 2015 at 20:24
  • 1
    @Bill - viewWillDisappear IIRC but it's been years since this problem first cropped up. Try that and let me know how it goes!
    – Mark
    Commented Apr 1, 2015 at 18:28
  • 1
    old question but deinit is the best place
    – CZ54
    Commented Dec 12, 2017 at 15:27
0

Your request is running on the main thread. Does the request make your UI hang? If you have to wait for the request to finish before the rest of the code executes, your user will be left wondering what's going on while the view is waiting to load. Try running the request on a separate thread.

2
  • UI does not hang. I can leave the screen while the request is loading which is causing this issue so the UI is not blocked or hanging.
    – Mark
    Commented Apr 21, 2011 at 4:23
  • 1
    UIWebView is part of UIKit and therefore must be accessed on the main thread, right?
    – powerj1984
    Commented Jun 28, 2013 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.