NSURLSession background download - resume over network failure

后端 未结 3 719
执念已碎
执念已碎 2020-12-13 07:05

After reading the Apple documentation about the background download with the new iOS7 api (NSURLSession), I\'m a bit disappointed. I was sure that Apple was managing the pau

相关标签:
3条回答
  • 2020-12-13 07:36

    As I'm still in background I cannot trigger a "resume" when network is back without launching the application…


    you can use "background fetch",when the app is launched by fetch,then you can check network and resume the download task.

    0 讨论(0)
  • 2020-12-13 07:53

    You should create the NSURLSession with background configurations, then your task is sent to a background demon and your app get called when it is completed.

    0 讨论(0)
  • 2020-12-13 07:54

    Implementing:

    application:handleEventsForBackgroundURLSession:completionHandler:
    

    in the app delegate - without calling the completionHandler - causes the app to hang around in the background after the device loses its connection whilst suspended. That way, the app can still listen to reachability notifications and restart the download when a network connection becomes available once again. However, this is a pretty dodgy approach and may not pass Apple's app store submission guidelines. Additionally, this approach isn't much help when the connection is lost while the app is in the foreground and the connection regained whilst the app is suspended.

    In the end I did the following:

    • Made use of the application:handleEventsForBackgroundURLSession:completionHandler: notification to pause my downloads in the background.
    • Made use of the intermittent background fetch notification (ie. application:performFetchWithCompletionHandler:completionHandler) to check connection status and restart any paused downloads. (hat-tip @gugupluto)

    This still doesn't provide optimal download performance and may lead users to wonder why their "background download" hasn't finished once they reopen the app, but it seems to be the best we can hope for from Apple for now.

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