Running interpreted code in an iPhone app
3.3.2 An Application may not itself install or launch other executable code by any means, including without limitation through the use of a plug-in architecture, calling other frameworks, other APIs or otherwise. No interpreted code may be downloaded and used in an Application except for code that is interpreted and run by Apple’s Published APIs and built-in interpreter(s).
An iPhone app we are developing will at some point have downloadable content, where a user can download levels. In order to implement the grading algorithm, we were looking into dumping some Lua script with each level and having an interpreter. The quoted statement above disallows this, but we've found an alternate solution. This is nothing new by any means, but it's not always the most obvious. UIWebView#stringByEvaluatingJavaScriptFromString can parse JavaScript, and therefore is exempted by the "built-in interpreter" clause.
in Utils.m
... // in initialization
webView = [[UIWebView alloc] init];
...
-(CGFloat) evaluate:(NSString*)script{
NSString *eval = [webView stringByEvaluatingJavaScriptFromString:script];
return [eval floatValue];
}
in GameScreen.m
... // in grading method
NSString* script = [[NSString alloc] initWithFormat:
@"var xi=%f, yi=%f, xf=%f, yf=%f, dx=xf-xi, dy=yf-yi; Math.sqrt(dx*dx + dy*dy)",
xi, yi, xf, yf];
CGFloat eval = [[Utils instance] evaluate:[script autorelease]];
...
This returns the distance between (xi, yi) and (xf, yf), and if we wanted another level to calculate the Manhattan distance or ignore the y axis altogether, we can simply load a different script with the level and we're set. All without getting flogged by the Apple gestapo, whew!