viewDidLoad is called only the first time - objective-c

Maybe someone could tell me why when launch my app in the second time (after pressing the home button) the method viewDidLoad: is not being called?

On iOS 4 apps are no longer exited when pressing the home button. They are moved out of RAM, paused and then continued when you re-enter them, not launched again.

In iOS4 there is:
(void)applicationDidBecomeActive:(UIApplication *)application { }
Which is called when the application becomes active (out of background) you could call things from here that need to happen when you open the app.
Thanks
James

The second time you "launch" your application, in fact you are not launching but only re-activating (your application was in background). Your views are not reloaded.
As said in the previous answers :
You can detect it via (void)applicationDidBecomeActive:(UIApplication *)application and execute some code here (refresh HMI, refetch datas, etc...),
Or move your code to viewWillAppear

Related

Covering the root view with another immediately upon launch or resume

I'm trying to build a security app that prompts the user to enter a passcode before allowing access to the application. This is done on the first launch or when the application is resumed.
Right now, I'm using a view controller, PasscodeViewController, which is presented modally on application launch or resume, i.e., in the app delegate:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if (!self.passcodeViewController.view.window)
[self.window.rootViewController presentViewController:self.passcodeViewController animated:NO completion:NULL];
}
The problem is that the main view controller's view is flashed momentarily before the PasscodeViewController is presented. This is a security risk because the user can quickly get a glimpse of the data before being asked to enter a passcode.
How do you solve this? How do programs like DotLockData, and other security programs, implement such a feature?
Seems it would be better to do that sort of thing on suspend rather than resume. Perhaps in applicationWillResignActive

Does didFinishLaunchingWithOptions happens after certain application "exits"?

Does didFinishLaunchingWithOptions happens after:
applicationWillResignActive
applicationDidEnterBackground
applicationWillEnterForeground
Or does it happen only after applicationWillTerminate?
And when applicationDidBecomeActive happens then? Thanks.
From the docs:
It is called after your application has been launched and its main nib
file has been loaded. At the time this method is called, your
application is in the inactive state. At some point after this method
returns, a subsequent delegate method is called to move your
application to the active (foreground) state or the background state.
It happens when the user opens your app. Followed by applicationDidBecomeActive when the app is ready to receive user events.
When the user presses the home button the following methods are called (by this order):
- applicationWillResignActive
- applicationDidEnterBackground
When the user opens your app again, and it is in background:
applicationWillEnterForeground
applicationDidBecomeActive
Finally, applicationWillTerminate is called instead of applicationDidEnterBackground on devices with iOS 3.x or earlier. Or with devices that do not support background apps (like the 3G).
application:didFinishLaunchingWithOptions:
only fires once: when your program starts up. You should typically create the main window/view controller here.

UIAlert during splashcreen

This is a two part question.
I have created a user agreement that the user must agree to when first launching the app (it is an alert with some information and agree/ do not agree button)
I call upon the method that creates this alert inside myAppDelegate.m and within the method
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
The problem is the alert pops up when the splash screen has finished loading and my first view comes up. I want this to happen during the splash screen. How would I do this?
The second question is When the users presses the "Do not agree button", I want them to exit the app so I have programmed it with
exit(0);
Is there a better way and will apple reject my app because of this?
Thanks in advance
1) You can't -- during the splash screen (your default.png) the app is loading into memory, and it cannot therefore execute any code, including presentation of a UIAlertView. That's why you don't see the alert until the splash disappears -- removal of the splash screen is the last thing that the app does before calling applicationDidFinishLoading:withOptions:.
What you can do is create a view controller that mimics your splash screen. This is easy -- you can even reuse default.png as a background if you want, though a better idea is just to present in this first view controller your agreement text and agree/disagree buttons.
As to your question re: use of exit(), it's best to avoid doing that. If the user refuses, you can simply do nothing. Of course, if you go the view controller route as I suggest, you can leave presented another opportunity for the user to agree.
Another thought is that Apple allows you to customize the EULA of your app when you upload a binary -- you could put it there and be covered.
Why not load our default.png as the background of you initial view and just handle the Alert in it's controller. you can always add another view or segue based on the answer.
The problem is the UIAlert blocks the Main thread, so it could stop your app from launching in time, and the process could be terminated.

iOS: Changing a view on backgroung does not refresh what is being seen before the app returns to foreground

Ok, I got this issue: I have an application with a login screen that is suposed to show everytime the app goes to background and return. The problem is, the previous screen appears for a fraction of second after the app return to foreground, because the system only refresh what is being seen after loading. What is need is a complete transition before the app returns to foreground. Yes, I am doing the transition on app delegate, at applicationDidEnterBackground. tried at every single other back/fore transition method, same results. The code works fine, but theres a flash of the screen before the login screen showing up.
The full code is as follows:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
if (!([LogicCore loadPass] == nil || [[LogicCore loadPass] isEqualToString:#""])) //a password is set,
{
[self.window.rootViewController dismissModalViewControllerAnimated:YES];//go back to the rootview, the login screen
}
}
I forgot about this, but you can have your app exit when the user backgrounds it. The only real problem here is they see your splash screen again while the app loads.
To have your app exit when backgrounded (suspended) put the key "Application does not run in background" - raw key: UIApplicationExitsOnSuspend to YES.
Not an ideal solution, but the only one I can find at the moment.
Try making the call in applicationWillEnterForeground: which "lets you know that your app is moving out of the background and back into the foreground, but that it is not yet active."
You're doing it after the transition has occurred (the change will be "queued" to happen after displaying the view)—you want to do it before, so you go with the will methods.

progressView doesn't work

i want to show a progressView when the app is launching (with the splash screen) so, in the appDelegate .h file i declared a progressView :
UIProgressView *progView;
and in the appDelegate .m file i did this :
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
sleep(1);
progView = [[UIProgressView alloc] initWithProgressViewStyle:UIProgressViewStyleBar];
progView.progress = 0.75f;
// the rest..
}
i didn't see the progress bar in the splash screen, help please, thx in advance :)
You cannot influence the splash screen itself, it's only a static image.
What you can do however is to let your app load until you get the delegate message, then fire up some basic UI which shows your progress view and then do some heavy startup process in a background thread, updating your progress bar accordingly. When the thread has finished its work, call back to the main thread to start the real application.
Edit: Updated my answer to clarify some things
The application startup in iOS happens basically in 2 phases.
The operating system launches your application, the app sets up basic stuff, starts the run loop, etc. During this time the splash screen is shown. During this phase the application is not under the control of the programmer.
-application:didFinishLaunchingWithOptions is called. In this method you as a programmer may do some startup tasks, such as loading the database, connecting to some service, etc. During this phase you can influence the user interface and also show for example a progress bar. If you need to do this, create a background thread and do the setup work there.
Note: It does not work if you just put a progress bar on the UI and then do some stuff in the -application:didFinishLaunchingWithOptions: method, since the UI is not shown until after this method has returned. That's why you need to do the setup in a background thread.
is that all the code for progView? Cuz you haven't added it as a subview to anything, so how would you be able to see it? You need something like:
[splashView addSubview:progView];
frenetisch gave some reasonable explanation of the startup process of an IOS app, but it sounds like you need to do some basic investigation into this. It generally takes very little time before your didFinishLaunchingWithOptions is called and you can show your first view. But what things do you then need to do before your app is actually usable? You want to show a progress view so there must be SOMETHING that you plan to do that takes some time. It's while THAT is going on you need to show the progress view and somehow update it's progress.