사과 설명서에 따라 "앱이 백그라운드로 이동하면 프레임 워크는 광고 및 탐색을 중지하고 열려있는 세션의 연결을 끊습니다. 포 그라운드로 돌아 가면 프레임 워크는 자동으로 광고 및 탐색을 재개하지만 개발자는 모든 닫힌 세션을 다시 설정해야합니다 세션은 "참고 :이 같은 상황에있는 사람들을 도움이 될 희망, 내 자신의 질문에 대답
을 다음과 같이 연결이 연장 Apple doc
한 가지 방법. iOS 개발을 처음 접하는 사람들에게 "백그라운드 서비스 사용하기"란 간단히 말해 타겟의 "기능"탭에서 "백그라운드 모드"옵션을 켜는 것을 의미합니다. 그것만으로도 앱이 죽기 전에 백그라운드에서 약 10 분의 생활을 제공해야합니다.
그러나 앱이 백그라운드로 이동하면 "backgroundTimeRemaining"을 사용하여 남은 시간을 알 수 있습니다. 180 (초, 3 분)부터 시작하지만 인쇄 루프는 계속됩니다. 작업은 3 분이 경과 했으므로 시간에 도달하면 수동으로 코드를 작성해야합니다.
Multipeer Connectivity의 경우 앱이 백그라운드로 들어갈 때 연결을 유지하기에 충분하며 문제없이 모든 메시지/스트림을 계속 수신합니다. 이 코드를 살펴 되세요 appDelegate.m
- (void)applicationDidEnterBackground:(UIApplication *)application
{
self.backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^
{
//This is called 3 seconds before the time expires
//Here: Kill the session, advertisers, nil its delegates,
// which should correctly send a disconnect signal to other peers
// it's important if we want to be able to reconnect later,
// as the MC framework is still buggy
[application endBackgroundTask:self.backgroundTask];
self.backgroundTask = UIBackgroundTaskInvalid; //Invalidate the background task
}];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Here: We should init back the session, start the advertising and set the delegates from scratch
// This should allow the app to reconnect to the same session with more often than not
self.backgroundTask = UIBackgroundTaskInvalid; //Here we invalidate the background task if the timer didn't end already
}
에서 appDelegate.h
에서
:
안정성을 위해서, 나는 다음과 같이 몇 가지 청소를 할 . https://github.com/davidkaminsky/Unplugged/blob/master/Unplugged/UnpluggedAppDelegate.m TCP 연결에서이 문제가 발생했습니다. 몇 분 동안 백그라운드에서 앱을 실행할 수 있지만 더 오래 걸리면 깨끗하게해야합니다. 연결을 설정하고 백그라운드 작업을 중지하십시오. 짧은 시간 동안 사용자가 앱을 사용하지 않는 동안 연결을 열어 둘 수 있습니다. – sbarow
고마워, 나는 조금 비슷한 것을했지만, 그 멋진 (즉, 배터리 수명을 모니터링하지 않음). – Einho