Update UI on settings change - Objective-C - iphone

For my application, I am storing a url in a settings panel so the user is able to edit it.
After the application is already open, I want to take care of the use case where the user presses the home button to background the application, goes into the settings panel, changes the url, and then wakes the application back up. The problem I am running into is when the application wakes back up and I try and get the value from the settings again, it is the same. The application only notices the change if the app is fully exited and then restarted.
Currently I am reading the settings like this:
NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
NSString *url = [settings stringForKey:"#url"];
This is wrapped inside of a function that gets invoked when UIApplicationDidBecomeActiveNotification gets fired.

Try to synchronize the defaults after setting the value :
[settings synchronize];

Related

How to make scollView (for T&C) only show up once at begin of app, then never show up again?

My questions is...how to make....this...
I am trying to make a scollview to show the term&condition at beginning of my app when the user is 1st time using the app.
if the user accepted the T&C (by clicking accept button), this T&C scollview will never show up again at beginning of the app, as he already accepted. So he will be free to use the app in future.
How do I implement this? any suggestions?
Use NSUserDefaults with a key like "TCShown". If the key does not exist in the NSUserDefaults at the beginning of the launch, you show the T&C and create "TCShown" value, set it to YES ([NSNumber numberWithBool:YES];) and store it to the NSUserDefaults.
Edit:
Assuming that you want to present the T&C in your first viewController,
#define kTCViewedFlag = #"tcViewed"
-(void) viewDidAppear {
NSUserDefaults *myDefaults = [NSUserDefaults standardUserDefaults];
if(![myDefaults objectForKey:kTCViewedFlag]) {
//show the TC
}
}
-(IBAction) userAcceptedTC {
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:kTCViewedFlag];
[[NSUserDefaults standardUserDefaults] synchronize];
//dismiss the scrollView
}
-(IBAction) userDidDeclineTC {
//handle refusal of TC
}
In addition to Kaan's answer, you can add the TCShown field to the server and update the values accordingly. This will take care of the case when the user who has already accepted the T&C's logs in from a different device.
Maybe you'll find this useful: RLAgreement View Controller
This project allows developers to include
and Agreement, Terms of Service, Non Disclosure Agreement, etc. to an
iPhone App. The controller stores a variable in the user's settings
when the user has a valid agreement and it checks every time the user
opens the App.

How do i update webview from settings preferences

very simple question. I have an app with a single view (webview) that loads a url. The app has a settings bundle attached to it with one field asking for a string. Here is the code in my viewcontroller:
-(void)viewDidLoad{
[super viewDidLoad];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *settingString = [prefs stringForKey:#"name_preference"];
[prefs synchronize];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.stackoverflow.com/%#", settingString]]]];
}
So Far So good right. It takes the string from settings and concatenates the end of the url and all is well. when i run in the simulator i have to close the sim everytime to update the view if i change the string in settings. Is there somewhere else i should put this code instead of "viewDidLoad"? I want my webview to update everytime i re-open my app from settings. Thanks for all of your help....
You can put it in viewDidAppear (in the vc) or applicationDidEnterForeground in your app delegate.
The problem you're having is when you 'launch' the app by tapping it on the home screen, you're actually just using iOS's multitasking to switch back to it (it never quit).
What Alex said will work. But Apple will never accept an app with only one UIWebView. I hope there's more to your app if you're hoping to publish it.

Remember Apps last screen in iPhone app

I want to remember the last screen of my app, means if I reopen my app, app should navigate me to the last screen where I were just before exiting app.
Thanks
Saurabh
Take a look at the NSUserDefaults API.
You could save an object for the current screen in the NSUserDefaults then check for that object on launch.
Create a NSMutableDictionary object when a view loads, and change it's value each time, and store it in the user defaults.
NSMutableDictionary *currentScreen = [[NSMutableDictionary alloc] init];
[currentScreen setObject:#"AboutPage" forKey:#"screen"];
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
[standardUserDefaults setObject:currentScreen forKey:#"lastScreen"];
Then when the app loads, check the user defaults "lastScreen" key and load the appropriate view.
Hope this helps.
in iOS4 it's done automatically, because when you quit your application it's actually enters the background mode and doesn't quit. So when you "launch" it again, it opens at the same place the user left.
Previous versions of iOS don't have this feature and you should take care of it by yourself.
Depending on your application, you may want to think about using the Three20 library. This library has the sort of persistence you are looking for built in.

How to quit an iPhone app nicely?

or "How to simulate a home button pressed event?"
I need to restart my iPhone app, and I want the program to quit, so the user will only have to start it.
If I simply use exit(0) some changes won't get saved, as they would if the user quits by pressing the home button.
The restart needed for language change.
Related code:
- (void)onChangeLanguage: (id)sender {
NSArray *lang = [NSArray arrayWithObjects:((Whatever *)sender).newLanguage, nil];
[[NSUserDefaults standardUserDefaults] setObject:lang forKey:#"AppleLanguages"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defaults objectForKey:#"AppleLanguages"];
NSString *currentLanguage = [languages objectAtIndex:0];
NSLog(#"Current language: %#", currentLanguage);
// ***
}
If the user restarts using the home button, language will change.
If // *** is replaced by exit(0), the language won't change.
Calling exit(0) is the only legal (but highly not recommended) way to exit the program from your code.
As a possible workaround you can show UIAlertView with no buttons that cannot be dismissed (forcing user to quit your program manually) and telling the user that he has to do that to apply your changes.
I think it’s perfectly fine to call exit, just call [[NSUserDefaults standardUserDefaults] synchronize] before you do that. You can read about the synchronize method in the Apple Documentation:
Because this method is automatically
invoked at periodic intervals, use
this method only if you cannot wait
for the automatic synchronization (for
example, if your application is about
to exit) or if you want to update the
user defaults to what is on disk even
though you have not made any changes.
It's worth noting that there's a private API call, too. Of course, all the usual warnings about using private APIs apply.
[[UIApplication sharedApplication] terminate];

applicationWillTerminate works as long as I don't switch off the iPhone

to save some variables of my apps I use:
-(void)applicationWillTerminate:(UIApplication *)application {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setFloat: self.viewController.sLabel.contentOffset.y forKey:#"floatKey"];
[prefs setObject:self.viewController.newText forKey:#"stringVal"];
[prefs synchronize];
}
and to retrieve them, via a button, I do the following:
-(IBAction) riprendi:(id) sender {
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
float myFloat = [prefs floatForKey:#"floatKey"];
//some actions here
}
Everything is working on the simulator. However, using it on a real iPhone, the variables' saving and retrieving works just if you press the Home button, exiting the app and opening again but NOT if you switch off/on the iPhone. In this case, the variables get simply lost once you re-open the app...
What am I missing?? This is actually driving me crazy :(
Thank you so much ;)
Fabio
If you mean hitting the lock button on the top of the phone by saying "switching on/off", then it won't work, because locking the phone does not cause an application to quit.
Your applicationWillTerminate: method is only called when you exit you're application to the home screen or to some other application. When the user presses the Sleep button, applicationWillResignActive: will be send to your application-delegate.
Apple's iPhone OS Programming Guide has a section on handling interruptions.
According to Apple's documentation
"NSUserDefaults caches the information to avoid having to open the user’s defaults database each time you need a default value."
If you want to make sure things are saved you should call "synchronize" on your prefs.