Showing posts with label UIApplication. Show all posts
Showing posts with label UIApplication. Show all posts

Wednesday, May 29, 2013

UIApplication unregisterForRemoteNotifications example in Objective C (iOS).

UIApplication unregisterForRemoteNotifications


Unregister for notifications received from Apple Push Service.

- (void)unregisterForRemoteNotifications

Discussion of [UIApplication unregisterForRemoteNotifications]
You should call this method in rare circumstances only, such as when a new version of the application drops support for remote notifications. Users can temporarily prevent applications from receiving remote notifications through the Notifications section of the Settings application. Applications unregistered through this method can always re-register.


UIApplication unregisterForRemoteNotifications example.
[[UIApplication sharedApplication] unregisterForRemoteNotifications];

Example of [UIApplication unregisterForRemoteNotifications].
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

[[UIApplication sharedApplication] unregisterForRemoteNotifications];

UIApplication unregisterForRemoteNotifications example.
Bool isOn = [[NSUserDefaults standardUserDefaults] boolForKey:@"SwitchKey"];

if (isOn) {
    NSLog(@"ok");  
}


if (!isOn) {
    [[[UIApplication sharedApplication] delegate] unregisterForRemoteNotifications];  
}

End of UIApplication unregisterForRemoteNotifications example article.

UIApplication supportedInterfaceOrientationsForWindow example in Objective C (iOS).

UIApplication supportedInterfaceOrientationsForWindow


Returns the default set of interface orientations to use for the view controllers in the specified window.

- (NSUInteger)supportedInterfaceOrientationsForWindow:(UIWindow *)window

Parameters
window
The window whose default interface orientations you want to retrieve.

Return Value of [UIApplication supportedInterfaceOrientationsForWindow]
A bit mask specifying which orientations are supported. See “UIInterfaceOrientationMask” for valid bit-mask values. The value returned by this method must not be 0.


Discussion of [UIApplication supportedInterfaceOrientationsForWindow]
This method returns the default interface orientations for the app. These orientations are used only for view controllers that do not specify their own. If your app delegate implements the application:supportedInterfaceOrientationsForWindow: method, the system does not call this method.

The default implementation of this method returns the app’s default set of supported interface orientations, as defined in the UISupportedInterfaceOrientations key of the Info.plist file. If the file does not contain that key, this method returns all interface orientations for the iPad idiom and returns all interface orientations except the portrait upside-down orientation for the iPhone idiom.


UIApplication supportedInterfaceOrientationsForWindow example.
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {

return UIInterfaceOrientationLandscapeRight;

}

- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)w {

return (NSUInteger)[application supportedInterfaceOrientationsForWindow:w] | (1<
}

Example of [UIApplication supportedInterfaceOrientationsForWindow].
- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {  

    return self.fullScreenVideoIsPlaying ?
        UIInterfaceOrientationMaskAllButUpsideDown :
        UIInterfaceOrientationMaskPortrait;
}

UIApplication supportedInterfaceOrientationsForWindow example.
- (NSUInteger)application:(UIApplication*)application
supportedInterfaceOrientationsForWindow:(UIWindow*)window
{
    if(self.shouldRotate ) //shouldRotate is my flag
    {
        self.shouldRotate = NO;
        return (UIInterfaceOrientationMaskAll);
    }
    return (UIInterfaceOrientationMaskPortrait);
}

End of UIApplication supportedInterfaceOrientationsForWindow example article.

UIApplication setStatusBarStyle animated example in Objective C (iOS).

UIApplication setStatusBarStyle animated


Sets the style of the status bar, optionally animating the transition to the new style.

- (void)setStatusBarStyle:(UIStatusBarStyle)statusBarStyle animated:(BOOL)animated

Parameters
statusBarStyle
A constant that specifies a style for the status bar. See the descriptions of the constants in UIStatusBarStyle for details.
animated
YES if the transition to the new style should be animated; otherwise NO .

Discussion of [UIApplication setStatusBarStyle animated]
The animation slides the status bar out toward the top of the interface.


UIApplication setStatusBarStyle animated example.
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent
                                            animated:YES];

Example of [UIApplication setStatusBarStyle animated].
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];
}

UIApplication setStatusBarStyle animated example.
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // don't forget to set navigationBar.translucent to YES
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
    [UINavigationBar setAnimationDuration:3.0];

    [UINavigationBar beginAnimations:@"" context:nil];
    [[UIApplication sharedApplication] setStatusBarHidden:!([UIApplication sharedApplication].statusBarHidden) withAnimation:NO];
    if ([UIApplication sharedApplication].isStatusBarHidden)
        [self.navigationController.navigationBar setAlpha:0.0];
    else [self.navigationController.navigationBar setAlpha:1.0];
    [UINavigationBar commitAnimations];
}

End of UIApplication setStatusBarStyle animated example article.

UIApplication setStatusBarStyle example in Objective C (iOS).

UIApplication setStatusBarStyle


Sets the style of the status bar, optionally animating the transition to the new style.

- (void)setStatusBarStyle:(UIStatusBarStyle)statusBarStyle animated:(BOOL)animated

Parameters
statusBarStyle
A constant that specifies a style for the status bar. See the descriptions of the constants in UIStatusBarStyle for details.
animated
YES if the transition to the new style should be animated; otherwise NO .

Discussion of [UIApplication setStatusBarStyle]
The animation slides the status bar out toward the top of the interface.


UIApplication setStatusBarStyle example.
[[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent
                                            animated:YES];

Example of [UIApplication setStatusBarStyle].
-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent animated:YES];
}

UIApplication setStatusBarStyle example.
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // don't forget to set navigationBar.translucent to YES
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackTranslucent];
    [UINavigationBar setAnimationDuration:3.0];

    [UINavigationBar beginAnimations:@"" context:nil];
    [[UIApplication sharedApplication] setStatusBarHidden:!([UIApplication sharedApplication].statusBarHidden) withAnimation:NO];
    if ([UIApplication sharedApplication].isStatusBarHidden)
        [self.navigationController.navigationBar setAlpha:0.0];
    else [self.navigationController.navigationBar setAlpha:1.0];
    [UINavigationBar commitAnimations];
}

End of UIApplication setStatusBarStyle example article.

UIApplication setStatusBarOrientation animated example in Objective C (iOS).

UIApplication setStatusBarOrientation animated


Sets the application's status bar to the specified orientation, optionally animating the transition.

- (void)setStatusBarOrientation:(UIInterfaceOrientation)interfaceOrientation animated:(BOOL)animated

Parameters
interfaceOrientation
A specific orientation of the status bar. See UIInterfaceOrientation for details. The default value is UIInterfaceOrientationPortrait.
animated
YES if the transition to the new orientation should be animated; NO if it should be immediate, without animation.

Discussion of [UIApplication setStatusBarOrientation animated]
Calling this method changes the value of the statusBarOrientation property and rotates the status bar, animating the transition if animated is YES . If your application has rotatable window content, however, you should not arbitrarily set status-bar orientation using this method. The status-bar orientation set by this method does not change if the device changes orientation.


UIApplication setStatusBarOrientation animated example.
//will rotate status bar
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];


//will re-rotate view according to statusbar
UIViewController *c = [[UIViewController alloc]init];
[self presentModalViewController:c animated:NO];
[self dismissModalViewControllerAnimated:NO];
[c release];

Example of [UIApplication setStatusBarOrientation animated].
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

UIApplication setStatusBarOrientation animated example.
-(void)rotateInterfaceToOrientation:(UIDeviceOrientation)orientation{

    CGRect bounds = [[ UIScreen mainScreen ] bounds ];
    CGAffineTransform t;
    CGFloat r = 0;
    switch ( orientation ) {
        case UIDeviceOrientationLandscapeRight:
            r = -(M_PI / 2);
            break;
        case UIDeviceOrientationLandscapeLeft:
            r  = M_PI / 2;
            break;
    }
    if( r != 0 ){
        CGSize sz = bounds.size;
        bounds.size.width = sz.height;
        bounds.size.height = sz.width;
    }
    t = CGAffineTransformMakeRotation( r );

    UIApplication *application = [ UIApplication sharedApplication ];

    [ UIView beginAnimations:@"InterfaceOrientation" context: nil ];
    [ UIView setAnimationDuration: [ application statusBarOrientationAnimationDuration ] ];
    self.view.transform = t;
    self.view.bounds = bounds;
    [ UIView commitAnimations ];

    [ application setStatusBarOrientation: orientation animated: YES ];    
}

End of UIApplication setStatusBarOrientation animated example article.

UIApplication setStatusBarOrientation example in Objective C (iOS).

UIApplication setStatusBarOrientation


Sets the application's status bar to the specified orientation, optionally animating the transition.

- (void)setStatusBarOrientation:(UIInterfaceOrientation)interfaceOrientation animated:(BOOL)animated

Parameters
interfaceOrientation
A specific orientation of the status bar. See UIInterfaceOrientation for details. The default value is UIInterfaceOrientationPortrait.
animated
YES if the transition to the new orientation should be animated; NO if it should be immediate, without animation.

Discussion of [UIApplication setStatusBarOrientation]
Calling this method changes the value of the statusBarOrientation property and rotates the status bar, animating the transition if animated is YES . If your application has rotatable window content, however, you should not arbitrarily set status-bar orientation using this method. The status-bar orientation set by this method does not change if the device changes orientation.


UIApplication setStatusBarOrientation example.
//will rotate status bar
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];


//will re-rotate view according to statusbar
UIViewController *c = [[UIViewController alloc]init];
[self presentModalViewController:c animated:NO];
[self dismissModalViewControllerAnimated:NO];
[c release];

Example of [UIApplication setStatusBarOrientation].
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight];
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];

UIApplication setStatusBarOrientation example.
-(void)rotateInterfaceToOrientation:(UIDeviceOrientation)orientation{

    CGRect bounds = [[ UIScreen mainScreen ] bounds ];
    CGAffineTransform t;
    CGFloat r = 0;
    switch ( orientation ) {
        case UIDeviceOrientationLandscapeRight:
            r = -(M_PI / 2);
            break;
        case UIDeviceOrientationLandscapeLeft:
            r  = M_PI / 2;
            break;
    }
    if( r != 0 ){
        CGSize sz = bounds.size;
        bounds.size.width = sz.height;
        bounds.size.height = sz.width;
    }
    t = CGAffineTransformMakeRotation( r );

    UIApplication *application = [ UIApplication sharedApplication ];

    [ UIView beginAnimations:@"InterfaceOrientation" context: nil ];
    [ UIView setAnimationDuration: [ application statusBarOrientationAnimationDuration ] ];
    self.view.transform = t;
    self.view.bounds = bounds;
    [ UIView commitAnimations ];

    [ application setStatusBarOrientation: orientation animated: YES ];    
}

End of UIApplication setStatusBarOrientation example article.

UIApplication setStatusBarHidden withAnimation example in Objective C (iOS).

UIApplication setStatusBarHidden withAnimation


Hides or shows the status bar, optionally animating the transition.

- (void)setStatusBarHidden:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation

Parameters
hidden
YES to hide the status bar, NO to show the status bar.
animation
A constant that indicates whether there should be an animation and, if one is requested, whether it should fade the status bar in or out or whether it should slide the status bar in or out.

Discussion of [UIApplication setStatusBarHidden withAnimation]
See the descriptions of the constants of the UIStatusBarAnimation type for more information.


UIApplication setStatusBarHidden withAnimation example.
if([[UIApplication sharedApplication] respondsToSelector:@selector(setStatusBarHidden: withAnimation:)])
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
else
    [[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];

Example of [UIApplication setStatusBarHidden withAnimation].
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
#if __IPHONE_OS_VERSION_MIN_REQUIRED > 30100
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:YES];
#else
    [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
#endif
#endif

UIApplication setStatusBarHidden withAnimation example.
if([[UIApplication sharedApplication] respondsToSelector:@selector(setStatusBarHidden: withAnimation:)])
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}
else if([[UIApplication sharedApplication] respondsToSelector:@selector(setStatusBarHidden: animated:)])
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];
}
else if([[UIApplication sharedApplication] respondsToSelector:@selector(setStatusBarHidden:)])
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
}

End of UIApplication setStatusBarHidden withAnimation example article.

UIApplication setStatusBarHidden example in Objective C (iOS).

UIApplication setStatusBarHidden

Hides or shows the status bar, optionally animating the transition.

- (void)setStatusBarHidden:(BOOL)hidden withAnimation:(UIStatusBarAnimation)animation

Parameters of [UIApplication setStatusBarHidden]
hidden
YES to hide the status bar, NO to show the status bar.
animation
A constant that indicates whether there should be an animation and, if one is requested, whether it should fade the status bar in or out or whether it should slide the status bar in or out.

Discussion of [UIApplication setStatusBarHidden]
See the descriptions of the constants of the UIStatusBarAnimation type for more information.


UIApplication setStatusBarHidden example.
if([[UIApplication sharedApplication] respondsToSelector:@selector(setStatusBarHidden: withAnimation:)])
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
else
    [[UIApplication sharedApplication] setStatusBarHidden:NO animated:YES];

Example of [UIApplication setStatusBarHidden].
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
#if __IPHONE_OS_VERSION_MIN_REQUIRED > 30100
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:YES];
#else
    [[UIApplication sharedApplication] setStatusBarHidden:YES animated:YES];
#endif
#endif

UIApplication setStatusBarHidden example.
if([[UIApplication sharedApplication] respondsToSelector:@selector(setStatusBarHidden: withAnimation:)])
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}
else if([[UIApplication sharedApplication] respondsToSelector:@selector(setStatusBarHidden: animated:)])
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO animated:NO];
}
else if([[UIApplication sharedApplication] respondsToSelector:@selector(setStatusBarHidden:)])
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
}

End of UIApplication setStatusBarHidden example article.

Saturday, May 25, 2013

UIApplication setNewsstandIconImage example in Objective C (iOS)


[UIApplication setNewsstandIconImage]

Sets the icon of a Newsstand application to an image depicting the current issue of a publication.
- (void)setNewsstandIconImage:(UIImage *)image
Parameters of [UIApplication setNewsstandIconImage]
image
An image to use as the icon of a Newsstand application. Pass nil to clear the currently set image and revert to the icon stored in the application bundle.
Discussion of [UIApplication setNewsstandIconImage]
The Newsstand application icon is typically downloaded from the application’s server for each issue.
Example of [UIApplication setNewsstandIconImage]
UIApplication *app = [UIApplication sharedApplication];
[app setNewsstandIconImage:[UIImage imageWithContentsOfFile:file]];
Example of [UIApplication setNewsstandIconImage]
//Finally, if the connection succeeds in downloading the request, the delegate receives the connectionDidFinishLoading: message
    - (void)connectionDidFinishDownloading:(NSURLConnection*)connection destinationURL:(NSURL*)destinationURL {

//[webView loadRequest:request];//if you wanted load the website into the webview

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);


   NSString *documentsDirectory = [paths objectAtIndex:0];
    [self saveFile:@"NSIcon" ofType:@"png" fromURL:@"http://website.com/NSIcon.png" inDirectory:documentsDirectory];



UIImage * newsstandImage = [self loadImage:@"NSIcon" ofType:@"png" inDirectory:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];

   UIApplication *app = [UIApplication sharedApplication];
    [app setNewsstandIconImage:newsstandImage];

    NSLog(@"downloading...");

}

UIApplication setKeepAliveTimeout handler example in Objective C (iOS)


[UIApplication setKeepAliveTimeout handler]

Configures a periodic handler for VoIP applications.
- (BOOL)setKeepAliveTimeout:(NSTimeInterval)timeout handler:(void (^)(void))keepAliveHandler
Parameters of [UIApplication setKeepAliveTimeout handler]
timeout
The maximum interval (measured in seconds) at which your application should be woken up to check its VoIP connection. The minimum acceptable timeout value is 600 seconds.
keepAliveHandler
A block that performs the tasks needed to maintain your VoIP network connection. Setting this parameter to nil releases the current handler block and prevents UIKit from scheduling the next wake.
Return Value
YES if the handler was installed or NO if it was not.
Discussion of [UIApplication setKeepAliveTimeout handler]
A voice-over-IP (VoIP) application can use this method to install a handler whose job is to maintain the application’s network connection with a VoIP server. This handler is guaranteed to be called before the specified timeout value but may be called at a slightly different time interval in order to better align execution of your handler with other system tasks, and thereby save power. Your handler has a maximum of 10 seconds to perform any needed tasks and exit. If it does not exit before time expires, the application is suspended.[UIApplication setKeepAliveTimeout handler]
Timeout values and handlers are not persisted between application launches. Therefore, if your application is terminated for any reason, you must reinstall the handler during the next launch cycle.
For calls to this method to succeed, the application must have the voip value in the array associated with the UIBackgroundModes key in its Info.plist file. Calling this method replaces the previously installed handler and timeout values, if any.
Example of [UIApplication setKeepAliveTimeout handler]
BOOL backgroundAccepted = [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{ [self backgroundHandler]; }];
 if (backgroundAccepted)
 {
      NSLog(@"VOIP backgrounding accepted");
 }
Example of [UIApplication setKeepAliveTimeout handler]
BOOL scheduled = [app setKeepAliveTimeout:pingTimeout handler:^{ // Schedule processing after some time interval      

  SchedulePing(0);
}
Another article about the setKeepAliveTimeout.
Example of [UIApplication setKeepAliveTimeout handler]
- (void)applicationDidEnterBackground:(UIApplication *)application {

    // This is where you can do your X Minutes, if >= 10Minutes is okay.
    BOOL backgroundAccepted = [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{ [self backgroundHandler]; }];
    if (backgroundAccepted)
    {
        NSLog(@"VOIP backgrounding accepted");
    }
}

UIApplication setKeepAliveTimeout example in Objective C (iOS)


setKeepAliveTimeout: handler:

Configures a periodic handler for VoIP applications.
- (BOOL)setKeepAliveTimeout:(NSTimeInterval)timeout handler:(void (^)(void))keepAliveHandler
Parameters of [UIApplication setKeepAliveTimeout]
timeout
The maximum interval (measured in seconds) at which your application should be woken up to check its VoIP connection. The minimum acceptable timeout value is 600 seconds.
keepAliveHandler
A block that performs the tasks needed to maintain your VoIP network connection. Setting this parameter to nil releases the current handler block and prevents UIKit from scheduling the next wake.
Return Value
YES if the handler was installed or NO if it was not.
Discussion of [UIApplication setKeepAliveTimeout]
A voice-over-IP (VoIP) application can use this method to install a handler whose job is to maintain the application’s network connection with a VoIP server. This handler is guaranteed to be called before the specified timeout value but may be called at a slightly different time interval in order to better align execution of your handler with other system tasks, and thereby save power. Your handler has a maximum of 10 seconds to perform any needed tasks and exit. If it does not exit before time expires, the application is suspended.[UIApplication setKeepAliveTimeout]
Timeout values and handlers are not persisted between application launches. Therefore, if your application is terminated for any reason, you must reinstall the handler during the next launch cycle.
For calls to this method to succeed, the application must have the voip value in the array associated with the UIBackgroundModes key in its Info.plist file. Calling this method replaces the previously installed handler and timeout values, if any.
Example of [UIApplication setKeepAliveTimeout]
BOOL backgroundAccepted = [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{ [self backgroundHandler]; }];
 if (backgroundAccepted)
 {
      NSLog(@"VOIP backgrounding accepted");
 }
Example of [UIApplication setKeepAliveTimeout]
BOOL scheduled = [app setKeepAliveTimeout:pingTimeout handler:^{ // Schedule processing after some time interval      

  SchedulePing(0);
}
Example of [UIApplication setKeepAliveTimeout]
- (void)applicationDidEnterBackground:(UIApplication *)application {

    // This is where you can do your X Minutes, if >= 10Minutes is okay.
    BOOL backgroundAccepted = [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler:^{ [self backgroundHandler]; }];
    if (backgroundAccepted)
    {
        NSLog(@"VOIP backgrounding accepted");
    }
}

UIApplication sendEvent example in Objective C (iOS)


[UIApplication sendEvent]

Dispatches an event to the appropriate responder objects in the application.
- (void)sendEvent:(UIEvent *)event
Parameters
event
UIEvent object encapsulating the information about an event, including the touches involved.
Discussion of [UIApplication sendEvent]
Subclasses may override this method to intercept incoming events. Any intercepted events should be dispatched by calling [super sendEvent:event] after examining the intercepted event.
Example of [UIApplication sendEvent]
- (void)sendEvent:(UIEvent *)event {
  [super sendEvent:event];
  [self. idleTimer invalidate];
  self. idleTimer = [NSTimer scheduledTimerWithTimeInterval:kIDLE_TIME target:self selector:@selector(logout:) userInfo:nil repeats:NO];}
Example of [UIApplication sendEvent]
@interface YourWindow : UIWindow {
    NSDate timeOfLastTouch;
}
@end

@implementation YourWindow

- (void)sendEvent:(UIEvent *)event
{
    if(<acceptible last touch>)
        timeOfLastTouch = <current time>;

    [super sendEvent:event];
}

@end
Example of [UIApplication sendEvent]
- (void)sendEvent:(UIEvent *)event {
    [super sendEvent:event];

    // Only want to reset the timer on a Began touch or an Ended touch, to reduce the number of timer resets.
    NSSet *allTouches = [event allTouches];
    if ([allTouches count] > 0) {
        // allTouches count only ever seems to be 1, so anyObject works here.
        UITouchPhase phase = ((UITouch *)[allTouches anyObject]).phase;
        if (phase == UITouchPhaseBegan || phase == UITouchPhaseEnded)
            [self resetIdleTimer];
    }
}

- (void)resetIdleTimer {
    if (idleTimer) {
        [idleTimer invalidate];
        [idleTimer release];
    }

    idleTimer = [[NSTimer scheduledTimerWithTimeInterval:maxIdleTime target:self selector:@selector(idleTimerExceeded) userInfo:nil repeats:NO] retain];
}

- (void)idleTimerExceeded {
    NSLog(@"idle time exceeded");
}