For iPhone Simulator iOS4.0+, NSInvocation doesn't handle exceptions well. I came across a workaround to use objc_msgSend. When I tried it for the below invocation as objc_msgSend(target_, [invocation selector]) and commenting out [invocation invoke] under createResponseFromInvocation() hangs. I tried different ways of calling obj_msgSend and didn't work.
- (NSInvocation *)createInvocationWithSelector:(SEL)selector
signature:(NSMethodSignature *)method
arguments:(NSDictionary *)arguments {
NSInvocation *invocation
= [NSInvocation invocationWithMethodSignature:method];
[invocation setSelector:selector];
[invocation setTarget:target_];
if (arguments != nil) {
if ([method numberOfArguments] > 2) {
[invocation setArgument:&arguments atIndex:2];
}
}
return invocation;
}
// Invoke the given invocation and create a response from it.
- (WDResponse *)createResponseFromInvocation:(NSInvocation *)invocation {
WDResponse *response;
@try {
[invocation invoke];
if ([[invocation methodSignature] methodReturnLength] == 0) {
response = [WDResponse responseWithValue:nil];
} else {
id result;
[invocation getReturnValue:&result];
response = [WDResponse responseWithValue:result];
}
}
@catch (NSException * e) {
NSLog(@"Method invocation error: %@", e);
response = [WDResponse responseWithError:e];
}
return response;
}