Saving a text from UIAlertView - save

I have written a UIAlertView code with a textField and I want to write a code to store whatever is typed by the user as a pushable detail list. How can I do that?
- (IBAction)NewReference:(id)sender {
UIAlertView *newreference = [[UIAlertView alloc] initWithTitle:#"New Reference" message:#"Enter Name" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles: #"Done",nil];
newreference.alertViewStyle = UIAlertViewStylePlainTextInput;
[newreference textFieldAtIndex:0].delegate = self;
[newreference show];
}

Here you go good sir. Per your comment above, if you want to store data for NSUserDefaults here is how you would do it:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString* saveData = [[newreference textFieldAtIndex:0] text];
[defaults setObject:saveData forKey:#"whatever key you want here"];
[defaults synchronize];
Then to invoke or call it:
NSString* recallData = [defaults objectForKey:#"whatever key you named above"];
Happy coding -

Related

Youtube videos failed to upload after upgrading to xcode7

- (void)uploadTicket:(GDataServiceTicket *)ticket
finishedWithEntry:(GDataEntryYouTubeVideo *)videoEntry
error:(NSError *)error
{
NSLog(#"ticket");
UIButton *uploadButton = (UIButton *)[backgroundImage viewWithTag:10];
UIButton *cancleButton = (UIButton *)[backgroundImage viewWithTag:20];
if (error == nil)
{
// tell the user that the add worked
NSLog(#"Video Successfully Uploaded to Youtube");
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSNumber *uploadedToYouTube = [defaults objectForKey:#"uploadedToYouTube"];
if(nil == uploadedToYouTube)
{
[defaults setObject:[NSNumber numberWithBool:YES] forKey:#"uploadedToYouTube"];
[defaults synchronize];
}
NSNumber *userOpenedYoutubeView = [defaults objectForKey:#"userOpenedYoutubeToUnlockTheme"];
if(nil != userOpenedYoutubeView)
{
// [defaults setBool:NO forKey:#"Unlock_Theme"];
[defaults setObject:[NSNumber numberWithBool:NO] forKey:#"Unlock_Theme"];
[defaults synchronize];
UIAlertView *alrtView = [[UIAlertView alloc] initWithTitle:#"Congrats...!" message:#"Your new theme is Unlocked" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alrtView show];
[alrtView release];
[self removeFromSuperview];
}
else
{
UIAlertView *alrtView = [[UIAlertView alloc] initWithTitle:#"Success...!" message:#"Your Video is successfully uploaded to Youtube" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alrtView show];
[alrtView release];
[self removeFromSuperview];
}
}
else {
NSLog(#"Fails to upload Video to Youtube");
UIAlertView *alrtView = [[UIAlertView alloc] initWithTitle:#"Sorry" message:#"Fails to upload video to Youtube. Please try again" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alrtView show];
[alrtView release];
}
mProgressView . hidden = YES;
uploadButton . hidden = NO;
cancleButton . enabled = YES;
[mProgressView setProgress: 0.0];
[self setUploadTicket:nil];
}
Every time i try to upload it is showing alert message "failed to upload". I don't get why it is showing like that. Before upgrading to Xcode 7 it works fine. Anyone know please help me.

Show uialertview after opening an app after 3 times?

Is it possible to show a Alertview when opening an app after lets say 3 times? Can this be done with NSUserDefaults?
Thanks!
int launches = [[NSUserDefaults standardUserDefaults] integerForKey:#"launchCount"];
if (launches > 3) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"My Alert"
message:#"Some message" delegate:nil
cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
[[NSUserDefaults standardUserDefaults] setInteger:launches+1 forKey:#"launchCount"];
if([[NSUserDefaults standardUserDefaults]integerForKey:#"launchCount"]==0){
[[NSUserDefaults standardUserDefaults]setInteger:1 forKey:#"launchCount"];
}else{
[[NSUserDefaults standardUserDefaults]setInteger:[[NSUserDefaults standardUserDefaults]integerForKey:#"launchCount"]+1 forKey:#"launchCount"];
}

Show UIAlertview after 3 launches App

What I'm trying to achieve is to show a UIAlertview when a user opens the app after 3 times. I use the code below in my ViewDidAppear's ViewController, but it shows the UIAlertview everytime when opening the app. Can someone tell me what I'm doing wrong here?
int launches = [[NSUserDefaults standardUserDefaults] integerForKey:#"launchCount"];
if (launches > 3) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"My Alert"
message:#"Some message" delegate:nil
cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
[[NSUserDefaults standardUserDefaults] setInteger:launches+1 forKey:#"launchCount"];
Edit: I'm also getting a NSInteger (aka 'long') to 'int' warning. Could this be the issue why it's not working?
You should move that code to your app delegate in
-application:didFinishLaunchingWithOptions:
#AdamPro13 is right that you should move the code but probably to - (void)applicationDidBecomeActive:(UIApplication *)application in your app delegate. The viewDidAppear method can be called several times for each app launch. If you want to show the UIAlertView only once per installation you could either save a BOOL if it has been shown or change the test launches == 4. Something like
NSInteger launches = [[NSUserDefaults standardUserDefaults] integerForKey:#"launchCount"];
BOOL launchAlertShown = [[NSUserDefaults standardUserDefaults] boolForKey:#"launchAlertShown"];
if (launches > 3 && !launchAlertShown) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"My Alert"
message:#"Some message" delegate:nil
cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"launchAlertShown"];
}
[[NSUserDefaults standardUserDefaults] setInteger:launches+1 forKey:#"launchCount"];
You have delete the app, not just overwrite with a new build, to delete the contents of [NSUserDefaults standardUserDefaults]. This means that the test launches > 3 will remain true until you have deleted the app from the device or simulator that you are testing on.,
Got it! Thanks to #AdamPro13
NSInteger launches = [[NSUserDefaults standardUserDefaults] integerForKey:#"launchCount"];
if (launches % 3 == 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"My Alert"
message:#"Some message" delegate:nil
cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
}
[[NSUserDefaults standardUserDefaults] setInteger:launches+1 forKey:#"launchCount"];

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