Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7418815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:57:11+00:00 2026-05-29T07:57:11+00:00

I have been searching for literally weeks to try to find an answer, or

  • 0

I have been searching for literally weeks to try to find an answer, or an example of how to do this.

All the examples/tutorials for NSURLConnection show it starting in the foreground or starting in the background, ditto all the examples for beginBackgrounTaskWithExpirationHandler: show how to start a background task after entering the background.

As far as I can tell there is nothing out there on the internet or books that shows how to start a connection while in the foreground and then if its not finished continue it in the background.

The answer to this question does not actually answer the question:

How should beginbackgroundtaskwithexpirationhandler: be dealt with for an NSUrlConnection that is already in progress?

If you read the referened Beyond The Basics section it says: “While the app is in the foreground the background task won’t have any effect”. This means it is not possisble to start a background task using NSURLConnection while in the foreground if you want to download in the foreground.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:57:12+00:00Added an answer on May 29, 2026 at 7:57 am

    You simply call beginBackgroundTaskWithExpirationHandler: while your app is in the foreground right when you start the download process. Note that you have to store the return value in an ivar/property:

    @property (nonatomic, assign) UIBackgroundTaskIdentifier backgroundTaskID;
    
    @synthesize backgroundTaskID;
    
    ...
    
    NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
    self.backgroundTaskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        // Cancel the connection
        [connection cancel];
    }];
    

    This will allow your app to keep running if it gets sent to the background while the download is running. Then, in your delegate methods that signify the completion of the download, you have to place the matching endBackgroundTask::

    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
        // Handle the error
        ...
    
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskID];
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
        // Save the downloaded data
        ...
    
        [[UIApplication sharedApplication] endBackgroundTask:self.backgroundTaskID];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been searching all over the internet for an answer to this. It
I have been searching for an answer to this but can't find it on
I have been searching for this but cannot seem to find the answer. I
I have literally been searching for this for weeks. I am a novice java
Have been searching all over the internet but struggling to find my answer to
Have been searching around and can't find the answer to this. I'm just wondering
I have been searching for a few hours and cant seem to find answer
I have been searching this for quite sometime but could not find an appropriate
I have been searching the answer to this question for a while. It seemed
I have been searching everywhere for this and not been able to find it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.