我從存儲在apps目錄中的本地文件處理我的應用程序的主線程中的javascript。我檢查我正在執行的js函數的開始和結束標記以及函數是否包含變量。
希望這可以給你一些你的問題的好主意。您也可以在js中發出警報,以查看運行應用程序時值是否正確發佈(我確信您已經想到了這一點,但值得一提)。快樂編碼!我希望這有幫助!
在.h文件
定義:
NSMutableString *processedCommand;
NSArray *commandArguments;
在.m文件:
// 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中),但沒有參數。難住了。 – 2012-04-18 17:23:57
你是否嘗試過改變get(NSString *)輸入來獲取:(id)輸入並設置斷點(而不是NSLog),以查看是否有任何事情發生? – JoeCortopassi 2012-05-01 22:04:31
我們可以請看看一些JavaScript代碼嗎? – Sam 2012-05-02 14:04:47