Skip to content

Instantly share code, notes, and snippets.

@danpalmer
Created November 2, 2012 15:22
Show Gist options
  • Save danpalmer/4001967 to your computer and use it in GitHub Desktop.
Save danpalmer/4001967 to your computer and use it in GitHub Desktop.
Rough console.log functionality from within a WebView
console = {
log: function(msg) {
if (typeof msg !== 'string') {
msg = JSON.stringify(msg);
}
if (msg) {
_logger.consoleLog_(msg);
}
}
};
@implementation ConsoleLogger
- (void)consoleLog:(NSString *)logString {
NSLog(@"%@", logString);
}
@end
@interface ScriptingEngine ()
@property (retain) ConsoleLogger *logger;
@end
@implementation ScriptingEngine
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame {
if (frame == [frame findFrameNamed:@"_top"]) {
// Initialisation
logger = [[ConsoleLogger alloc] init];
[[self.webview windowScriptObject] setValue:logger forKey:@"_logger"];
NSString *apiScript = [[NSString alloc] initWithContentsOfFile:@"API.js" encoding:NSUTF8StringEncoding error:NULL];
[[self.webview windowScriptObject] evaluateWebScript:apiScript];
// more initialisation
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment