appsディレクトリに格納されているローカルファイルから、自分のアプリケーションのメインスレッドでjavascriptを処理します。実行しているjs関数の先頭と末尾のトークンと、その関数に変数が含まれているかどうかを調べます。
これは、問題の良いアイデアを提供してくれることを願っています。また、jsで警告を出して、アプリケーションを実行するときに値が正しくポストされているかどうかを確認することもできます(私はあなたがすでにそれを考えていると確信していますが、言及する価値があります)。私はこれが助けて欲しい! .hファイルで
は定義:.mファイルで
NSMutableString *processedCommand;
NSArray *commandArguments;
:興味深いことに
// tokens
#define kOpenToken @"<%%"
#define kCloseToken @"%%>"
// this will throw
-(void)executeJScriptCommand:(NSString *)aCommand {
[self performSelectorOnMainThread:@selector(executeThisCommand:) withObject:aCommand waitUntilDone:YES];
}
// this will throw
-(NSString *)executeCommand:(NSString *)command {
NSString *aCommand = [[[command stringByReplacingOccurrencesOfString:kOpenToken withString:@""]
stringByReplacingOccurrencesOfString:kCloseToken withString:@""]
stringByTrimmingLeadingAndTrailingWhitespaces];
if ([aCommand hasPrefix:@"="])
{
// variable. get value
[self getVariableFromCommand:aCommand];
}
else {
[self executeThisCommand:aCommand];
}
NSString *returnValue = [NSString stringWithString:processedCommand];
self.processedCommand = nil;
self.commandArguments = nil;
return returnValue;
}
-(void)executeThisCommand:(NSString *)aCommand {
BOOL hasError = NO;
// clear result
self.processedCommand = nil;
self.commandArguments = nil;
BOOL isFromJS = NO;
NSString *function = nil;
NSMutableArray *commandParts = nil;
@try {
// first, break the command into its parts and extract the function that needs to be called, and the (optional) arguments
commandParts = [[NSMutableArray alloc] initWithArray:[aCommand componentsSeparatedByString:@":"]];
if ([[[commandParts objectAtIndex:0] lowercaseString] isEqualToString:@"js-call"]) {
isFromJS = YES;
[commandParts removeObjectAtIndex:0];
}
// get our function, arguments
function = [[commandParts objectAtIndex:0] retain];
[commandParts removeObjectAtIndex:0];
if ([commandParts count] > 0){
if (isFromJS == YES) {
NSString *arguments = [[commandParts objectAtIndex:0] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
if ([arguments length] > 0) {
self.commandArguments = [arguments JSONValue];
}
}
else {
self.commandArguments = [NSArray arrayWithArray:commandParts];
}
}
// build invoke
SEL sel = NSSelectorFromString(function);
if ([self respondsToSelector:sel]) {
[self performSelectorOnMainThread:sel withObject:nil waitUntilDone:YES];
// using invocation causes a SIGABORT because the try/catch block was not catching the exception.
// using perform selector fixed the problem (i.e., the try/catch block now correctly catches the exception, as expected)
}
else {
[appDelegate buildNewExceptionWithName:@"" andMessage:[NSString stringWithFormat:@"Object does not respond to selector %@", function]];
}
}
@catch (NSException * e) {
hasError = YES;
[self updateErrorMessage:[NSString stringWithFormat:@"Error processing command %@: %@", aCommand, [e reason]]];
}
@finally {
[function release];
[commandParts release];
}
if (hasError == YES) {
[appDelegate buildNewExceptionWithName:@"executeThisCommand" andMessage:self.errorMessage];
}
}
// this can return nil
-(NSString *)getQueryStringValue:(NSString *)name {
NSString *returnValue = nil;
if (queryString != nil) {
returnValue = [queryString objectForKey:[name lowercaseString]];
}
return returnValue;
}
、それは私はWebViewのでロードされたページ内のスクリプトからこれらのメソッドを呼び出すときのように見え、物事は大丈夫です。引数が落とされる開発者ツールを使用してJavaScriptメソッドを呼び出すときだけです。ここでも、メソッドが呼び出され、ブレークポイントは(Xcodeでは)ヒットしますが、引数はありません。困った。 –
get:(NSString *)入力を取得して、(id)の入力とブレークポイント(NSLogではなく)を設定して何かが流れているかどうかを確認しようとしましたか? – JoeCortopassi
JavaScriptのコードもご覧ください。 – Sam