UIAlert for review page - iphone

I am fairly new to iPhone coding and wanted to know if someone can help me.
Have got the code ready so that when the app loads a UIAletView loads and prompts the user to review/rate the application but i have a button called "Never Rate"
I needed help to find out how to code the "never rate" button so what when it is pressed the UI Alert does not load everytime the app is loaded.
This is my code so far:
(void)viewDidLoad {
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:#"Rate My Appication" message:#" Please Rate my Application and check out my other Apps"
delegate: self
cancelButtonTitle:#" Cancel "
otherButtonTitles: #" Rate Now ", #"Check Other Apps", #" Never Rate ", nil];
[alert show];
[alert release];
}
(bool)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// Never Review Button
if (buttonIndex == 3)
{
}
// Review Button
else if (buttonIndex == 1)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://itunes.apple.com/gb/app/calculator/id389197581?mt=8"]];
}
// Other Apps Button
else if (buttonIndex == 2)
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://itunes.com/apps/2headsolutions"]];
}
}

You could store a boolean flag somewhere on disk. Then the next time, you check for neverRate == NO and display the alert accordingly.
You could store this using NSUserDefaults:
[[NSUserDefaults standardDefaults] setBool:YES forKey:#"neverRate"];
Then to retrieve it:
BOOL neverRate = [[NSUserDefaults standardDefaults] boolForKey:#"neverRate"];
if(neverRate != YES) {
//Show alert here
}

This is my code so far:
- (void)viewDidLoad {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Rate My Appication"
message:#" Please Rate my Application and check out my other Apps"
delegate:self
cancelButtonTitle:#" Cancel "
otherButtonTitles: #" Rate Now ", #"Check Other Apps", #" Never Rate ", nil];
[alert show];
[alert release];
}
- (bool)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
// Never Review Button if (buttonIndex == 3) {
//}
// Review Button else if (buttonIndex == 1) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://itunes.apple.com/gb/app/calculator/id389197581?mt=8"]]; }
// Other Apps Button else if (buttonIndex == 2) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://itunes.com/apps/2headsolutions"]]; }
}

Rengers is right
Try using NSUserDefaults
Saving
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// saving a BOOL
[prefs setBool:BOOL_var forKey:#"RATE_KEY"];
[prefs synchronize];
Reading
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting a BOOL
BOOL var = [prefs boolForKey:#"RATE_KEY"];
Check This link

Related

IAP consumable triggers twice when buying once

"not solved yet..."
I'm having an issue with my iap consumable packages. The thing is that when I buy one, it duplicates the purchase, making another one without requesting it. The alert View appears double also.
A curious thing: this errors only happens when I make an ipa of my app, and not when debugging it! I can't understand why...
Here is the productPurchase method implemented in my main menu where I put the IAP buttons:
- (void)productPurchased:(NSNotification *)notification {
NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
NSString * productIdentifier = notification.object;
NSString* plistPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"XXXXX.plist"];
if(plistPath != nil){
int playerSilverCoins = [defaults integerForKey:#"userSilverCoins"];
int playerGoldCoins = [defaults integerForKey:#"userGoldenCoins"];
NSArray *coinsItems = [NSArray arrayWithContentsOfFile:plistPath];
for (NSDictionary *coinItem in coinsItems) {
if ([[coinItem objectForKey:#"productId"] isEqualToString:productIdentifier]) {
playerSilverCoins = playerSilverCoins + [[coinItem objectForKey:#"silverCoins"] integerValue];
playerGoldCoins = playerGoldCoins + [[coinItem objectForKey:#"goldenCoins"] integerValue];
[defaults setInteger:playerSilverCoins forKey:#"userSilverCoins"];
[defaults setInteger:playerGoldCoins forKey:#"userGoldenCoins"];
[defaults synchronize];
NSLog(#"product bought: %#" , productIdentifier);
NSString *message = nil;
if([[coinItem objectForKey:#"goldenCoins"] integerValue] == 0)
message = [NSString stringWithFormat:#"You have bought %d silver coins", [[coinItem objectForKey:#"silverCoins"] integerValue]];
else
message = [NSString stringWithFormat:#"You have bought %d golden coins and %d silver coins", [[coinItem objectForKey:#"goldenCoins"] integerValue],[[coinItem objectForKey:#"silverCoins"] integerValue]];
UIAlertView * alert = [[UIAlertView alloc]initWithTitle:#"XXXXX" message:message delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
NSNumber* achievementContributor = [defaults objectForKey:#"AchievementContributor"];
if ([achievementContributor intValue] != 1 ) {
[[GameCenterAchievement sharedGameCenterAchievement] checkAchievements:_kContributor];
[defaults setObject:[NSNumber numberWithInt:1] forKey:#"AchievementContributor"];
[[GKAchievementHandler defaultHandler] notifyAchievementTitle:#"Contributor"
andMessage:#"Buy a coin pack"];
}
break;
}
}
}
}
I don't know if there's a problem there, or on my IAP classes where I implement the transactions, restore, etc. If someone needs more code, just ask for it. Thanks!
Make sure you are adding observer only ones.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(productPurchased:) name:IAPHelperProductPurchasedNotification object:nil];
Adding listener multiply times will cause a multiple calls of your callback method.
If you are reusing your class - remember to removeObserver after you dealloc it - it won't do it on it's own.

Identifying the Push Notification Message

In my project I want to show the events and offers through push notification, but the problem is, I'm able to show the events or offers, not both. Is there any way to identify the message of Push notification. Here's the code:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSString *message = nil;
id alert = [userInfo objectForKey:#"alert"];
if ([alert isKindOfClass:[NSString class]]) {
message = alert;
} else if ([alert isKindOfClass:[NSDictionary class]]) {
message = [alert objectForKey:#"body"];
}
if (alert) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Title"
message:#"AThe message." delegate:self
cancelButtonTitle:#"button 1"
otherButtonTitles:#"button", nil];
[alertView show];
}
NSString *contentsInfo = [userInfo objectForKey:#"contTag"];
NSLog(#"Received contents info : %#", contentsInfo);
NSString *nibName = [AppDelegate fetchNibWithViewControllerName:#"EventsViewController"];
EventsViewController *evc = [[EventsViewController alloc] initWithNibName:nibName bundle:nil];
evc.newEvent = YES;
[self.navigationController pushViewController:evc animated:YES];
[UIApplication sharedApplication].applicationIconBadgeNumber = [[[userInfo objectForKey:#"aps"] objectForKey: #"badgecount"] intValue];
}
alert is always an NSDictionary with two keys: body and show-view. The value of the former is the alert message and the latter is a Boolean (false or true). If false, the alert’s View button is not shown. The default is to show the View button which, if the user taps it, launches the application.
check the docs
To identify type of the message you can provide additional fields, as described here
Example:
{
"aps":{
"badge":1,
"alert":"This is my special message!",
"mycustomvar1":"123456",
"mycustomvar2":"some text",
"myspecialtext":"This is the best!",
"url":"http://www.mywebsite.com"
}
}

Unable to navigate to home page from UIAlertView

I am using a UITableView to show data, and by using customise button and delete function I am trying to delete selected row. But i want to put an alertview inside that function when UITableView is empty, and by using buttons inside the UIAlertView I am trying to navigate to main page and previous page according to conditions. But it's getting crashed after UITableView is getting empty and I push the delete button with "Program received signal: “SIGABRT".
My code looks like this:
- (IBAction)DeleteButtonAction:(id)sender
{
DMSAppDelegate *d = (DMSAppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(#"Message From Custom Cell Received");
if(d->newtableData.count != 0)
{
NSIndexPath *indexPath = [self.tablevieww2 indexPathForCell:(UITableViewCell *)[[[sender superview] superview] superview]];
NSUInteger row = [indexPath row];
[d->newtableDataQt removeObjectAtIndex:row];
NSLog(#"data removed");
[self.tablevieww2 reloadData];
}
else
{
UIAlertView *alertview=[[UIAlertView alloc] initWithTitle:#"hello" message:#"Warning!!: Table is empty" delegate:self cancelButtonTitle:#"Yes" otherButtonTitles:#"",#"No",nil];
textfieldQty1 = [alertview textFieldAtIndex:0];
textfieldQty1.keyboardType = UIKeyboardTypeNumberPad;
textfieldQty1.keyboardAppearance = UIKeyboardAppearanceAlert;
textfieldQty1.autocorrectionType = UITextAutocorrectionTypeNo;
[alertview show];
[alertview release];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0)
{
DMSViewController *bt=[[DMSViewController alloc]initWithNibName:nil bundle:nil];
bt.modalTransitionStyle=UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:bt animated:YES];
}
else if (buttonIndex == 1)
{
NSString *newqty = [[NSString alloc] initWithFormat:#"%#",textfieldQty1.text];
DMSAppDelegate *d= (DMSAppDelegate *)[[UIApplication sharedApplication] delegate];
[d->newtableDataQt replaceObjectAtIndex:slCell1 withObject:(id)newqty];
NSLog(#"tb%#",d->newtableDataQt);
[self.tablevieww2 reloadData];
int total1=0;
for ( int i=0 ; i < [d->newtableDataQt count];++i )
{
NSString *string = [d->newtableDataQt objectAtIndex:i];
NSLog(#"string%#",string);
if ([string isEqualToString:#"0"])
{
}
else
{
NSLog(#"newArray%#",d->newtableDataPrice);
NSString *strP=[d->tableDataPrice objectAtIndex:i];
NSInteger sp=[strP integerValue];
NSInteger st=[string integerValue];
total1=total1+st*sp;
NSLog(#"total1%d",total1);
}
}
NSString *newtotal1=[NSString stringWithFormat:#"%d",total1];
DMSAppDelegate *d2 = (DMSAppDelegate *) [[UIApplication sharedApplication] delegate];
d2->totalD = [[NSString alloc] initWithString:newtotal1];
}
}
Please give me some solution. I am trying really hard from yesterday but not getting any success.
Thanks in advance.
#
You need to check two things:-
First:- if(d->newtableData.count != 0)
is the condition and you are not removing the items from newtableData you are removing it from newtableDataQt so thats why your else method is not getting called. because newtableData will never have count =0.
Second thing;-
one thing if your table is empty means that newtableDataQt will contain no values , it will be empty.Now when you click on the delete button, the alert view appears , after that if you click whatever button at index 1 then in your code you have written :-
[d->newtableDataQt replaceObjectAtIndex:slCell1 withObject:(id)newqty];
so newtableDataQt has already be empty before and now you are using it.This might be the reason of crash.
try
if( [newtableDataQt count] >slCell1)
{ [d->newtableDataQt replaceObjectAtIndex:slCell1 withObject:(id)newqty];
}
I hope it might help you.

XCODE - How to: UIAlertView (with a text field) on a loop until correct value entered into field?

I'm currently working in Xcode on an iOS app...
I've set up a UIAlertView (with a question as the message ) to pop up with a text field to retrieve a response.
The desired functionality is that upon entering an incorrect value into the text field, the UIAlert would loop... Until the correct response is entered. At this point, the UIAlert would be dismissed.
Heres what I have so far...
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
NSString* correctAnswer = #"2";
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Alarm"
message:#"1 + 1 ="
delegate:self
cancelButtonTitle: nil
otherButtonTitles:#"Continue", nil ];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
UITextField* answerField = [alert textFieldAtIndex:0];
answerField.keyboardType = UIKeyboardTypeNumberPad;
answerField.placeholder = #"answer";
[alert show];
// I feel like this would work, but I know it doesn't...
NSString *answerFieldString = answerField.text;
if ([answerFieldString isEqualToString: correctAnswer ])
{
[alert dismissWithClickedButtonIndex:-1 animated:YES];
}
}
I've done extensive google searching and can't come up with a solution... Any responses would be much appreciated!
try this...
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
NSString *answerFieldString = answerField.text;
if ([answerFieldString isEqualToString: correctAnswer ])
{
[alertView dismissWithClickedButtonIndex:-1 animated:YES];
}
}

For push notifications: how do I add action to alert view to change views?

So I have push notifications sending to my application.
The code that triggers the alert is in my app delegate file (I think thats where it is supposed to go?)
How do I make an action for my alert button so that I can change to a different view?
// Set Below code in your App Delegate Class...
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo{
// Call method to handle received notification
[self apnsPayloadHandling:userInfo];
}
-(void) apnsPayloadHandling:(NSDictionary *)userInfo{
// Example Payload Structure for reference
/*
remote notification: {
acme1 = 11114;
aps = {
alert = {
"action-loc-key" = "ACTION_BUTTON_TITLE";
"loc-args" = ("MSG_TEXT");
"loc-key" = "NOTIFICATION_DETAIL_PAGE";
};
badge = 10;
sound = "chime";
};
}
*/
//======================== Start : Fetching parameters from payload ====================
NSString *action_loc_key;
NSArray *loc_args_array;
int badge;
NSString *sound=#"";
NSArray *payloadAllKeys = [NSArray arrayWithArray:[userInfo allKeys]];
// getting "acme1" parameter value...
if([payloadAllKeys containsObject:#"acme1"]){
acme1 = [userInfo valueForKey:#"acme1"]; // getting "ID value" as "acme1"
}
NSString *localizedAlertMessage=#"";
// getting "aps" parameter value...
if([payloadAllKeys containsObject:#"aps"]){
NSDictionary *apsDict = [NSDictionary dictionaryWithDictionary:[userInfo objectForKey:#"aps"]];
NSArray *apsAllKeys = [NSArray arrayWithArray:[apsDict allKeys]];
if([apsAllKeys containsObject:#"alert"]){
if([[apsDict objectForKey:#"alert"] isKindOfClass:[NSDictionary class]]){
NSDictionary *alertDict = [NSDictionary dictionaryWithDictionary:[apsDict objectForKey:#"alert"]];
NSArray *alertAllKeys = [NSArray arrayWithArray:[alertDict allKeys]];
if([alertAllKeys containsObject:#"action-loc-key"]){
action_loc_key = [alertDict valueForKey:#"action-loc-key"]; // getting "action-loc-key"
}
if([alertAllKeys containsObject:#"loc-args"]){
loc_args_array = [NSArray arrayWithArray:[alertDict objectForKey:#"loc-args"]]; // getting "loc-args" array
}
if([alertAllKeys containsObject:#"loc-key"]){
loc_key = [alertDict valueForKey:#"loc-key"]; // getting "loc-key"
}
if([loc_args_array count] == 1){
localizedAlertMessage = [NSString stringWithFormat:NSLocalizedString(loc_key, nil),[loc_args_array objectAtIndex:0]];
}
else if([loc_args_array count] == 2){
localizedAlertMessage = [NSString stringWithFormat:NSLocalizedString(loc_key, nil),[loc_args_array objectAtIndex:0],[loc_args_array objectAtIndex:1]];
}
else if([loc_args_array count] == 3){
localizedAlertMessage = [NSString stringWithFormat:NSLocalizedString(loc_key, nil),[loc_args_array objectAtIndex:0],[loc_args_array objectAtIndex:1],[loc_args_array objectAtIndex:2]];
}
}
else{
localizedAlertMessage = [apsDict objectForKey:#"alert"];
}
}
if([apsAllKeys containsObject:#"badge"]){
badge = [[apsDict valueForKey:#"badge"] intValue]; // getting "badge" integer value
}
if([apsAllKeys containsObject:#"sound"]){
sound = [apsDict valueForKey:#"sound"]; // getting "sound"
}
}
//======================== Start : Handling notification =====================
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateActive){ // application is already open
if(apnsAlert){
apnsAlert = nil;
}
if(action_loc_key){ // View Button title dynamic...
apnsAlert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:#"%# %#",[[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleDisplayName"],NSLocalizedString(#"NOTIFICATION", nil)] message:localizedAlertMessage delegate:self cancelButtonTitle:NSLocalizedString(#"CANCEL", nil) otherButtonTitles: NSLocalizedString(action_loc_key, nil),nil];
}
else{
apnsAlert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:#"%# %#",[[NSBundle mainBundle] objectForInfoDictionaryKey:#"CFBundleDisplayName"],NSLocalizedString(#"NOTIFICATION", nil)] message:localizedAlertMessage delegate:self cancelButtonTitle:NSLocalizedString(#"OK", nil) otherButtonTitles:nil];
}
[apnsAlert show];
}
else{ // application is in background or not running mode
[self apnsViewControllerRedirectingFor_loc_key:loc_key with_acme1:acme1];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
// the user clicked one of the OK/Cancel buttons
if(alertView == apnsAlert){
if (buttonIndex == 1){
[self apnsViewControllerRedirectingFor_loc_key:loc_key with_acme1:acme1];
}
}
}
-(void) apnsViewControllerRedirectingFor_loc_key:(NSString *)loc_key_para with_acme1:(NSString *)acme1_para{
if([loc_key_para isEqualToString:#"NOTIFICATION_DETAIL_PAGE"]){
DetailPageClass *detailPage = [[DetailPageClass alloc] initWithNibName:#"DetailPageClass" bundle:nil];
[self.navigationcontroller pushViewController:detailPage animated:YES]; // use nav controller where you want to push...
}
else if([loc_key_para isEqualToString:#"NOTIFICATION_MAIN_PAGE"]){
}
...
}
To change the title of the button, use the action-loc-key key in the notification dictionary (see this section of the guide).
To do something when the notification is tapped, you can implement a few methods in your app delegate: Handling notifications.