重要または貴重なダウンロードまたはアップロード操作を実行していて、アプリがバックグラウンドに入った場合、「IOS」から追加の時間をリクエストして作業を完了することができます。アプリがバックグラウンドにある間に、それを完了するために追加の 10 分間が許可されます。モード。
ただし、オペレーションは重要で受け入れ可能でなければならないことに注意してください。そうしないと、アプリが Apple レビュー プロセスから拒否される可能性があります。
詳細については、バックグラウンド実行とマルチタスクに関する Apple ドキュメントを参照してください。
さて、いくつかのアクションのポイントと時間を締めくくり、バックグラウンドでタスクを続行するには、次のメソッドで実行できます。アプリケーション デリゲート メソッドを管理する必要はありません。次のスニペットを使用するだけで、ダウンロードまたはアップロードにデリゲートを使用しないでください。
if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4
if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking
UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance
__block UIBackgroundTaskIdentifier background_task; //Create a task object
background_task = [application beginBackgroundTaskWithExpirationHandler: ^ {
[application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks
background_task = UIBackgroundTaskInvalid; //Set the task to be invalid
//System will be shutting down the app at any point in time now
}];
//Background tasks require you to use asyncrous tasks
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//Perform your tasks that your application requires
NSLog(@"\n\nRunning in the background!\n\n");
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"YOUR HOST URL"]];
NSURLResponse *response = nil;
NSError *requestError = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"ResponseString:%@",responseString);
[application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform
background_task = UIBackgroundTaskInvalid; //Invalidate the background_task
});
}
}