Apple watch app entry point - ios

In xcode you need to specify the initial interface controller for the watch app, which is the entry point of the watch app, shown first when you open it.
But is it shown every time you open the watch app? For example you open a watch app, navigate to a page, close it, and open it again. Does it open on the page you were last time (like on iOS), or again on the first interface controller?
According to apple documentation:
Normally, WatchKit displays the first interface controller in the sequence initially.
Well, normally is not every time. I looked through watch app videos from the watch presentation event, but there wasn't a case when they opened an app twice.

That's a great question!
Main Entry Point
First off, you can certainly avoid showing that MainInterfaceController each time. See this thread for more information where I detail exactly how to use that entry point to launch the appropriate set of InterfaceController objects.
Watch Extension Lifecycle
It is VERY important to understand what the expected lifecycle of a Watch Extension actually is. It will only run while the user has the Watch up and is running your app. This will generally be 1-5 seconds (opinionated value). As soon as the user lowers their wrist, your Watch Extension will be terminated completely. Therefore, it is going to be restarted every time at the same entry point. This means that you need to track your app state if you want to launch a different page set in the MainInterfaceController.
Hopefully that helps shed some light.

If your WatchKit extension is still running, it will pick up where you left off. If not, and everything has been dumped out of memory, it should start again with your initial interface controller.

Related

Will application Relaunch after app goes background

If I send my app to background by clicking the home button and wait a certain time (perhaps 1 or 2 hours).
What will happen if i tap on the app icon now?
Will the app relaunch or simply be brought from background to foreground?
Quick "prologue":
Welcome to the wonderful world of stack overflow (SO), I myself am rather new here, but found it much friendlier to use welcome you anyway!
Just in case you haven't: Before you ask a question please look around a bit on SO in case someone else has asked the same thing, but if you can't get your question answered from that, then you should of course ask your own question.
Answer:
This question has no definite answer, because it depends. When you tap the home button your app enters, as you've said, the background and is still running to a certain degree. However after done so, the apps life cycle is up to iOS (the devices operating system) to determine. iOS controls and checks memory and CPU usage (etc..) of the device, and if you start another activity while your app is in the background that makes the available memory and CPU etc of the device not sufficient, iOS will terminate any apps in the background to not waste those resources (or battery etc). If so your app will relaunch next time you tap on it.
Although if you don't do anything performance heavy it is more likely that iOS keeps your device running in the background.
I'm not sure about the exact conditions and such the iOS works on, but i would say its very likely your app will have gotten terminated and is relaunched after 1 or 2 hours "in the background". Additional conditions apply if the device is locked during that period.
For proper documentation of this i would recommend reading Apples documentation for handling App State Transitions and/or the api for UIApplicationDelegate on apples developer website. Where you can see what the different methods in the AppDelegate does and how they interact.
Edit (answer to comment):
A way to relaunch the app everytime it goes into background?
Hm, yes, but also no. I'm not 100% sure about this (never encountered that "wish" before), but you can do this in your AppDelegate: (It will basically crash your app, but beware that apple does not encourage you to crash your own app anywhere). Doing this might stop the app from passing through apple store review process (i.e. your app might not be accepted to the App Store).
func applicationDidEnterBackground(_ application: UIApplication) {
exit(0)
}
Check out the answer to these post for a bit more information: call exit(0) in iphone app , objc - Proper way to exit iPhone application?.
Personally I would recommend you to work around it and don't do this. Also remember that when your app will enter the background applicationWillResignActive will be called and when the user opens it again, applicationDidBecomeActive gets called so you can do a reload or something from there if you want to refresh any data.
Whenever we press the home button in our device, the application releases the currently being used memory and moves to background stage. However, when we press this application app icon again it brings the app on top of the iPhone screen and occupies memory again. This is definitely not the relaunch because relaunch depends upon the UIApplicationDelegate method
didFinishLaunchingWithOptions. When we are switching app from background to foreground then applicationWillEnterForeground method fires.
For better understanding, the following link might be useful
iOS Application Life Cycle

