简体   繁体   中英

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

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

Maybe you should change to use this library.

SDBridgeOC

 self.bridge.consolePipeBlock = ^(id  _Nonnull water) {
        NSLog(@"Next line is javascript console.log------>>>>");
        NSLog(@"%@",water);
    };

This can easy to get javascript console.log.

Also have Swift language Version.

SDBridgeSwift

bridge.consolePipeClosure = { water in
         guard let jsConsoleLog = water else {
             print("Javascript console.log give native is nil!")
              return
         }
         print("Next line is Javascript console.log----->>>>>>>")
         print(jsConsoleLog)
      }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM