Running URL Requests in the Background

前端 未结 3 1300
面向向阳花
面向向阳花 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:28

    I think there are some restrictions in iOS, You can not continuously send the request when app is in Background mode. iOS allows a little time to finish the web request when App is going to background mode. please check this Apple Documentation: https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplication_Class/Reference/Reference.html#//apple_ref/occ/instm/UIApplication/beginBackgroundTaskWithExpirationHandler:/

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-01-28 07:50

    At the basic level : simply schedule a NSTimer and launch the URL request on the timer function. If you are targeting iOS7 then make sure you are using NSURLSession. However an important point is to know if you want to do tis in the background or not. If yes you have to find out more about the iOS background modes as you will not be able to maintain your timer in this mode.. Nothing there should cause rejection.

    0 讨论(0)
提交回复
热议问题