Created
February 12, 2012 02:41
-
-
Save hamrickdavid/1805900 to your computer and use it in GitHub Desktop.
Using UIWebview as a Javascript Interpreter
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
JavascriptContext* ctx = [[JavascriptContext alloc] init]; | |
[ctx evaluateJS:@"2+2;"]; //Results in 4 | |
[ctx evalateJS:someScriptStoredInBundle]; | |
[ctx release]; |
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 JavascriptContext : NSObject | |
- (NSString*)evaluateJS:(NSString*)js; | |
@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 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