I had to detect if user has touched on the iPhone screen. So, I created a class named “CustomApplication” in my project (subclassing UIApplication) and then, I modified my main.m to look like this:
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, @"CustomApplication",nil);
[pool release];
return retVal;
This class “CustomApplication.m” contains a method as follows:
- (void)sendEvent:(UIEvent *)event {
[super sendEvent:event];
[MyUtility showAlertWithTitle:@"Alert!!!!" message:@"Session Expired!!!!"]; // showing an alert here
}
The Method showAlertWithTitle looks like this:
+ (void) showAlertWithTitle:(NSString *)aTitle message:(NSString *)aMessage
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:aTitle message:aMessage delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show]; // Line causing problem in iOS 5 - base sdk 5.0
[alertView release];
}
Everythings working fine in iOS 4.2, but on iOS 5.0 the app is crashing on touching the screen (when sendEvent:event method gets called). When I debugged the code, I found that the problem is in [alertView show]; line. In iOS 5, what is happening is when this particular line ([alertView show];) gets executed, it again calls sendEvent method of CustomApplication and this method calls showAlertWithTitle: method of MyUtility which in turn again calls sendEvent method and hence, the code is entering into infinite loop. I dont know the solution. If someone has faced this weired thing, then please tell me what should I write so that when alert is being showed the sendEvent method doesn’t get called?
This may not seem like the best approach, but It may work. Have you tried simply including a static boolean variable in your code, so that the function does not get called again?
Somewhere up
And then the sendEvent