UIActionSheet Controller crashed on iPad and Works Fine on iPhone - ipad

I've been trying to work a UIActionSheet into my App. The idea is, if you exited the app in a certain state, it would popup on launch and ask you if you'd like to continue or reset. Here's the code:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
continueYesNo = [prefs boolForKey:#"keyContinueMeeting"];
if (continueYesNo) {
NSString *message_continue = [[NSString alloc] initWithFormat:#"Do you want to Continue the Prior Meeting"];
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:message_continue
delegate:self
cancelButtonTitle:#"Reset"
destructiveButtonTitle:#"Continue"
otherButtonTitles:nil];
[actionSheet showInView:self.view];
[actionSheet release];
[message_continue release];
}
This is in viewDidLoad. And the actual Code action is:
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
Wasted_TimeAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if (buttonIndex != [actionSheet cancelButtonIndex]) {
delegate.continueMeeting = YES;
[prefs setBool:YES forKey:#"keyContinueMeeting"];
[ [NSUserDefaults standardUserDefaults] synchronize];
} else {
delegate.continueMeeting = NO;
[prefs setBool:NO forKey:#"keyContinueMeeting"];
[ [NSUserDefaults standardUserDefaults] synchronize];
}
}
It all seems pretty straight forward, but for some reason it runs on the iPhone with no problem but on the iPad the program crashes at this point.

Ok this one get's a big fat DOH! I had incorrectly implemented the iPad XIB, after redeveloping them this works as designed.

Related

Xcode NSUserDefaults settings lost when app is force quit

I'm using NSUserDefaults to save BOOLs that denote whether a mapped annotation is a "favorite". The BOOLs are saved correctly when the User quits the app and then re-launches it. However, when the User force quits (press the Home button twice and swipe up on the app) the app, the NSUserDefaults are lost. Can someone please explain why this is happening? I'm using Xcode v7.0.
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
// If annotation is displayed, the User can save/clear annotation as a favorite
if ((_acmeMotorsIsDisplayed == YES) && (_acmeMotorsIsFavorite == YES)){
_acmeMotorsIsFavorite = NO;
[[NSUserDefaults standardUserDefaults] setBool:NO forKey:#"acmeMotorsIsFavorite"];
}
else if ((_acmeMotorsIsDisplayed == YES) && (_acmeMotorsIsFavorite == NO)){
_acmeMotorsIsFavorite = YES;
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"acmeMotorsIsFavorite"];
}
}
-(void)loadUserDefaults{
// One of many lines of code that load user default settings
_acmeMotorsIsFavorite = [[NSUserDefaults standardUserDefaults] boolForKey:#"acmeMotorsIsFavorite"];
}
-(void)viewDidLoad {
[self loadUserDefaults];
}
I think you forget about synchronize method.
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setBool:NO forKey:#"acmeMotorsIsFavorite"];
[userDefaults synchronize];
When the user double taps Home the app delegate method applicationWillResignActive: is called, at that time synchronize NSUserDefaults:
- (void)applicationWillResignActive:(UIApplication *)application {
[[NSUserDefaults standardUserDefaults] synchronize];
}

Making a View for the first use of an app

I wish to make an app that, when the user opens it for the first time he selects his country from a picker in order to make his flag appear on the main screen. If the user closes the app and opens it again I want the app to start the menu screen with his flag on it directly.
I am using the following code now but it doesn't work at all. Every time the app is opened it takes him to the picker VIew (TappViewController)
- (void)viewDidLoad
{
[super viewDidLoad];
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"FirstLaunch"]) {
secViewController *menu = [[secViewController alloc] init];
[self presentViewController:menu animated:YES completion:^{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"FirstLaunch"];
[[NSUserDefaults standardUserDefaults] synchronize];}];
}
else{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"FirstLaunch"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
Change your code like this:
- (void)viewDidLoad
{
[super viewDidLoad];
if (![[NSUserDefaults standardUserDefaults] boolForKey:#"isPickerOpened"]) {
secViewController *menu = [[secViewController alloc] init];
[self presentViewController:menu animated:YES completion:^{
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"isPickerOpened"];
[[NSUserDefaults standardUserDefaults] synchronize];
}];
}
}

Settings page to show only once

I have an app which should only show the settings page once: when the app is opened for the first time.
Now this works, and when the user presses the middle button on the iPhone it then reopens the app and carries on from the main screen - that's great. But if I double click on the iPhone button and swipe the application off, it will then go to the settings screen again and not to where it was.
Why is it doing that? How can I make my app only show its settings once?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
NSUserDefaults *settingsscreen = [NSUserDefaults standardUserDefaults];
[settingsscreen registerDefaults:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],#"firstTime", nil]];
BOOL firstTime = [settingsscreen boolForKey:#"firstTime"];
if ( firstTime==YES) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"SettingsShown"];
[[NSUserDefaults standardUserDefaults] synchronize];
self.window.rootViewController = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"SetUpNav"];
}
else
{
return YES;
}
}
Don't use if ( firstTime==YES) { based on #"firstTime", because that flag is never actually saved. You should be using the flag saved with the #"SettingsShown" key.
BOOL firstTime = [settingsscreen boolForKey:#"SettingsShown"];
if (!firstTime) {
...
Instead doing in appDelegate try to acheive it in setting Page itself
-(void) viewDidLoad {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
username = [defaults objectForKey:#"username"];
if (username != NULL ) {
[self selfLogin];
}
}
-(void)selfLogin{
nextPageController = [[NextPageViewController alloc]init];
[self.navigationController pushViewController:nextPageController animated:YES];
}

NSUserDefaults and alertView

I am creating a Disclaimer that will be shown when the user first launches the app. The disclaimer is an alertView with 2 option. If user agrees then firstViewController will display. If he doesn't he will be redirected to another viewController. But I can't get the disclaimer to disappear if the user agrees the first time. It is shown every time the app launches. Any help would be appreciated. Thank you in advance..
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![[defaults valueForKey:#"keyDisclaimer"] isEqualToString:#"accepted"]) {
UIAlertView *disclaimer = [[UIAlertView alloc] initWithTitle:#"Read Before use" message:#"By using this app you agree to its terms and conditions.\n\n\n\n\n\n\n\n\n\n\ntext heren\n\n\n\n\n\n\n\n\n\n\n\n\n" delegate:self cancelButtonTitle:#"No!" otherButtonTitles:#"Yes Let me In", nil];
[disclaimer show];
}
// Override point for customization after application launch.
return YES;
}
-(void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *buttonString = {[alertView buttonTitleAtIndex:buttonIndex]};
if ([buttonString isEqualToString:#"Yes Let me In"]) {
NSMutableDictionary* defaultValues = [NSMutableDictionary dictionary];
[defaultValues setValue:#"accepted"forKey:#"keyDisclaimer"];
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
}
else if ([buttonString isEqualToString:#"No!"]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sorry!" message:#"You are not allowed to use this app due to the fact that you did not agree to the terms and Conditions. Please exit this app!" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alert show];
// [[NSUserDefaults standardUserDefaults] setValue:#"notAccepted" forKey:#"keyDisclaimer"];
}
if ([buttonString isEqualToString:#"OK"]) {
introViewController *intro = [[introViewController alloc] initWithNibName:#"introViewController" bundle:nil];
_window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
_window.rootViewController = intro;
[_window makeKeyAndVisible];
}
}
NSMutableDictionary* defaultValues = [NSMutableDictionary dictionary];
[defaultValues setValue:...forKey:...]
[[NSUserDefaults standardUserDefaults] registerDefaults:defaultValues];
This will register your defaults if they are not set (for the first time)
Also, seems like you're forgetting [defaults synchronize] to commit changes after setting values. If so, you don't need the registerDefaults method at all.
Like this:
if ([buttonString isEqualToString:#"Yes Let me In"]) {
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:#"accepted"forKey:#"keyDisclaimer"];
[defaults synchronize];
}
You want to use something like stringForKey or objectForKey instead of valueForKey.
if([[defaults stringForKey:#"keyDisclaimer"] isEqualToString:#"accepted"]) {
I've had bad experiences with valueForKey in the past not always working the way I want. That might be what's causing you some problems.

Extremely Simple Saved High Score

The game that I am creating has a highScore integer variable that gets assigned when the player loses. I am using NSUsersDefaults class to save my high score. Here is my code that I am using:
-(void)saveScore {
[[NSUserDefaults standardUserDefaults] setInteger:score forKey:#"highScore"];
[defaults setInteger:score forKey:#"highScore"];
[defaults synchronize];
NSLog(#"High Score: %i ", highScore);
}
-(IBAction)buttonReleased:(id)sender {
[stopWatchTimer invalidate];
AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);
NSString *label0 = #"Hold to Start";
[labelText setText:label0];
if (score > 0) {
score--;
}
else {
score = 0;
NSLog(#"Changed score to 0");
}
if (score > highScore) {
[self saveScore];
NSString *scoreMessage =[[NSString alloc] initWithFormat:#"Congrats! You have a new High Score! Click Share High Score to share your score of: %i",score];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"High Score!" message:(NSString *)scoreMessage delegate:nil cancelButtonTitle:#"OK" otherButtonTitles: nil];
[alert show];
[alert release];
score = 0;
}
else {
NSString *scoreMessage =[[NSString alloc] initWithFormat:#"Game Over! Your score was: %i",score];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"GAME OVER!" message:(NSString *)scoreMessage delegate:nil cancelButtonTitle:#"Try Again" otherButtonTitles: nil];
[alert show];
[alert release];
score = 0;
}
- (void)viewDidLoad
{
[super viewDidLoad];
int highscore = [[NSUserDefaults standardUserDefaults] integerForKey: #"highScore"];
[stopWatchTimer invalidate];
stopWatchTimer=nil;
}
I have been wrestling with this for HOURS! What am I doing wrong?! Note: Can you explain it as simply as possible.
Thanks!
-Matt
Reading it:
int highscore = [[NSUserDefaults standardUserDefaults] integerForKey: #"highScore"];
It will most likely be the default value of int (i.e. 0) when the file is blank.
Also don't forget to force a write of your defaults to "disk" with synchronize:
-(void)saveScore {
NSUSerDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setInteger:score forKey:#"highScore"];
[defaults synchronize];
}
You can load the highscore either in viewDidLoad or even in your init (or initWithNibName) method since this part isn't dependent on your view being loaded.
You could declare a property on your Scores view that you set in the viewDidLoad method. Alternatively you could expose the UILabel of that scores class (if that's what you use) as a property of your scores class.
- (void)viewDidLoad:
{
...
self.scoresView.textLabel.text = [NSString stringWithFormat:#"%d", highScore];
...
}
There is a really simple highscore management system which I have written it even has online support. You can find it https://github.com/faizanaziz/HighScore

Resources