People,
I managed to get around this by sending HTTP requests to the iOS client to process and return the results to UIAlertView. Please note that all modifications to iOS code are wrapped in #if DEBUG conditional compilation directives.
First, configure the client to send notifications when the device is shaken. Read this post for more information .
Next, in your applet for the main iOS app, add this code:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceShakenShowDebug:) name:@"DeviceShaken" object:nil];
Then add a method that looks something like this:
- (void) deviceShakenShowDebug:(id)sender { if (!self.textFieldEnterDebugArgs) { self.textFieldEnterDebugArgs = [[[UITextField alloc] initWithFrame:CGRectMake(0, 0, 260.0, 25.0)] autorelease]; self.textFieldEnterDebugArgs.accessibilityLabel = @"AlertDebugArgsField"; self.textFieldEnterDebugArgs.isAccessibilityElement = YES; [self.textFieldEnterDebugArgs setBackgroundColor:[UIColor whiteColor]]; [self.tabBarController.selectedViewController.view addSubview:self.textFieldEnterDebugArgs]; [self.tabBarController.selectedViewController.view bringSubviewToFront:self.textFieldEnterDebugArgs]; } else { if ([self.textFieldEnterDebugArgs.text length] > 0) { if ([self.textFieldEnterDebugArgs.text hasPrefix:@"http://"]) { [self doDebugHttpRequest:self.textFieldEnterDebugArgs.text]; } } } } - (void)requestDidFinishLoad:(TTURLRequest*)request { NSString *response = [[[NSString alloc] initWithData:((TTURLDataResponse*)request.response).data encoding:NSUTF8StringEncoding] autorelease]; UIAlertView *resultAlert = [[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Request Loaded",@"") message:response delegate:nil cancelButtonTitle:NSLocalizedString(@"OK",@"") otherButtonTitles:nil] autorelease]; resultAlert.accessibilityLabel = @"AlertDebugResult"; [resultAlert show]; }
This code will add a UITextField to the controller of the topmost view after shaking, hit right above any navigation bar or other user interface element. The UIAutomation, or user, can manually enter the URL into this UITextField. When you shake the device again, if the text starts with "http", it will issue an HTTP request in code (reading exercise for doDebugHttpRequest).
Then, in my UIAutomation JavaScript file, I defined the following two functions:
function httpGet(url, delayInSec) { if (!delayInSec) delay = 1; var alertDebugResultSeen = false; var httpResponseValue = null; UIATarget.onAlert = function onAlert(alert) { httpResponseValue = alert.staticTexts().toArray()[1].name(); alert.buttons()[0].tap(); alertDebugResultSeen = true; } var target = UIATarget.localTarget(); var application = target.frontMostApp(); target.shake();
Now, in my javascript file, I can call
httpGet('http://localhost:3000/do_something')
and it will execute an HTTP request. If I want to return JSON data from the server, I call
var jsonResponse = httpGetJSON('http://localhost:3000/do_something')
If I know it will be a long call, I call
var jsonResponse = httpGetJSON('http://localhost:3000/do_something', 10 )
I have been using this approach successfully for several weeks now.