How to save a NSUserDefault using applicationDidEnterBackground in the appdelegate - objective-c

Seems a simple question, but I'm not able to find an answer. I have some user information that I want to save locally using NSUserDefaults whenever the user presses the home button. I've read that I should put this in the appdelegate classes applicationDidEnterBackground method. But how do I make the actual data to be saved (in the view controllers) available to the applicationDidEnterBackground method?
Thanks for the help.

You have the right idea, but the code doesn't have to be in the app delegate. You can observe the notification that is sent when the application enters the background from anywhere and save your data when this notification is received.

Related

handling push notifications to different views

I receive my push notification successfully but when I tap on it, it simply takes me to first view when my app loads.
I want the user to go to specific views depending on the push notification because the user will be receiving many different push notifications (1 for each soccer team, hence possiblity of many different views).
So example: I have a view controller called manchesterUnitedView and somebody just scored so the user receives a push notification for that particular view. When they press it, I want it to take them to the manchesterUnitedView.
Does anyone know how to do this?
thanks
You can use the payload of the notification to pass the information you need to the app, then retrieve it and use it when the app is launched as a result of the user tapping on a notification.
To do this just add a custom dictionary to the JSON payload of the notification and add the information you need to pass to the app there.
See here for details on the payload format.
You can then retrieve your custom dictionary when the app is launched in the application:didFinishLaunchingWithOptions: method implementation of your UIApplicationDelegate and use it to initialize your application state.
See here for documentation of how to retrieve the payload in your UIApplicationDelegate.
I am also interested in this.
The following should work:
I receive the Push on my iPhone and after clicking the "View" Button the App should open, and then i can read the full message.
just like here on this site (bottom): http://www.myfitapp.de/fitnessstudio-app/push-nachrichten/

Reload (table)view after retrieving update from server

I'm building an iPhone app which gets updates from the web at regular time intervals and when the user is browsing through the apps data.
I have a class called updateManager which handles the updating process.
When the updating process is finished (0.1-3.0 seconds later) the data in my app could be changed but the user is still viewing the old (false) data. This causes big problems when the user is viewing a (drill-down)tableview and he selects a cell which data doesn't exist anymore.
Is there a way to access the current view(controller) from another class and reload it's (table)view? It is not possible to send the current view controller to the updateManager since the user could have changed view's while the update is in progress
EDIT:
I looked at this post: Reload a tableView from the AppDelegate but i think the problem with the user switching views while updating is still there isn't it?
Not sure that's the best solution. You get a more decoupled design if you use the Observer pattern instead. Use NSNotificationCenter to post a notification when the data changes. Then any view who is displaying data would subscribe to those notifications and can then reload the table upon such an event.
i would fire a NSNotification with the NSNotification Center
Your ViewController with the TableView listen to the Notification (and reload your table view if the notification fired) and your update Class fire the Notification if the data changed.

How to dismiss UIActionSheets and UIPopoverControllers without knowing who presented them

In my client application I have an idle timeout control mechanism and when the user does not do anything with the app for a specified time interval, I display a warning and throw him back to the login screen. This control happens in my container view where I initiate all of my other views. When the idle time is up, I pop this container view to its caller, i.e the login screen.
The problem is, if the user does sthg that displays an action sheet or a popover and then does not do anything until the idle time is up, when I throw him to the login screen the action sheets and the popovers also remain on the login screen since I don't dismiss them.
To solve this, I can think of making all the action sheets and the popovers retained members of my view controllers and then dismissing them on the viewWillDisappear methods of their owners. But I have so many view controllers, so I'm looking for other ways, if there are any.
So, the question is how can I make all these action sheets and popovers go away from my login screen without knowing who their callers are?
I would register the UIPopover instance to listen to some notification.
[[NSNotificationCenter defaultCenter] addObserver:_myPopOver
selector:#selector(myDismissPopover)
name:#"dismissPopover"
object:nil];
And add extension to UIPopover class.
- (void) myDismissPopover {
[self dismissPopoverAnimated:YES];
}
When I need to dismiss popover, I just need to post notification.
[[NSNotificationCenter defaultCenter] postNotificationName:#"dismissPopover"
object:nil];
I'll write down my own solution as we've talked with bshirley in the comments of the question.
I've implemented a mechanism like this to solve the problem:
In my login view controller, I create an NSMutableArray that will keep all my action sheets and popover controllers that are going to be dismissed. Then I store this array in a global dictionary. I access this dictionary via a utility method. Then all through the application, whoever creates an action sheet or a popover controller, adds the component to this array (retrieves the array from the global data, modifies it and then saves it back to the global data). Then when the user is thrown back to the login screen, in viewWillDisappear of my login view controller, I loop through this array and call the appropriate dismiss method by checking if the UIView I get from the array is an action sheet or a popover controller. Then I remove all the elements of this array and then store it back in the global data, again.
Hope this helps anyone who needs to implement a similar mechanism. Your comments will be appreciated.

Delay navigationController to pop detailView of UITableView

Im looking for some help regarding to put a save like confirmation if some changes where made to a UITextField and UISegmentedControl.
Can I prevent the UINavigationController from pop the view? And then pop based on buttons in a AlertView?
I use the UITextField and UISegmented control to POST data to a webservice.
I perhaps need to use a modalView for this? but wanted first to see if someone have another idea, because I would like to keep navigation clicks down if possible.
Any suggestions for this?
Thanks,
Why not just using a UIAlertView?
EDIT: On second thought, and re-reading your question + comment, I would recommend to use a Modal View with classics OK/Cancel buttons + a UIAlertView(s) for confirmation(s). (UIAlertView "poping" on OK/Cancel is easy to do via UIAlertViewDelegate)
That's what Modal views are for, block UI until some user action has been completed. Like a form. This is how I do all my forms, and how Apple does (just look at the create mail screen for an example, or any form of iOS apps)
Adding a "Magical" action requiring user interaction on the back button of a navigation controller is bad in terms of user experience, if you hit back, you expect the view to pop, nothing else. I would then be surprised if Apple SDK even allows to cancel that event...
You can do what you would like without the need of a modal view.
First, you can use your text field's UITextFieldDelegate to set a flag in your controller when the field content is modified. You can reset this flag when the data is sent out.
Then you could override your UIViewContorller's viewWillDisappear to show an alert to the user in case new data have not been posted at the moment the view is going to disappear and give him the possibility of sending it to the server. This method will be called when you move to a different controller in your navigation UI, and you will not have a chance to "reject" the operation.

how to load application on button event of push notification in objective c?

I wanted to know whether it is possible to load the application or some specific page on clicking the "OK" button of the push notification or not in objective c.
As i want to load the application when i press the button of the push notification.Kindly help me out if possible.
Thanking you.
the push notification usually have a "View" button that will automatically launch your application. you don't have to do anything in your app for that to work.
when the user launches the application that way, your app delegate receives a call to
- (BOOL) application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions,
the launchOptions dictionary containing whatever data was added to the push notification payload by the server.
you can check that and open any relevant page or view, or handle that notification anyway you want.