2

I'm developing an iOS app with latest SDK.

It's a fullscreen app.

I have a method on viewWillAppear method that has to be called every time the apps comes from background.

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self setUpVideo];
}

On setUpVideo I set up AVCaptureVideoPreviewLayer because I lose the video when the apps come back from background.

As I have read, viewWillAppear isn't called when the apps come back from background and now, I don't know where to put that code.

On this question, occulus suggest to use [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doMyLayoutStuff:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil]; but it doesn't work for me.

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setUpVideo:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
}

Any advice?

Community
  • 1
  • 1
VansFannel
  • 45,055
  • 107
  • 359
  • 626

3 Answers3

4

Observe UIApplicationWillEnterForegroundNotification instead.

- (void)viewDidAppear {
    [super viewDidAppear];
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(enterForeground:) 
              name:UIApplicationWillEnterForegroundNotification 
              object:nil];
    // ...
}

- (void)enterForeground:(NSNotification *)notification {
    // do stuff
}

Don't call viewWillAppear: directly from the enterForeground: method. Instead move all required code to a separate method and call that from both viewWillAppear: and enterForeground:.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
1
applicationWillEnterForeground will trigger when app comes from background

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}

Additionally, you can use UIApplicationDidBecomeActiveNotification for firing some method

[[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(handleMethod:)
                                                 name: UIApplicationDidBecomeActiveNotification
                                               object: [UIApplication sharedApplication]];
Cœur
  • 37,241
  • 25
  • 195
  • 267
βhargavḯ
  • 9,786
  • 1
  • 37
  • 59
0

Try posting this notification from - (void)applicationDidBecomeActive:(UIApplication *)application of AppDelegate(or observe corresponding notification which is better)

Timur Kuchkarov
  • 1,155
  • 7
  • 21