简体   繁体   English

当应用程序进入后台时,是否可以调用webRequest?

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

I want to upload images when home button pressed and application goes in background. 我想在按下主页按钮和应用程序进入后台时上传图像。 Is it possible? 可能吗? If yes then how? 如果是,那怎么样? and if no then do i have any other alternative? 如果没有那么我还有其他选择吗? Thanks in advance... 提前致谢...

You can perform a task in background by using this code for a specific time- 您可以在特定时间内使用此代码在后台执行任务 -

 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];

Refer this: http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html 请参阅: http//developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

I think this will help u. 我想这会对你有所帮助。 :) :)

An app can request to run in the background for up to 10 minutes after it has been closed so that it can finish a long-running task. 应用程序可以在关闭后请求在后台运行最多10分钟,以便它可以完成长时间运行的任务。 Only some processes are allowed to run in background. 只允许某些进程在后台运行。 See Implementing Long-Running Background Tasks section in this reference. 请参阅本参考中的 实现长时间运行的后台任务部分

If your app is allowed so, you can try below code: 如果允许您的应用,您可以尝试下面的代码:

- (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;

    });

}

if you want to know how much time your app has left to run 如果您想知道您的应用程序剩余运行的时间

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

For more ref go with this reference PDF(page 60) 有关更多参考,请参阅参考PDF(第60页)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM