1

我想在按下主页按钮并且应用程序进入后台时上传图像。是否可以?如果是,那怎么办?如果没有,那么我还有其他选择吗?提前致谢...

4

2 回答 2

3

您可以在特定时间使用此代码在后台执行任务 -

 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

我认为这会对你有所帮助。:)

于 2013-03-07T09:20:55.407 回答
1

应用程序可以在关闭后请求在后台运行长达 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 页)

于 2013-03-07T09:46:49.800 回答