应用程序最小化后我们可以调用该方法吗?
例如,5秒后被称为applicationDIDEnterBackground:.
我使用此代码,但测试方法不调用
- (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 *)应用程序方法:
// dispatch to a background queuedispatch_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]; }});总结
以上是内存溢出为你收集整理的iphone – 我们可以在应用程序最小化后调用该方法吗?全部内容,希望文章能够帮你解决iphone – 我们可以在应用程序最小化后调用该方法吗?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)