[ios] 백그라운드에서 앱을 열 때 ViewDidAppear가 호출되지 않습니다

나는 나의 값이 0 (라벨) 때 내가보기 컨트롤러가 서로 개폐되는 뷰 컨트롤러가 ViewController내가 설정 한 viewDidAppear라벨에 설정 값 (20)을. 그것은 잘 작동하지만 난 내 응용 프로그램을 종료하지 않고 다시 내 응용 프로그램을 열보다하지만 값이 있기 때문에 변경하지 않을 때 viewDidLoad, viewDidAppear그리고 viewWillAppear아무것도 전화를받을. 앱을 열 때 어떻게 전화를 걸 수 있습니까? 내가해야 할 일이 applicationDidBecomeActive있습니까?



답변

정확한 이벤트 순서에 대해 궁금한 점은 다음과 같이 앱을 계측 한 것입니다. (@Zohaib, 아래 NSNotificationCenter 코드를 사용하여 질문에 답변 할 수 있습니다).

// AppDelegate.m

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    NSLog(@"app will enter foreground");
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSLog(@"app did become active");
}

// ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"view did load");

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillEnterForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
}

- (void)appDidBecomeActive:(NSNotification *)notification {
    NSLog(@"did become active notification");
}

- (void)appWillEnterForeground:(NSNotification *)notification {
    NSLog(@"will enter foreground notification");
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    NSLog(@"view will appear");
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    NSLog(@"view did appear");
}

시작시 출력은 다음과 같습니다.

2013-04-07 09:31:06.505 myapp[15459:11303] view did load
2013-04-07 09:31:06.507 myapp[15459:11303] view will appear
2013-04-07 09:31:06.511 myapp[15459:11303] app did become active
2013-04-07 09:31:06.512 myapp[15459:11303] did become active notification
2013-04-07 09:31:06.517 myapp[15459:11303] view did appear

배경을 입력 한 다음 전경을 다시 입력하십시오.

2013-04-07 09:32:05.923 myapp[15459:11303] app will enter foreground
2013-04-07 09:32:05.924 myapp[15459:11303] will enter foreground notification
2013-04-07 09:32:05.925 myapp[15459:11303] app did become active
2013-04-07 09:32:05.926 myapp[15459:11303] did become active notification


답변

Objective-C 사용

의 메소드 UIApplicationWillEnterForegroundNotification에를 등록해야 하며 백그라운드에서 앱이 다시 표시 될 때마다 알림에 등록 된 메소드에서 원하는 모든 작업을 수행 할 수 있습니다. 의 viewWillAppear 또는 viewDidAppear는 응용 프로그램이 포 그라운드로 배경에서 돌아 오면 호출되지 않습니다.ViewControllerviewDidLoadViewController

-(void)viewDidLoad{

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doYourStuff)

  name:UIApplicationWillEnterForegroundNotification object:nil];
}

-(void)doYourStuff{

   // do whatever you want to do when app comes back from background.
}

등록한 알림을 등록 취소하는 것을 잊지 마십시오.

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

당신이 등록 할 경우 viewController를 위해 UIApplicationDidBecomeActiveNotification다음 방법은 앱이 활성화 될 때마다 호출 될 것이다, 등록을 권장하지 않습니다 viewController이 통지.

스위프트 사용

관찰자를 추가하려면 다음 코드를 사용할 수 있습니다

 override func viewDidLoad() {
    super.viewDidLoad()

     NotificationCenter.default.addObserver(self, selector: "doYourStuff", name: UIApplication.willEnterForegroundNotification, object: nil)
 }

 func doYourStuff(){
     // your code
 }

관찰자를 제거하려면 swift의 deinit 기능을 사용할 수 있습니다.

deinit {
    NotificationCenter.default.removeObserver(self)
}


답변

스위프트 3.0 ++ 버전

viewDidLoad백그라운드 작업에서 열린 알림을 들으려면 알림 센터에 등록 하십시오 .

NotificationCenter.default.addObserver(self, selector:#selector(doSomething), name: NSNotification.Name.UIApplicationWillEnterForeground, object: nil)
        

그런 다음이 기능을 추가하고 필요한 조치를 수행하십시오.

func doSomething(){
    //...
}

마지막으로이 기능을 추가하여 뷰 컨트롤러가 파괴 될 때 알림 관찰자를 정리하십시오.

deinit {
    NotificationCenter.default.removeObserver(self)
}


답변

스위프트 4.2. 버전

viewDidLoad앱이 백그라운드에서 돌아올 때 알림을 받으려면 NotificationCenter에 등록하십시오.

NotificationCenter.default.addObserver(self, selector: #selector(doSomething), name: UIApplication.willEnterForegroundNotification, object: nil)

호출해야하는 메소드를 구현하십시오.

@objc private func doSomething() {
    // Do whatever you want, for example update your view.
}

관찰자 ViewController가 파괴 되면 관찰자를 제거 할 수 있습니다 . iOS9 및 macOS 10.11 이하에서만 필요합니다.

deinit {
    NotificationCenter.default.removeObserver(self)
}


답변

뷰 컨트롤러가 UIApplicationWillEnterForegroundNotification알림에 등록 하고 그에 따라 반응하도록하십시오.


답변

UIApplicationWillEnterForegroundNotification에 등록하면 해당 알림에 반응하는 둘 이상의 컨트롤러가 생길 수 있으므로 위험합니다. 알림이 수신 될 때 이러한 컨트롤러가 여전히 표시된다는 것을 보증하는 것은 없습니다.

여기 내가하는 일이 있습니다 : 앱의 델리게이트 didBecomeActive 메소드에서 직접 활성 컨트롤러에서 viewDidAppear 호출을 강제합니다.

아래 코드를 추가하십시오 - (void)applicationDidBecomeActive:(UIApplication *)application

UIViewController *activeController = window.rootViewController;
if ([activeController isKindOfClass:[UINavigationController class]]) {
    activeController = [(UINavigationController*)window.rootViewController topViewController];
}
[activeController viewDidAppear:NO];


답변

AppDelegate applicationWillEnterForeground에서 이것을 추가하십시오.

func applicationWillEnterForeground(_ application: UIApplication) {
    // makes viewWillAppear run
    self.window?.rootViewController?.beginAppearanceTransition(true, animated: false)
    self.window?.rootViewController?.endAppearanceTransition()
}