Killing an iOS app programmatically

I have an app when a specific action fires I close the app and ask the user to reopen it again.
The problem is when the app executes exit(0) the app stays in the apps stack (when I click the home button twice). I want it to be killed completely so that viewDidLoad() will be executed again when the app opens.
You cannot terminate an app on it's own.
From Apple's Human User Guidelines...
Don’t Quit Programmatically
Never quit an iOS application programmatically because people tend to
interpret this as a crash. However, if external circumstances prevent
your application from functioning as intended, you need to tell your
users about the situation and explain what they can do about it.
Depending on how severe the application malfunction is, you have two
choices.
Display an attractive screen that describes the problem and suggests a
correction. A screen provides feedback that reassures users that
there’s nothing wrong with your application. It puts users in control,
letting them decide whether they want to take corrective action and
continue using your application or press the Home button and open a
different application
If only some of your application's features are not working, display
either a screen or an alert when people activate the feature. Display
the alert only when people try to access the feature that isn’t
functioning.
Have a look at this answer: https://stackoverflow.com/a/8491688/742298

watchOS 3 app restarts after tapping complication

I have a simple "timer" watchOS app that uses hierarchical navigation. I can press the digital crown to return to the watch Springboard, then tap the app icon and be returned to the same interface controller I was using.
If I return to the watch face and tap my app's complication, the app is launched, but appears to have restarted: I lose my current state. Is there any way to prevent this?
It sounds like you're asking how to stop your app being swapped out of memory?
If so, just like on iOS this isn't possible - and obviously the watch has less memory than a phone so is more likely to be swapped out.
What you need to do is store your state in some persistence layer - e.g. NSUserDefaults - so when the app restarts it can reload its state
If you open your watch app by tap complication, the app will automatically back to root interface controller(s). This is a system forced behavior.
If you want to preserve previous state, you need to change your app's hierarchic to page-based interfaces.
User interaction in page-based interfaces can be just like navigation-based interfaces. You can replace pushController(withName:context:), pop() and popToRootController() methods to becomeCurrentPage(). I also found switch from one page interface to another is also faster than navigation (push/pop) to another.

Start app and navigate to the view previously displayed

I have an app (iOS, Swift) which is usually started in the morning by the user. The user will press a couple of buttons, enter some text, move to other views (using a navigation controller) and so on. When everything is filled out the right way, the user will put the iPhone into standby mode. As far as the app life cycle is concerned, it will enter the background state. When the user opens the app again, he will still be on the same page, with the same parameter, ... Everything is fine.
In case of a suspended app (due to a lack of resources or other stuff), the app will start again from the beginning and not from the view where the user has navigated to before. In such a case, what's the best way to navigate to the specific view the user was when bringing the phone into the standby mode and keep respectively build up the whole navigation stack as it was before the suspension?
You can use Apple state restoration and preservation technique for this purpose. Here you can find an example provided by Apple.
Hope this will help.

Reset View hierarchy iOS

I'd like to add some code to the app delegate that resets the view hierarchy back to the beginning.
My app is basically a demonstration mockup, and I'd like EVERYTIME the app opens that it resets to the first view in the storyboard, and doesn't remember what page the user was on when they closed or 'minimized' the app.
I'm using iOS sdk 8.1, and Xcode 6.
Putting aside that it's actually quite bad user experience - it's very easy to do. You just need to specify that your app doesn't run in the background, and each time user closes app, next time - brand new copy will be launched.
Here is what you need to set in your project properties in Xcode:
If your'r info.plist Show raw keys/values option are enabled, the property is named UIApplicationExitsOnSuspend which you can get by right clicking on the empty space of info.plist properties table and selecting Add Row option as follows
and right after that you will be presented with following option,
where you can select second options which is Application does not run in the background.
Selecting the mentioned property and setting it to YES, the app opt out of background mode and it cycles between the not-running, inactive, and active states and never enters the background or suspended states and moved back to the not-running state. In other words, iOS will not preserved any states which allows the app to run as fresh on next launch.

Resources