Sie können die Hintergrundaufgabe APIs verwenden, um eine Methode aufzurufen, nachdem Sie (so lange backgrounded worden bin als Ihre Aufgabe übernehmen nicht zu lang - normalerweise ~ 10 Minuten ist die maximal zulässige Zeit).
iOS lässt Timer nicht abfeuern, wenn die App im Hintergrund läuft. Daher habe ich festgestellt, dass das Senden eines Hintergrundthreads, bevor die App in den Hintergrund versetzt wird und der Thread in den Ruhezustand versetzt wird, den gleichen Effekt wie ein Timer hat.
Setzen Sie den folgenden Code in der App delegieren - (void)applicationWillResignActive:(UIApplication *)application
Methode:
// 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];
}
});
Ihre Methoden in ** AppDelegate.m Datei ** 'applicationWillResignActive' und dann' applicationDidEnterBackground' genannt wird. – Rohan
Danke. Aber ich meine, wenn es im Hintergrund ist – Rubinc
Werfen Sie einen Blick auf [Wie kann ich einen Screenshot des iPhone-Startbildschirms programmgesteuert machen] (http://stackoverflow.com/q/13459682/593709) –