2016-12-29 6 views
1

アプリケーションのバックグラウンドモードでダウンロードすることができました。私はバックグラウンドフェッチモードをXCodeに実装し、バックグラウンドタスクを登録して正常に動作しています。バックグラウンドモードでアプリを終了してから再開します。

ユーザーがアプリケーションを強制終了した後にタスクをダウンロードすることはできますか?どうやって?エラーオブジェクトかnil(タスクが正常に完了した場合)のいずれかで方法:タスク::didCompleteWithError

+0

を述べてダウンロード –

+0

強制終了後にまだダウンロードを完了したい – Saranjith

+0

申し訳ございませんが、あなたのアプリは閉じることができません。 –

答えて

1

任意のタスクが完了するは、NSURLSessionオブジェクトは、デリゲートのURLSessionを呼び出します。 タスクが失敗した場合、ほとんどのアプリは、ユーザーがダウンロードをキャンセルするか、サーバーが要求が成功しないことを示すエラーを返すまで、要求を再試行する必要があります。しかし、あなたのアプリはただちに再試行しないでください。代わりに、到達可能性APIを使用してサーバーに到達可能かどうかを判断し、到達可能性が変更されたという通知を受け取った場合にのみ新しい要求を行う必要があります。

ダウンロードタスクを再開できる場合、NSErrorオブジェクトのuserInfoディクショナリには、NSURLSessionDownloadTaskResumeDataキーの値が含まれています。あなたのアプリはこの値を渡してdownloadTaskWithResumeData:またはdownloadTaskWithResumeData:completionHandler:を呼び出して、既存のダウンロードを続ける新しいダウンロードタスクを作成します。

タスクを再開できない場合は、新しいダウンロードタスクを作成し、最初からトランザクションを再開する必要があります。ここ

チェックアウト:アプリがユーザーによって終了得るときLife cycle of URL Session

2

いいえ、あなたはダウンロードを続行することはできません!あなたのアプリはバックグラウンド状態に留まる必要があります!ユーザが強制終了アプリを意味する場合、彼はもうそれを実行したくないからです。あなたのアプリがシステムによって中断されている場合、それは起床することができますが、ユーザによって終了された場合は起きることはありません!

If an iOS app is terminated by the system and relaunched, the app can use the same identifier to create a new configuration object and session and retrieve the status of transfers that were in progress at the time of termination. This behavior applies only for normal termination of the app by the system. If the user terminates the app from the multitasking screen, the system cancels all of the session’s background transfers. In addition, the system does not automatically relaunch apps that were force quit by the user. The user must explicitly relaunch the app before transfers can begin again.

更新:(コメントに尋ねたよう)

Refer the apple documentation、これは、ファイルが存在しない場合は起動し、その後、あなたのファイルは、ドキュメントディレクトリにあるかを確認applicationWillEnterForeground、

This method lets your app know that it is about to be terminated and purged from memory entirely. You should use this method to perform any final clean-up tasks for your app, such as freeing shared resources, saving user data, and invalidating timers. Your implementation of this method has approximately five seconds to perform any tasks and return. If the method does not return before time expires, the system may kill the process altogether.

For apps that do not support background execution or are linked against iOS 3.x or earlier, this method is always called when the user quits the app. For apps that support background execution, this method is generally not called when the user quits the app because the app simply moves to the background in that case. However, this method may be called in situations where the app is running in the background (not suspended) and the system needs to terminate it for some reason.

After calling this method, the app also posts a UIApplicationWillTerminate notification to give interested objects a chance to respond to the transition.

+0

(void)applicationWillTerminate:(UIApplication *)applicationこれがどれくらい時間がかかるか – Saranjith

+0

まず、 'applicationWillTerminate'が呼び出される保証はありません!!だから、あなたはそれに頼るべきではありませんが、理想的には5秒かかると言うことができます!!!! – Lion

+0

私の更新された答えをより明確に確認することができます! – Lion

関連する問題