I am new to mobile development. I would like to show a different screen if GPS is not enabled. I have put the code in the view did appear to show the new screen this works most of the time. However when app returns from background the new screen is not shown. After debugging i found that when the app returns to foreground Viewdidload/viewdidappear/the constructor of the controller is not called.
Is there an override which I can use to when the app returns from background on the controller. Also after research I found this link
My Research
If this is the way forward, can someone help me convert this code to Xamarin ios.
Thanks in advance.
In Xamarin IOS , adding Notifications in ViewDidLoad Method , can do that in ViewDidAppear.
public override void ViewDidLoad ()
{
base.ViewDidLoad ();
UIApplication.Notifications.ObserveWillEnterForeground ((sender, args) => {
Console.WriteLine("Welcome back!");
//Add code from ViewDidAppear method here
});
}
Here is the IOS LifeCycle document.
You can also use default notification center and then call your ViewModel methods.
NSNotificationCenter.DefaultCenter.AddObserver(UIScene.WillEnterForegroundNotification,
notification =>
{
ViewModel.ViewAppearing();
});
Bonus:
If you'll put this code in ViewDidLoad then it will add multiple observers which will cause the observer to trigger multiple times.
Save NSNotificationCenter.DefaultCenter.AddObserver return token and then in ViewWillDisappear you can just dispose the token. It will now not called multiple times now.
Related
I have been writing an app that involves updating several values depending on the time the user is in - I update these values using viewDidAppear but unfortunately this function is not being called when the app is loaded again after being sent to the background.
Is there any way to prevent the app from being sent to the background? Or to force the app to open on a certain page after being opened from the background? Or is there a function that is like viewDidAppear, but is always whenever the app is loaded from the background?
Thanks!
Note:
I tried this:
func applicationDidBecomeActive() {
viewDidAppear(false)
}
inside the viewController class, but it didn't work.
You may be looking for applicationWillEnterForeground(application: UIApplication). This method has to be implemented in AppDelegate, not in a controller
I can't find information about how to detect when the iOS breadcrumbs are used to return to the app. I am looking to call a function on the controller when the view is active again specifically when this breadcrumb is used (Our use case being when location has been enabled externally).
I have tried using viewDidAppear but this isn't called. Is it possible? I find it unusual that this isn't called.
To be clear the breadcrumbs I am talking about are
I think you can use this method on Appdelegate
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
then send a notification to your viewcontroller you want to handler.
Since apple watch is just an interface. Can I use background refresh to update the apple watch screen while the app is in the background mode not in foreground?
And if yes, how can I do that?
You can change the screen when for some reason a page deactivates, like for example, switching to another page. You should be using the method:
- (void)didDeactivate {
// This method is called when watch view controller is no longer visible
[super didDeactivate];
}
Which is on every WKInterfaceController class.
We now that in monotouch and for iPhone / ipad application when we want to have splash screen before app lunch we should to set launch image in info.plist file and it will show this image before application launches.
But what is the best way to implement a splash screen when we want to have a splash that runs some heavy codes in background and not disappear until these operations had not completed? Some codes like downloading application config from internet and saving theme that often used in splash screen.
Possible solution:
Make a SplashViewController, which contains same image as app's splash image. Also it contains UIActivityIndicatorView;
In AppDelegate's FinishedLaunching method make new instance of SplashViewController, set it as window.RootViewController, call:
activityIndicator.StartAnimating();
Runs some heavy codes in background;
When it's done, set window.RootViewController to ViewController, which is app's starting point.
BTW, there is another solution: create main UIViewController, set it as Window.RootViewController immediately in AppDelegate's FinishedLaunching method. Then create and show modally splashViewController by this code:
...
MainViewController.PresentModalViewController(splashViewController, true);
...
Hiding modal UIViewController is possible via calling code:
DismissModalViewControllerAnimated(true);
Note that since iOS 6 PresentModalViewController becomes deprecated method. So, for many iOS versions compatibility you could code special method for showing modal UIViewController.
public void ShowModalViewController (UIViewController vc, bool animated)
{
if (UIDevice.CurrentDevice.CheckSystemVersion(6, 0)) {
MainViewController.PresentViewController(vc, animated, null);
} else {
MainViewController.PresentModalViewController(vc, animated);
}
}
Imagine the app is running and you press the iphone button (the phone button) and you exit the app. then you tap on the app again to enter the app. My problem is that when ever the user does this I want the viewWillAppear or viewDidAppear functions to be called, but unfortunately none of these functions gets called.
I want to know if these function won't get called, then what is the function that is called when the app is appearing again?
How about - (void)applicationDidBecomeActive:(UIApplication *)application in your UIApplicationDelegate?
Look at UIApplicationDelegate. -applicationDidBecomeActive: is what you are looking for.
You can also register for notifications in your classes (UIApplicationDidBecomeActiveNotification). This may be simpler to implement than having your app delegate handle everything since you can have, for example, each view controller manage itself.
(Use NSNotificationCenter's -addObserver:selector:name:object: to register, don't forget to unregister during object cleanup, typically in -dealloc.)