簡體   English   中英

當應用程序進入后台時,是否可以調用webRequest?

[英]Is it possible to call webRequest when application goes in background?

我想在按下主頁按鈕和應用程序進入后台時上傳圖像。 可能嗎? 如果是,那怎么樣? 如果沒有那么我還有其他選擇嗎? 提前致謝...

您可以在特定時間內使用此代碼在后台執行任務 -

 UIBackgroundTaskIdentifier bgTask = 0;
    UIApplication  *app = [UIApplication sharedApplication];
    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask]; 
    }];
    self.silenceTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self
                                                       selector:@selector(startLocationServices) userInfo:nil repeats:YES];

請參閱: http//developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

我想這會對你有所幫助。 :)

應用程序可以在關閉后請求在后台運行最多10分鍾,以便它可以完成長時間運行的任務。 只允許某些進程在后台運行。 請參閱本參考中的 實現長時間運行的后台任務部分

如果允許您的應用,您可以嘗試下面的代碼:

- (void)applicationDidEnterBackground:(UIApplication *)application

{

    UIBackgroundTaskIdentifier bgTask;
    bgTask = [application beginBackgroundTaskWithExpirationHandler:^{

        // Clean up any unfinished task business by marking where you

        // stopped or ending the task outright.

        [application endBackgroundTask:bgTask];

        bgTask = UIBackgroundTaskInvalid;

    }];



    // Start the long-running task and return immediately.

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{



        // Do the work associated with the task, preferably in chunks.



        [application endBackgroundTask:bgTask];

        bgTask = UIBackgroundTaskInvalid;

    });

}

如果您想知道您的應用程序剩余運行的時間

NSTimeInterval ti = [[UIApplication sharedApplication]backgroundTimeRemaining];
NSLog(@"Remaining Time: %f", ti); // just for debug

有關更多參考,請參閱參考PDF(第60頁)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM