Skip to content
Advertisement

EXEC_BAD_ACCESS error when using WebViewJavascriptBridge

I’m a very new iOS developer (I only started a few days ago), and I’m trying to utilize the WebViewJavascriptBridge class with a UIWebView that I’ve got on my Storyboard. Whenever I try to use it, I get an EXEC_BAD_ACCESS error.

The trouble lines seem to be:

- (void)viewDidLoad
{
    [super viewDidLoad];

    WebViewJavascriptBridge* bridge = [WebViewJavascriptBridge bridgeForWebView:webView handler:^(id data, WVJBResponseCallback responseCallback) {
        NSLog(@"Received message from javascript: %@", data);
        responseCallback(@"Right back atcha");
    }];

    webView.scrollView.bounces = NO;

    [[UIApplication sharedApplication] setStatusBarHidden:YES];

    NSString *path = [[NSBundle mainBundle] bundlePath];
    path = [NSString stringWithFormat:@"%@/%s", path, "htdocs/index.html"];
    NSURL *URL = [NSURL fileURLWithPath:path];

    [webView loadRequest:[[NSURLRequest alloc] initWithURL:URL]];
}

To be exact, the last line. If I don’t make that request, I don’t get the error. I’ve tried it with a UIWebView created just in Objective-C, and still gotten the error, although maybe I did it wrong.

Any suggestions?

EDIT:

Method for storing webView is this code + reference outlet.

@interface mcViewController : UIViewController
    {
        IBOutlet UIWebView *webView;
    }
@end

Advertisement

Answer

EXC_BAD_ACCESS usually occurs when you’ve tried to access an objective C object that has been deallocated. I can’t see a problem from the code you’ve posted, so actual issue may be elsewhere. Check your properties – is everything that should be strong actually declared that way?

Please enable zombies to get information about what type of object it is that has been accessed after deallocation.

Where is webView declared, and how?

I take it you’re using ARC?

Update

Ok, I think I see the problem now.

You’re declaring your troublesome object as a local variable:

WebViewJavascriptBridge* bridge = ...

This means that once that function scope is left (i.e. the end of the function is reached), that object can be deallocated, and is.

Try declaring a strong property for your bridge instead. Then it won’t be deallocated prematurely. For example:

@property (strong, nonatomic) WebViewJavascriptBridge* bridge;

Then later in your code:

self.bridge = ... // the creation code

And once you’ve finished with your object, don’t forget to deallocate it:

self.bridge = nil;
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement