I've got a simple UIAlertView showing when user runs the app. It has this structure:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Welcome!", "")
message:NSLocalizedString(#"This is a welcome message.", "")
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
The question is, How can I customize it to show every 5 runs for example?
Thanks in advance ;)
You can use NSUserDefaults to store app executions count for key AppRunCount (you can introduce your own key name):
int runCount = [[NSUserDefaults standardUserDefaults] integerForKey:#"AppRunCount"] + 1
[[NSUserDefaults standardUserDefaults] setInteger:runCount forKey:#"AppRunCount"];
if (runCount <= 5) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(#"Welcome!", "")
message:NSLocalizedString(#"This is a welcome message.", "")
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
[alert release];
}
You can just add code above to viewDidLoad
There will be a method in AppDelegate class like
- (void)applicationDidBecomeActive:(UIApplication *)application
Now create one NSUSerdefaults in this method and make one integer and increment that integer and save it in NSUserdefaults
Now every time application starts that method will be called and integer will incremented
Now make if condition in that method like below
if(your integer which has nsuserdefaults >=5)
{
your alertview
again here make your nsinteger to Zero which is stored in nsuserdefaults
your integer which has nsuserdefaults =0
}
This is the answer of your second question,Every after 5 times app run. alert will be pop up
Let me know it is working or not..!!!!
Happy Coding!!!!
Related
i have an error which i donno how to fix it it's making me crazy. i have searched about the solution a lot but didn't find anything which works here .
here is the code and you can see the error in the picture .
thanks
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:[NSString stringWithUTF8String:title]
message:[NSString stringWithUTF8String:body]
delegate:nil
cancleButtonTitle:[NSString stringWithUTF8String:cancleLable]
otherButtonTitles:[NSString stringWithUTF8String:firstLable],
[NSString stringWithUTF8String:secondLable], nil];
[alert show];
I have a simple question with no answer.
I have a UIAlertView local variable like this,
- (void)insertNewObject:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"title"
message:#"message"
delegate:nil
cancelButtonTitle:#"cancelButtonTitle"
otherButtonTitles:#"otherButtonTitle", nil];
[alert show];
}
When I execute the - (void)insertNewObject:(id)sender method, every time the reference count of the UIAlertView is increased one by one and they are not discarded.
So, any issue is on this with ARC. I need to keep the living objects count without increase while I execute the - (void)insertNewObject:(id)sender method.
I am new to programming. I am having trouble finding out what all is wrong with this. It is an alert view that i am trying to randomize the text displayed in the message.
-(void)alert:(id)sender{
int randomNumber;
randomNumber = (randomNumber() %3 + 1);
NSLog(#"%i", randomNumber);
if (randomNumber == 1) {
self.YouWin.text = [NSString stringWithFormat:#"You Win"];
}
else if (randomNumber == 2) {
self.YouWin.text = [NSString stringWithFormat:#"You Lose"];
}
else if (randomNumber == 3) {
self.YouWin.text = [NSString stringWithFormat:#"Tie"];
}
NSLog(#"%#",YouWin);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Hello" message:[NSString stringWithFormat:#"%#",YouWin] delegate:self cancelButtonTitle:#"Ok" otherButtonTitles: nil];
[alert show];
button.hidden = YES;
Try this one:
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Hello"
message:self.YouWin.text
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
You needed text-value stored in YouWin, but you passed the YouWin object itself.
*Note: you can use arc4random() for generating random numbers.
There are many good suggestions; I agree with AKB8085.
Replacing the randomNumber() with arc4random() will help at compile time.
But you might want to re-think implementing a random number generator. The reason is in fairness to your user. I pose the question, “Is it fair to assume you want your user to guess a number with this large of a number range?”
Did you Know?
Using the arc4random(3), rand(3), or random(3) you are using a C function.
You are asking the user to guess a number with the following ranges:
arc4random(3) = 0 to 4294967296
rand(3) and random(3) that top out at a RAND_MAX of 0x7fffffff (214748647)
To help in answering your question, answer the following requirement questions:
Is there a min/max range restraint?
What type of compelling delays will happen
by using arc4random(3), rand(3), or random(3)?
Is using NSArray like in the Fisher–Yates_shuffle a better answer?
SUGGESTION:
Read an article on random numbers and NSArray.
NOTE:
Random numbers tend to task the compiler and your user experience will be hindered.
As Anoop noted you are using stringWithFormat but you're not providing a format string at all.
You should do
[NSString stringWithFormat:#"%#", #"You Win"];
but that's extremely redundant, although correct, and it's totally equivalent to just using #"You Win".
Also an advice for the general approach on the problem. Instead of having a big if-else statement, it's better to store all your string into a data structure and then randomly access to it.
In code this would translate to
NSArray * outcomes = #[ #"You Win", #"You lose", #"Tie" ];
int randomIndex = arc4random_uniform(outcomes.count);
NSString * randomOutcome = outcomes[randomIndex];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Hello"
message:randomOutcome
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
button.hidden = YES;
Note the usage of arc4random_uniform() that gives you back a random number between 0 and the argument provided, excluded.
Replace
randomNumber = (randomNumber() %3 + 1);
with
randomNumber = arc4random() %3 + 1;
Also use this...
if (randomNumber == 1) {
self.YouWin.text = [NSString stringWithFormat:#"You Win"];
}
else if (randomNumber == 2) {
self.YouWin.text = [NSString stringWithFormat:#"You Lose"];
}
else if (randomNumber == 3) {
self.YouWin.text = [NSString stringWithFormat:#"Tie"];
}
NSLog(#"%#",YouWin);
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:#"Hello"
message:self.YouWin.text
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
button.hidden = YES;
I want to do an application that saving an image from the current main UIImageView when pressing a button & save it to photo album.
What my program did was it shows an error after i press save to photo album button. Probably my code structure is not right. Or can someone show me the right code structure?
Here is my progress so far.
-(IBAction) saveToPhotoAlbum{
NSString *saveMyPhoto=[NSHomeDirectory() stringByAppendingPathComponent:#"image.png"];
UIImage *saved=[UIImage imageWithContentsOfFile:saveMyPhoto];
NSData *imageData = [NSData dataWithContentsOfFile:(NSString *)UIImagePNGRepresentation(saved)];
[imageData writeToFile:(NSString *)saved atomically:YES ];
UIImageWriteToSavedPhotosAlbum(saved, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
UIAlertView *alert;
if (error)
alert = [[UIAlertView alloc] initWithTitle:#"Error"
message:#"Unable to save image to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
else
alert = [[UIAlertView alloc] initWithTitle:#"Success"
message:#"Image saved to Photo Album."
delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
[alert release];
}
You should give a correct path to save the image. One that is also read and write, check out for Documents directory, NSString * docPath=[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"],then add you file name using the same method -stringByAppendingPathComponent: or just create a formatted string with both contents
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"the response string is %#",responseString);
if ([responseString isEqualToString:#"No Data Available"] )
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Message"
message:#"No data Avilable in server"delegate:self cancelButtonTitle:nil otherButtonTitles:
#"OK", nil];
[alert show];
[alert release];
}
in my connectiondidfinishloadin delegate method i have the above code. when the server replies there is no data , it does not go into the if loop for some reason. not sure why. any hint will be greatly appreciated.
Thank You.
ResponseString is probably null/nil and runtime knows that that if statement will b false because of this, so skips it. You should see this with your NSLog(#"the response string is %#",responseString);
On a side note, Charles is a good debugging tool for this- you can see the http info as it goes out and comes in.