Running URL Requests in the Background

前端 未结 3 1370
面向向阳花
面向向阳花 2021-01-28 07:02

I want to make url request in a certain time interval (e.g. every 10 minutes app should make a url call and get some json data). App should be able to do this when running in ba

3条回答
  •  灰色年华
    2021-01-28 07:45

    With ios 7 new list of apps are added which can run in background. They are:

    1.  Apps that play audible content to the user while in the background, such as a music player app
    2.  Apps that record audio content while in the background.
    3.  Apps that keep users informed of their location at all times, such as a navigation app
    4.  Apps that support Voice over Internet Protocol (VoIP)
    5.  Apps that need to download and process new content regularly
    6.  Apps that receive regular updates from external accessories
    

    We just need to declare app's supported background tasks in info.plist. We need to add The UIBackgroundModes key to our app’s Info. plist. After that you can work normally with your timer, like:

    UIBackgroundTaskIdentifier bgTask;
    
    UIApplication  *app = [UIApplication sharedApplication];
    
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask]; 
    }];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:600 target:self
    selector:@selector(startServices) userInfo:nil repeats:YES];
    

    Hope it will help you.

提交回复
热议问题