IOS performs this operation at intervals in the background.

Source: Internet
Author: User

Steps:

1. Add the uibackgroundmodes key to info. plist. The value is an array and one of the arrays is a VoIP string:

<Key> uibackgroundmodes </key> <array> <string> VoIP </string> </array>

2. Call the-(void) setupbackgroundhandler function when the program starts. The function body is as follows:

#pragma mark - VoIP- (void)setupBackgroundHandler{        if( UIUDeviceIsBackgroundSupported() )    {        if(           [[UIApplication sharedApplication] setKeepAliveTimeout:600 handler: ^            {                [self requestServerHowManyUnreadMessages];            }            ]           )        {            UDLog(@"Set Background handler successed!");        }        else        {//failed            UDLog(@"Set Background handler failed!");        }    }    else    {        UDLog(@"This Deviece is not Background supported.");    }}- (void)requestServerHowManyUnreadMessages{    UIApplication* app = [UIApplication sharedApplication];        if([app applicationState] == UIApplicationStateBackground)    {        NSArray * oldNotifications = [app scheduledLocalNotifications];        if ([oldNotifications count] > 0)            [app cancelAllLocalNotifications];        UILocalNotification* alarm = [[[UILocalNotification alloc] init] autorelease];        if (alarm)        {            alarm.fireDate = [NSDate dateWithTimeIntervalSinceNow:15];            alarm.timeZone = [NSTimeZone defaultTimeZone];            alarm.repeatInterval = 0;            alarm.soundName = UILocalNotificationDefaultSoundName;            alarm.alertBody = @"Time to request MOA2 Server!";            [app scheduleLocalNotification:alarm];        }    }    else if([app applicationState] == UIApplicationStateActive)    {        UIAlertView *alertView =  [[[UIAlertView alloc] init] autorelease];        [alertView setTitle:@"alert"];        [alertView setMessage:@"Time to request MOA2 Server!"];        [alertView addButtonWithTitle:NSLocalizedString(@"cancel", nil)];        [alertView setDelegate:nil];        [alertView show];    }}

Explanation:

-(Bool) setkeepalivetimeout :( nstimeinterval) Timeout handler :( void (^) (void) keepalivehandler

Function: the app wakes up every timeout.

0. To successfully call this function, you must set one of the array values of the uibackgroundmodes key in info. plist to a VoIP string.

1. Timeout must be greater than or equal to 600

2. The interval between wake-up apps is inaccurate.

3. Only 10 seconds after waking up. That is, the code in handler is executed in 10 seconds. The app is blocked again 10 seconds later.

(The-backgroundtimeremaining attribute can be used to return the remaining time)

4. The function is valid within the life cycle of the program after it is successfully called.

This function is still effective when you return to the foreground. (So you can use it as a timer .)

5. The clearkeepalivetimeout function is used to clear handler.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.