この例では、4.0より前の互換性のために括弧で囲まれている:
UIApplication *app = [UIApplication sharedApplication];
if ([app respondsToSelector:@selector(beginBackgroundTaskWithExpirationHandler:)]) {
backgroundTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
dispatch_async(dispatch_get_main_queue(), ^{
if (backgroundTaskIdentifier != UIBackgroundTaskInvalid)
{
// you took too long - clean up what you can, then …
[app endBackgroundTask:backgroundTaskIdentifier];
backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}
});
}];
}
// start HTTP request …
あなたのプロセスを完了するとき、あなたはアプリが UIBackgroundTaskIdentifier __block backgroundTaskIdentifierを追加してみてください。また
を知っているように
endBackgroundTask:
を呼び出す必要があります。このブロックの前に :) – huesforalice