1

I read somewhere to read javascript console messages using the

- (void)webView:(WebView *)webView addMessageToConsole:(NSDictionary *)message

delegate method from the UIDelegate. But how/where do I need to set the delegate of the WebView (not the UIWebView) to my custom delegate?

I know Apple doesn't allow this in the AppStore, but I just want to implement this for debugging purposes.

What I tried so far:

- (void)webView:(id)sender didClearWindowObject:(id)windowObject forFrame:(WebFrame*)frame
{
    [webView setUIDelegate:[[MyCustomUIDelegate alloc] init]];
}

and

-(void) webView:(id)webView windowScriptObjectAvailable:(id)newWindowScriptObject 
{    
    [webView setUIDelegate:[[MyCustomUIDelegate alloc] init]];
}
pmdj
  • 22,018
  • 3
  • 52
  • 103
Thizzer
  • 16,153
  • 28
  • 98
  • 139
  • UIDelegate is part of the MacOS SDK, not iOS. Are you trying to do this on MacOS or iOS? – MyztikJenz Jan 19 '12 at 01:41
  • Owh.. I was trying to do this in iOS, to get the log messages the UIDelegate receives, is there a way to do this on iOS? I tried scriptdebugdelegate but that doesn't give console.log messages. – Thizzer Jan 19 '12 at 08:06

1 Answers1

4

This post may help you:

How can my iPhone Objective-C code get notified of Javascript errors in a UIWebView?

You can hook UIWebView control to hidden WebKit framework and get all exceptions, executed functions and similar.

Another way is posted here: Inject javascript code into the response that invoke to a objecie-c function.

NSString* path = [[NSBundle mainBundle] pathForResource:@"script" 
                                                 ofType:@"js"];
NSString* content = [NSString stringWithContentsOfFile:path
                                              encoding:NSUTF8StringEncoding
                                                 error:NULL];

[sender stringByEvaluatingJavaScriptFromString:content];

for exapmle the javascript code may be like:

console = new Object();
console.log = function(log) {
    var iframe = document.createElement("IFRAME");
    iframe.setAttribute("src", "ios-log:#iOS#" + log);
    document.documentElement.appendChild(iframe);
    iframe.parentNode.removeChild(iframe);
    iframe = null;    
}
console.debug = console.log;
console.info = console.log;
console.warn = console.log;
console.error = console.log;

and the objective-c code like:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString *requestString = [[[request URL] absoluteString] stringByReplacingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    //NSLog(requestString);

    NSLog(@"Request: %@", requestString);

    if ([requestString hasPrefix:@"ios-log:"]) {
        NSString* logString = [[requestString componentsSeparatedByString:@":#iOS#"] objectAtIndex:1];
        NSLog(@"UIWebView console: %@", logString);
        return NO;
    }

    return YES;
}
Community
  • 1
  • 1
Esepakuto
  • 1,332
  • 9
  • 12