In dieser Situation können wir unten Code für die kontinuierliche Ausführung unserer App im Hintergrund schreiben.
AppDelegate.h
Bool *isBackground = NO;
- (void)applicationDidEnterBackground:(UIApplication *)application {
NSLog(@"Enter Background");
[self extendBackgroundRunningTime]; //Call function for upadte time
NSLog(@"Time Remaining: %f", [[UIApplication sharedApplication] backgroundTimeRemaining]);
NSLog(@"%d",_isFromBackground);
}
Funktion
- (void)extendBackgroundRunningTime {
if (_isFromBackground != UIBackgroundTaskInvalid) {
return;
}
NSLog(@"Attempting to extend background running time");
__block Boolean self_terminate = YES;
_isFromBackground = [[UIApplication sharedApplication] beginBackgroundTaskWithName:@"DummyTask" expirationHandler:^{
NSLog(@"Background task expired by iOS");
if (self_terminate) {
[[UIApplication sharedApplication] endBackgroundTask:_isFromBackground];
_isFromBackground = UIBackgroundTaskInvalid;
}
}];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(@"Background task started");
while (true) {
NSLog(@"background time remaining: %8.2f", [UIApplication sharedApplication].backgroundTimeRemaining);
[NSThread sleepForTimeInterval:10];
}
});
}
Hope this Sie helfen können.
Es ist keine Hintergrundbehandlung für iOS erforderlich, denn wenn Sie WCSessions Hintergrundmethoden verwenden, wird Ihre iPhone App automatisch aktiviert. – Mrug
Sie haben Recht. Wenn Sie Ihre iPhone App in den Hintergrund stellen, können Sie nach 180 Sekunden nicht mehr auf die iphone App zugreifen und senden Sie niemals eine Anfrage von iwatch an iphone, um Daten in iwatch anzuzeigen. (Im Fall der lokalen Datenbank verwenden wir in der iPhone App) –