Show uialertview after opening an app after 3 times? - ios

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"];
}

Related

iOS ARC UIAlertView leaking memory

I have simple method showing AlertView with textfield. Instruments showing memory leak in this. Please explain.
- (void)method {
NSString *value = [[NSUserDefaults standardUserDefaults] valueForKey:#"key"];
if (value == nil) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Title" message:#"message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
alertView.tag = 101;
alertView.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField *txtGroup = [alertView textFieldAtIndex:0];
[txtGroup becomeFirstResponder];
[alertView show];
alertView = nil;
}
}
Please find the screenshot of Instruments:
You need to create alertView as :
static UIAlertView *alertView = nil;
if (!alertView){
alertView = [[UIAlertView alloc] initWithTitle:#"Title" message:#"message" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
}

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.

Saving app data after iphone/ipad reset

The photo in the link below illustrates the app I am currently working on, the name is a UITextfield that the user edits him/herself and the balance is label in which the user clicks the add button to add their desired funds. Then the two textfields below that allow for the input of money spent and how it was spent, this is then saved as a UITableviewCell in the UITable view. The problem I am currently having right now is when I restart my iPhone device the previously inputted data is rested meaning the user will have to rewrite everything which wouldn't help as this is an app to monitor one's savings. I did some googling to see if there was a fix for this I came across NSUserDefaults and tried the following code in my viewDidLoad.
Could someone provide the steps for a different approach to keep the inputted information where it is even after the user turns off his or her iPhone/iPad etc. Heres the photo link
http://imgur.com/DceJ7gs
[[NSUserDefaults standardUserDefaults] synchronize];
This is the code I am using to add funds which in turn changes the balance displayed by the uilabel Balance in the above photo
- (IBAction)addFunds:(id)sender {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Add Funds" message:#"Please add your funds" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:#"Done", nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[_inputStatement resignFirstResponder];
[_spent resignFirstResponder];
[myTableView resignFirstResponder];
[_custName2 resignFirstResponder];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
//alertview input text was blank so we are doing a check
if([[alertView textFieldAtIndex:0].text isEqual:#""] && buttonIndex == 1){
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Input Required" message:#"Please fill in the textfield" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
} else {
if(buttonIndex == 1){
NSString *tempText = [alertView textFieldAtIndex:0].text;
float toAdd = [tempText floatValue];
float add = [_currentBalance.text floatValue];
if(toAdd < 0) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"You can't add a negative number, try again" delegate:nil cancelButtonTitle:#"Ok" otherButtonTitles:nil];
[alert show];
} else {
add = add + toAdd;
if(add >= 99999999.50){
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Well you broke it" message:#"You are not allowed to input this much. In the event you do have these funds, I apologize for putting in this restriction. Personally contact me at thejobemuhammed#gmail.com for further inquiries." delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
} else {
_currentBalance.text = [NSString stringWithFormat:#"%.2f", add];
if(!array){
array = [[NSMutableArray alloc]init];
}
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
date1 = [NSDate dateWithTimeIntervalSinceNow:5];
NSString *resultString = [dateFormatter stringFromDate:date1];
[array insertObject:[NSString stringWithFormat:#"%#%#%#",#"$",tempText, #" deposited"] atIndex:0];
[date insertObject:resultString atIndex:0];
[details insertObject:#"This is a deposit" atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.myTableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
}
[_inputStatement resignFirstResponder];
[_spent resignFirstResponder];
[myTableView resignFirstResponder];
}
add this code where you want to save . i am not getting where are you saving .
save it using this code
NSString *valueToSave = #"hereis yourvalue";
[[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:#"preferencekyename"];
[[NSUserDefaults standardUserDefaults] synchronize];
to get it back later using this code
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:#"preferencekyename"];

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"];

action on textFieldDidEndEditing

I want an alert when the following text fields receive the conditions in second section of code
self.circuit.rcdAtIan = [ICUtils nonNilString:self.rcdAtIan.text];
self.circuit.rcdAt5an = [ICUtils nonNilString:self.rcdAt5an.text];
The above code works fine so now I need to fire it. Using the below method was my first thought but that fires the alert on every keyboard resignation. I only want the alert to display once.
- (void)textFieldDidEndEditing:(UITextField *)textField {
if ([_rcdAtIan.text intValue]> 200) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"my message" delegate:nil cancelButtonTitle: #"Ok" otherButtonTitles: nil];
[alert show];
}
if ([_rcdAt5an.text intValue]> 40) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"my message" delegate:nil cancelButtonTitle: #"Ok" otherButtonTitles: nil];
[alert show];
}
}
Im thinking maybe I need a bool with NSUserDefaults perhaps? but not sure how to implement that to check if the alert has been shown. Normally if I wanted an alert shown once I would do
if (![#"1" isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:#"alert"]]){
[[NSUserDefaults standardUserDefaults] setValue:#"1" forKey:#"alert"];
[[NSUserDefaults standardUserDefaults] synchronize];
[alert show];
}
However in this instance, when the page is reloaded I want the alerts to be shown again and in any case im not sure if thats an efficient way to solve this
Don't use NSUserDefaults, that would be inappropriate
in your #interface...
#property (assign) BOOL freshAlert1;
#property (assign) BOOL freshAlert2;
then something like this... (assuming 'page is reloaded' equates to your viewController coming back onscreen)
- (void)viewDidAppear
{
self.freshAlert1 = YES;
self.freshAlert2 = YES;
}
if (([_rcdAtIan.text intValue]> 200) && self.freshAlert1) {
self.freshAlert1 = NO;
...
}
if (([_rcdAt5an.text intValue]> 40) && self.freshAlert2) {
self.freshAlert2 = NO;
...
}
What exactly do you mean by "once"? Once per app run, once per editing step?
Or maybe simply add two BOOL flags that you can reset whenever you want and simply to
- (void)textFieldDidEndEditing:(UITextField *)textField {
if ([_rcdAtIan.text intValue]> 200 && !alert1shown) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"my message" delegate:nil cancelButtonTitle: #"Ok" otherButtonTitles: nil];
alert1shown = YES;
[alert show];
}
if ([_rcdAt5an.text intValue]> 40 && !alert2shown) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"my message" delegate:nil cancelButtonTitle: #"Ok" otherButtonTitles: nil];
alert2shown = YES;
[alert show];
}
}

Resources