私たちは、バックグラウンドで私たちのアプリを継続的に実行するためのコードを以下のように書くことができます。
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);
}
機能
- (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];
}
});
}
が、これはあなたを助けることを願っています。
WOSessionのバックグラウンドメソッドを使用している場合、自動的にiPhoneアプリを起動させるため、iOSのバックグラウンド処理は必要ありません。 – Mrug
あなたは正しいです。 iPhoneアプリをバックグラウンドに置くと180秒後にiphoneアプリにアクセスできなくなり、iwatchからiwatchにデータを表示するためにiwatchからリクエストを送信することはありません。 (私たちはiPhoneアプリケーションで使用しているローカルデータベースの場合) –