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.
Related
As suggested here "https://developer.foursquare.com/overview/auth" I get the access token in a webview and then I close the browser by after getting the accesstoken:
- (void)webViewDidFinishLoad:(UIWebView *)webView {
NSString *URLString = [[self.webView.request URL] absoluteString];
NSLog(#"--> %#", URLString);
if ([URLString rangeOfString:#"access_token="].location != NSNotFound) {
NSString *accessToken = [[URLString componentsSeparatedByString:#"="] lastObject];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:accessToken forKey:#"access_token"];
[defaults synchronize];
[self.webView removeFromSuperview];
}
}
a similar example "https://github.com/anoopr/core-data-talk/blob/master/example/Classes/FoursquareAuthViewController.m"
But once I remove the webview, I get a dark screen. Which makes sense because I dont have a view anymore. But i probably should do something like [self.view addSubview:self.Anotherview], I am not sure what that Anotherview should be. Can I just segue to another ViewController?
I am pretty new to IOS so any help would be much appreciated.
You can put whatever you want after removing the UIWebView from the view. For example, if you just want to display a map, you can add a map view, or even easier, you can have a map view and a web view on top if you are using the interface builder, and removing the web view will leave you with the map view. But if what you have on the web view's view controller (the view controller that you've put the web view) is irrelevant of what you are trying to achieve after you get the access token, the cleanest way is just storing the access token somewhere else (e.g. App delegate class) performing a segue to another view controller, and using the token from there for whatever you need. You don't even need to deal with removing the web view from superview, you are already leaving that view controller and segueing to another one. Especially, when you are a beginner, as long as you can use storyboards, segues, and NIBs, go with them, try to do stuff in Interface Builder visually rather than trying to remove/add views/viewcontrollers (yes, it is also possible) within the code.
I found some threads that discuss this and implemented it into my code, however I'm having an error.
I am trying to do something every 5th launch.
Also the code sets launchAmounts, is that built in into userDefaults, or do I have to declare this somewhere?
I am doing this from viewwillappear in my main view controller.
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
NSInteger appLaunchAmounts = [userDefaults integerForKey:#"LaunchAmounts"];
appLaunchAmounts = appLaunchAmounts %5;
NSLog(#"app has been launched = %d", appLaunchAmounts);
[userDefaults setInteger:appLaunchAmounts+1 forKey:#"LaunchAmounts"];
if (appLaunchAmounts==0) {
That code looks like it should work, except for the fact that you have it in viewWillAppear. That method could be called many times in one run if you're switching back and forth between different view controllers. You should put it in the applicationDidFinishLaunching method in the app delegate.
What's not working the way you have it now?
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.
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.
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.