-1

Good morning all, I have my application checking for a connected device via BLE, if device is not connected i am using this code to segue to the settings App to establish the connection.

  NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
    [[UIApplication sharedApplication] openURL:url];

However upon returning to the Class using the provided back to app button on top of UI, The viewDidLoad method is not triggered again !! , the question is how may I perform the same check and now take a different action. i.e. segue to next scene ?

Regards JZ

John Z
  • 125
  • 1
  • 13

3 Answers3

1

You can use the viewDidAppear method to trigger it again

Black Magic
  • 2,706
  • 5
  • 35
  • 58
  • The ViewDidAppear ? Okay, so if I put my method in the viewDidAppear it will be accessed Twice ? On first launch and than returning from the Settings app ? – John Z Feb 19 '16 at 15:41
  • It should be. Everytime the ViewController appears on screen it will be called. However, even if ViewDidLoad is called, viewDidAppear will also be called! So beware of that – Black Magic Feb 19 '16 at 15:42
  • 1
    `viewDidAppear` does not get called on resuming the view from the `Settings` app. – Kanchu Jan 04 '18 at 16:24
1

You should check the View Controller lifecycle chart from Apple, because in this document your have more informations about viewDidLoad method than in the UIViewController class reference.

From this page, the description of viewDidLoad should help you :

viewDidLoad() — Called when the view controller’s content view (the top of its view hierarchy) is created and loaded from a storyboard. This method is intended for initial setup. However, because views may be purged due to limited resources in an app, there is no guarantee that it will be called only once.

Except for the special case where your app run out of memory, viewDidLoad is then called only once in a view controller lifecycle.

Use viewDidAppear method instead, which is called every time the view becomes visible (first appearance or when you come back from app settings for example).

Michaël Azevedo
  • 3,874
  • 7
  • 31
  • 45
0

You need to listen to UIApplicationWillEnterForegroundNotification in that view controller and check that condition when you app will enter foreground. In that case, the application going to foreground means the user coming back to your application from settings, so this is where you can check and perform a segue.

But be careful, if this view controller is always alive, it will execute the code every time your application goes to foreground. Be sure you only present this view controller when it is really needed.

Mert Buran
  • 2,989
  • 2
  • 22
  • 34
  • Thanks I will give that a try. Thanks again for your help. – John Z Feb 19 '16 at 15:43
  • Thanks guys for the help, viewDidAppear does not get triggered the second time coming back from Settings App. A little help on how to listen for UIApplicationWillEnterForegroundNotification , to accomplish my task – John Z Feb 19 '16 at 16:09
  • Great I figured it out UIApplicationWillEnterForegroundNotification Works perfectly.. – John Z Feb 19 '16 at 16:16