Continue download in background

后端 未结 3 714
余生分开走
余生分开走 2021-02-07 21:13

I am creating an application wherein I am downloading some data from server. While going in background I want that connection should continue running so that data can be downloa

相关标签:
3条回答
  • 2021-02-07 21:46

    I don't know how you handle your data downloading exactly. But you can take a look at ASIHTTPRequest. It is very simple and straightforward, and works with ARC if you set the compiler flags to -fno-objc-arc. With this you only have to use

    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setShouldContinueWhenAppEntersBackground:YES]; //For iOS 4.0 and up 
    

    And that works.

    Here you can see how ASIHTTPRequest works

    Hope it helps!

    0 讨论(0)
  • 2021-02-07 21:50

    [edit] Sorry I was incorrect, as was pointed out in the comments you can extend the time limit you have to perform operations once/before your app goes into the background. Here is Apple's Official Documentation

    0 讨论(0)
  • 2021-02-07 22:08

    One way to do some operations that continue in the background is to create a separate thread to do the downloading. Inside the thread, bracket your download operations between calls to beginBackgroundTaskWithExpirationHandler: and endBackgroundTask. You don't need to check to see whether you are running in the background or not, you just always call these two methods.

    // Tell iOS this as a background task in case we get backgrounded
    UIBackgroundTaskIdentifier taskId = [[UIApplication sharedApplication] 
              beginBackgroundTaskWithExpirationHandler:NULL];
    
    //----------------------------------------------
    // Perform your download operations here
    //----------------------------------------------
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    
    // Tell iOS that we are done with stuff that needed to keep going even if backgrounded    
    [[UIApplication sharedApplication] endBackgroundTask:taskId];
    
    0 讨论(0)
提交回复
热议问题