Created
November 2, 2012 15:22
-
-
Save danpalmer/4001967 to your computer and use it in GitHub Desktop.
Rough console.log functionality from within a WebView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console = { | |
log: function(msg) { | |
if (typeof msg !== 'string') { | |
msg = JSON.stringify(msg); | |
} | |
if (msg) { | |
_logger.consoleLog_(msg); | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation ConsoleLogger | |
- (void)consoleLog:(NSString *)logString { | |
NSLog(@"%@", logString); | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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