![](/img/trans.png)
[英]Can we change iPhone app binary after it has been upload and waiting for upload
[英]Can we call the method after the application has been minimized?
iOS版
应用程序最小化后我们可以调用该方法吗?
例如,5秒后被称为applicationDidEnterBackground:
.
我使用此代码,但test
方法不调用
- (void)test
{
printf("Test called!");
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self performSelector:@selector(test) withObject:nil afterDelay:5.0];
}
您可以使用后台任务API在后台运行后调用方法(只要您的任务不需要太长时间 - 通常约10分钟是最大允许时间)。
当应用程序背景化时,iOS不会让计时器触发,因此我发现在应用程序背景化之前调度后台线程,然后将该线程置于睡眠状态,与计时器具有相同的效果。
将以下代码放入app delegate - (void)applicationWillResignActive:(UIApplication *)application
方法:
// Dispatch to a background queue
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
// Tell the system that you want to start a background task
UIBackgroundTaskIdentifier taskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
// Cleanup before system kills the app
}];
// Sleep the block for 5 seconds
[NSThread sleepForTimeInterval:5.0];
// Call the method if the app is backgrounded (and not just inactive)
if (application.applicationState == UIApplicationStateBackground)
[self performSelector:@selector(test)]; // Or, you could just call [self test]; here
// Tell the system that the task has ended.
if (taskID != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:taskID];
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.