Refresh view after app resumes from multitasking - ios

I have a tabBarController containing three tabs. One tab is a settings tab, it has three UISwtch's which are contained in a UITableView, and it stores the data in [NSUserDefaults standardUserDefaults]. I'm testing everything a user could possibly do and have found a small problem. When the settings tab is in the foreground and I exit the app, enter the main settings for the phone, alter the settings for my app, exit settings then restart my app, the settings tab is still in the foreground but the toggle switches do not reflect the changes made in the previous step. If i switch to another tab then back to my settings tab the changes are now reflected.
I've tried everything, viewWillAppear, viewDidAppear, [theTtableView reloadData], nothing works. The strange thing is the exact same functions are called when it resumes and the settings tab is in the forground, as when another tab is in the foreground then I select the settings tab. I just can't get it to refresh, even though viewDidAppear and viewWillAppear are both being called on the settings controller.
I know is probably unlikely a user will be in the in app settings tab, then exit and go to the phone settings tab to make a change, then back to the app, but it's driving me nuts and I need to fix this.
I've tried every suggestion I could find here, still can't make it work.
Any ideas?
Thanks

Call this
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(refreshViews) name:UIApplicationWillEnterForegroundNotification object:nil];
in your view controller's initialization (replace refreshViews with selector of the method you refresh your switches to reflect the actual values in).
Don't forget to [[NSNotificationCenter defaultCenter] removeObserver:self] in dealloc.
Alternatively you can register for the notification in viewDidLoad or viewWillAppear and unregister in viewDidUnload orviewWillDisapper respectively.

Did you try putting some refresh code in applicationWillResumeActive or applicationWillEnterForeground in your UIApplicationDelegate?

Related

Disable all notification observers in a certain viewController

I used NSNotification.Name.UIApplicationDidBecomeActive in many places in my app. but I want to disable all of them in just a ViewController for example named vc. in vc I called NotificationCenter.default.removeObserver(self) in viewWillDisappear method. but when I opened a URL in Safari and use the back-to-app button to come back to my app all the notifications were triggered again.
is there a way to disable all of the notifications in my whole application and enable them again?

download files when user press back or close the app and restart the app in iOS

I am very new to iOS and I want to download the database from the server in iOS App. It works perfectly first time when i run the app from Xcode in simulator.
After download is finished,I pressed command+shift+H to open the home screen, as there is no home/back button in the simulator. Again I opened the app but it doesn't download the database this time.
I have kept the download code in viewWillAppear and viewDidLoad both and its downloading the db twice at the same time.
My purpose is to download the file when user resumes/restart the app. I have done same thing in android, where onResume/onPause/onRestart methods are available. I want to achieve the same thing ion iOS also.
One doubt......
How to press back button in iOS simulator?
I think back button starts the app from start (At least in android) not home button. How can I test this in iOS simulator.
If you really need to download your data every time the view is shown (I really don't recommend this), remove the download code from viewDidLoad and keep the one from viewWillAppear.
command+shift+H is the right way to press the home button on the simulator.
You have to handle application states. refer below document from apple -
https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/TheAppLifeCycle/TheAppLifeCycle.html
handle those from appDelegate or
You can handle those by adding the observers like below
[[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(backgroundMethod)
name:UIApplicationDidEnterBackgroundNotification
object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(foregroundMethod)
name:UIApplicationDidBecomeActiveNotification
object:nil];

Is applicationDidBecomeActive called in every app?

I'm making a jailbreak tweak for my iOS device and have run into a problem regarding application bundleIDs and multitasking.
I currently have it set up so that when an application icon is tapped, it grabs the application ID and stores it for later. I needed to get this for multitasking so I tried applicationDidBecomeActive with no luck as it was never called when I thought it would be. Is there any way to tell when an application loads from multitasking and/or app switching?
You can register notification in your viewDidLoad:
[[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(selectorYouWant)
name:UIApplicationDidBecomeActiveNotification
object:nil];
Here are all the UIApplicationDelegate methods that may be called. Upon first launch application:willFinishLaunchingWithOptions: and application:didFinishLaunchingWithOptions: will be called when initially loading the app when it is launching for the first time/is no longer in memory. As for becoming active while still in memory I would recommend you use applicationWillEnterForeground:.

Weird behavior with button near notification center pulldown on iPad

I've got a UIButton (the 'back button') in the upper left corner of an iPad application that dismisses a view controller. I've discovered that if you tap this button slightly too high, you can both activate the button and start to pull down the notifications pane at the same time. When this happens, my -viewWillDisappear gets executed and stops the animations in the view, but the view doesn't actually dismiss. Of course, the notifications pane doesn't come down all the way, so the net result looks like my animations crashed, and that the back button failed as well.
The obvious solution would be to just move the button down a little bit, but as that is undesirable for layout reasons, I'm curious if:
Anyone has ever seen this behavior before.
If it's well-defined behavior, and if so where does Apple describe it.
Are there any known work-arounds?
EDIT: actually looks like less of an issue after all. Turns out it's my -applicationWillResignActive that's getting called, not -viewWillDisappear. Still looks bad, but at least the behavior is well defined. I'm not activating my home button at all, just pulling down the notifications pane.
I have done an UIViewController which has a timed animation (like a banner ad) going on, and bringing the notification pane down does not stop it until the pane is fully disclosured.
Maybe you have to deal with your animations on -applicationWillResignActive: and -applicationDidBecomeActive:, like pausing and resuming them.
You can receive those notifications directly on your UIViewController (instead of dealing with them on you AppDelegate) by adding the following code to your -viewDidLoad:
// Add observers
[[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(pauseAnimations:)
name: UIApplicationWillResignActiveNotification
object:nil];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:#selector(resumeAnimations:)
name:UIApplicationDidBecomeActiveNotification
object:nil];

what is the function that is called when the app is appearing?

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.)

Categories

Resources