Skip to content

Instantly share code, notes, and snippets.

@hamrickdavid
Created February 12, 2012 02:41
Show Gist options
  • Save hamrickdavid/1805900 to your computer and use it in GitHub Desktop.
Save hamrickdavid/1805900 to your computer and use it in GitHub Desktop.
Using UIWebview as a Javascript Interpreter
JavascriptContext* ctx = [[JavascriptContext alloc] init];
[ctx evaluateJS:@"2+2;"]; //Results in 4
[ctx evalateJS:someScriptStoredInBundle];
[ctx release];
@interface JavascriptContext : NSObject
- (NSString*)evaluateJS:(NSString*)js;
@end
@interface JavascriptContext ()
@property (nonatomic,retain) UIWebView* webview;
@end
@implementation JavascriptContext
@synthesize webview = _webview;
- (void)dealloc
{
[_webview release], _webview = nil;
[super dealloc];
}
- (id)init
{
self = [super init];
self.webview = [[[UIWebView alloc] init] autorelease];
return self;
}
- (NSString*)evaluateJS:(NSString*)js
{
return [self.webview stringByEvaluatingJavaScriptFromString:js];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment