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");
}

UIApplication scheduleLocalNotification example in Objective C (iOS)


[UIApplication scheduleLocalNotification]

Schedules a local notification for delivery at its encapsulated date and time.
- (void)scheduleLocalNotification:(UILocalNotification *)notification
Parameters of [UIApplication scheduleLocalNotification]
notification
A local notification that the operating system delivers for the application at the date and time specified in the fireDate property of notification. Because the operating system copies notification, you may release it once you have scheduled it.

Example of [UIApplication scheduleLocalNotification]

UILocalNotification *localNotification = [[UILocalNotification alloc] init];

    // Set the fire date/time
    [localNotification setFireDate:yourDate];
    [localNotification setTimeZone:[NSTimeZone defaultTimeZone]];

    localNotification.applicationIconBadgeNumber=1;

    // Setup alert notification
    [localNotification setAlertAction:@"Open App"];
    [localNotification setAlertBody:[randonQuotesDic objectForKey:@"Rajneesh"]];
    [localNotification setAlertBody:@"You had set a Local Notification on this time"];

    localNotification.soundName=UILocalNotificationDefaultSoundName;
    [localNotification setHasAction:YES];      
    UIApplication *app=[UIApplication sharedApplication];
    [app scheduleLocalNotification:localNotification];

Example of [UIApplication scheduleLocalNotification]
-(void)endProxyAndWriteToSystemLocalNotification
{
        _proxying = NO;
        NSDate *dateAnchor = [NSDate date];

        NSEnumerator *enumerator = [self.notifications objectEnumerator];
        NSInteger i = 0;
        while (i < maxLocalNotifCount) {
            UILocalNotification *n = [enumerator nextObject];
            if (!d) {
                break;
            }

            if ([n.fireDate timeIntervalSinceDate:dateAnchor] >= 0) {
                [[UIApplication sharedApplication] scheduleLocalNotification:n];
                i++;
            }
        }

        [self.notificationDatas removeAllObjects];

}
Example of [UIApplication scheduleLocalNotification]
-(void) scheduleLocalNotifications
{

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    for (int i = 0; i < 60; i++)
    {
        UILocalNotification *localNotif = [[UILocalNotification alloc] init];
        if (localNotif == nil)
            return;
        NSDate *sleepDate = [[NSDate date] dateByAddingTimeInterval:i * 60];
        NSLog(@"Sleepdate is: %@", sleepDate);

        localNotif.fireDate = sleepDate;    

        NSLog(@"fireDate is %@",localNotif.fireDate);
        localNotif.timeZone = [NSTimeZone defaultTimeZone];
        localNotif.alertBody = [NSString stringWithFormat:NSLocalizedString(@"This is local notification %i"), i];

        localNotif.alertAction = NSLocalizedString(@"View Details", nil);
        localNotif.soundName = UILocalNotificationDefaultSoundName;
        localNotif.applicationIconBadgeNumber = 1;

        [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
        NSLog(@"scheduledLocalNotifications are %@", [[UIApplication sharedApplication] scheduledLocalNotifications]);
        [localNotif release];

    }

    [pool release];
}