Keep track of launches bug - iphone

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?

Related

Loading ModalView only on first open

I've created a modal view that loads on top of my tabBar to serve as a 3 step welcome screen.
While all of that works OK, the problem I'm having is finding a way to load it only once, so that the user doesn't have to deal with a welcome message on every single load.
I've done some research, and it looks like I might be able to call a method with NSTimer, but I'm not sure if that's a proper way to do it.
In your app delegate, set Bool and save in NSUserDefault Check if BOOL is set. If not then present view modally also set the BOOL.
Code might look like this:
In your appdelegate implementatuin. Application didFinishLaunching method:
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"FirstTimeBool"])
{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"FirstTimeBool"];
// present view controller modally after this
}
You can store this information in NSUserDefaults that exactly what's it is for :
At first start :
[[NSUserDefaults standardUserDefaults] setValue:[NSNumber numberWithBool:YES] forKey:#"IS_FIRST_LAUNCH"];
Then to know if it is the first time you run the app :
BOOL isFirstLaunch = [[[NSUserDefaults standardUserDefaults] valueForKey:#"IS_FIRST_LAUNCH"] boolValue]
Hope this helps,
Vincent

How to make a favourite list iphone

I am making an app that has two tabs, one of which is a "favourite" tab. In the other tab called "search", I have a list of results displayed in a table view, when you tap one, you get to see the detail of that particular result. What I'm trying to do here is, there is a button in the detail view, when it is pressed, the current result gets sent to the "favourite" tab. I tried to use delegate to pass the information, but it didn't work out. Here is my code:
DetailViewController.m
-(IBAction) addSomething {
[self.delegate detailViewController:self addToFavourite:self.something];
}
FavouriteViewController.m, implement the delegate method:
- (void) detailViewController:(DetailViewController *)detailViewController addToFavourite:(Something *)something{
detailViewController.delegate = self;
[thingsList addObject:something];
[theTableView reloadData];
}
Everything is built and fine, but when I click "add" button in the detail view, the data doesn't get sent to "favourite" tab's view. Can anyone help me with this one? Do I need to use core data in this case, I never used core data before. Thanks.
One way to do it that is drop dead easy is storing your information in NSUserDefaults.
NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
[defaults setObject:thingsList forKey:#"myData"];
[defaults synchronize];
When you load your data up in your other tab, just fill it from user defaults.
NSUserDefaults *defaults = [NSUserDefaults standardDefaults];
otherData = [defaults objectForKey:#"myData"];
I probably abuse user defaults more than I should, but it makes for a very easy way to store your data locally and have it persist between runs. It will also backup with iTunes for added persistence. Hope this helps.

Getting variables from one view to another

I realise that there are other topics like this, but none of them really help. I'm trying to get variables from one view into another, but I have absolutely no idea how.
To give some backstory, my game is a fruit ninja like game where stuff goes on the screen and you have to slice it. If 3 sprites leave the screen unsliced, the game is over and it flips to the game over view screen with a button to go back. Additionally, this SHOULD go to the "flipSideView", which is the highscore, but my implementation of the flipSideView transition doesn't work. This isn't the main issue, the main issue is that I don't know how to get the score from the game in the mainView (which stores it as an int) into the flipSideView (which has the player name).
The main view changes to the gameOverView through this condition in the tick method (which performs regular checks and methods for the game)
if (lifeCounter < 1)
{
gameIsOver = YES;
[self showInfo:0];
[self viewGameOverScreen];
}
That goes to the gameOverView, which will sit there until the replay button is pressed with:
- (IBAction)replayAction:(id)sender
{
[self.delegate gameOverViewControllerDidFinish: self];
}
gameOverViewControllerDidFinish restarts the game and dismisses the view.
- (void) gameOverViewControllerDidFinish: (GameOverViewController *) controller
{
[self restart];
[self dismissModalViewControllerAnimated: YES];
}
The restart method just resets the 3 primary values in the main view (The score, the level, the lives).
As it restarts, the game should take the score and whatever name is stored in the text field in the flipSideView (which can be viewed at any one time gameplay) and store it somehow for future reference. It's not supposed to store it in a file yet because that's next week's task.
I don't wanna post the entire program due to plagiarism issues, but if there are additional parts that might make it easier to understand, I will definitely post them.
Thanks in advance,
Christian
Use Search
NSUserDefaults *currentScore = [NSUserDefaults standardUserDefaults];
[currentDefaults setFloat:yourScoreVariable forKey:#"HighScore"];
[currentDefaults synchronize];
Above to store the score.
Below to retrieve the score.
CGFloat fltHighScore;
NSUserDefaults *currentDefaults = [NSUserDefaults standardUserDefaults];
fltHighScore = [currentDefaults floatForKey:#"HighScore"];
Suggestion:
You could put the variable in your Controller class,where they both could access.
It is always better to have sharable data in Controller then views if data has to be shared among views.
Update the shareable data through delegate method.
You can use your app delegate for this. You need to declare your variable there, and then you can access this in the following way throughout your whole app:
TestAppDelegate *appDelegate = (TestAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *string = [appDelegate.Placemarks objectForKey:appDelegate.title];
Don't know if it's the best way tough...
Luck with it!

Why won't my NSUserDefaults load?

So I have an iphone app that presents a modal view when it starts if the user has not registered. Once they register, it saves a user ID into NSUserDefaults. When the app starts at a later time, it needs to load this user id in the appdelegate file.
//Registration.m
NSUserDefaults *savedUID = [NSUserDefaults standardUserDefaults];
[savedUID setObject:UID forKey:#"user_id"];
//AuctionTracAppDelegate.m
NSUserDefaults *savedID = [NSUserDefaults standardUserDefaults];
NSString *uidString = [savedID stringForKey:#"user_id"];
My problem is that when I try to load this value at a separate execution, the object I saved is always null. I know for a fact that it is saving the string object correctly. Also, this weird behavior JUST surfaced. This exact code was working earlier, then after working on a totally unrelated file, this code fails. I'm clueless as to what happened. Any hints?
I believe that you need to call the synchronize method.

Can I save my app's state at close time in iPhone OS?

I have an app that functions much like an ebook. I have a bunch of textual information in various languages that is accessible through a number of drill down methods. When a user wants to get into where they were reading last, they currently have to navigate through the section and chapter menus to get back to where they were. An ideal solution for this would be to setup a bookmark system, which I am considering.
But if I remember correctly, when iPhone OS 4 was announced, they seemed to make a big deal of the added ability to save the state of an app. Does that mean that someone using my reader app would be able to just exit right out, do whatever, and then when they came back in, it would be the reading screen just as they left it?
I don't know much about how to setup a bookmarking system, I suppose it would be worth investigating, but I would probably want to just hold off for iPhone OS 4 if that is indeed what it will be capable of doing. Any thoughts or insights would be appreciated!!
This has always been possible in the iPhone OS. Most people use NSUserDefaults to store their application's state. Then in your application delegate's -applicationDidFinishLaunching method, or wherever you set up your views and initialize your application state, simply restore the state that you saved using NSUserDefaults.
As others have pointed out this is not particular to OS4 and it is good practice to implement this in all apps.
I would suggest that when a user turns a page in a book for example you save a reference to that page using NSUserDefaults as follows:
NSUserDefaults *userPreferences = [NSUserDefaults standardUserDefaults];
[userPreferences setObject:currentPageNumber forKey:#"pageNumber"];
When the application launches you can read this value and act accordingly.
[[NSUserDefaults standardUserDefaults] stringForKey:#"pageNumber"];
You may prefer only to store the reference when the application is about to close but it is better practice to store it earlier (in case the application quits unexpectedly)
Also, you mention that users navigate through a number of "drill down methods". You may want to keep a record in a NSMutableArray of where exactly the user is in the app and save this out to NSUserDefaults too. When your app launches you would process the information in this array to get the user back to where they left off.
What they meant for save state is for background application. A ebook app probably won't need to run in the background so you'll still have to save and restore state just like in OS 3. NSUserDefaults works well for this.
if you are using navigation controller then you can use the following line of code to save you app state:
NSMutableArray *listOfClasses = [[NSMutableArray alloc] init];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:#"ViewControllers"];
NSArray *controllers = [_navController viewControllers];
for(UIViewController *VC in controllers)
[listOfClasses addObject:[NSString stringWithFormat:#"%#",[VC class]]];
[defaults setObject:listOfClasses
forKey:#"ViewControllers"];
[defaults synchronize];
at the time of termination of application did enter in background
and you can use these line of code to restore your app:
NSArray *classes = [[NSUserDefaults standardUserDefaults] objectForKey:#"ViewControllers"];
if(classes != nil) {
for(NSString *class in classes) {
if([class isEqualToString:#"FirstViewController"]) {
FirstViewController *viewController = [[FirstViewController alloc] init];
[controllers addObject:viewController];
[viewController release];
} else if([class isEqualToString:#"SecondViewController"]) {
SecondViewController *viewController = [[SecondViewController alloc] init];
[controllers addObject:viewController];
[viewController release];
} else if([class isEqualToString:#"ThirdViewController"]) {
ThirdViewController *viewController = [[ThirdViewController alloc] init];
[controllers addObject:viewController];
[viewController release];
} else {
}
}
}
at the time of application did finish loading