Thursday 25 August 2011

Adding To Favorites Functionality

In most of the apps of iphone there is an option of favorites .You can add items to favorite list,remove items and view items marked as your favorites ones.

This wiki includes the complete implementation of favorites functionality

In your AppDelegate.h file create a favorite array.

NSMutableArray *favoritesArray;
make the property and synthesize it.

In your AppDelegate.m file

- (void)applicationDidFinishLaunching:(UIApplication *)application {

//Create an instance ofNSUserDefaults

NSUserDefaults *stdDefaults=[NSUserDefaults standardUserDefaults];
if([stdDefaults valueForKey:@"favorites"] == nil)
[self setFavoritesArray:[[NSMutableArray alloc]init]];
else
[self setFavoritesArray:[NSMutableArray arrayWithArray:[stdDefaults valueForKey:@"favorites"]]];

}

- (void)applicationWillResignActive:(UIApplication *)application{

if(updated == NO){
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:favoritesArray forKey:@"favorites"];
[NSUserDefaults resetStandardUserDefaults];
updated = YES;
}
}

- (void)applicationWillTerminate:(UIApplication *)application {
// Save data if appropriate

if(updated == NO){
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:favoritesArray forKey:@"favorites"];
[NSUserDefaults resetStandardUserDefaults];
updated = YES;
}
}


Before adding the item in the favorites we check if it already exists or not.If it does not exist it gets added else the popup appears.




-(IBAction)addToFavorites
{
once = NO;

for(int i=0;i<[[appDelegate favoritesArray] count];i++)
{
NSDictionary *tempFavDict = [[appDelegate favoritesArray] objectAtIndex:i];
NSArray *key=[tempFavDict allKeys];
NSDictionary *dkeydict=[tempFavDict objectForKey:[key objectAtIndex:0]];
NSString *businessName = [dkeydict valueForKey:@"name"];

if([name isEqualToString:businessName]){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Already added" message:nil
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil ,nil];
[alert show];
alert.tag=1;
[alert release];
once=YES;
break;
}
}
if(once==NO)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message: @"Do you want to add this business to your Favorites ?"
delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alert show];
alert.tag=2;
[alert release];

}
}







Now on the button click or wherever you want to add items in your favorite list add code:-

In the following code I have created a favoriteDict dictionary which contains all the values and adding that dictionary into favorites array.


- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(alertView.tag==2)
{
if(buttonIndex==0)
{
return;
}
if(buttonIndex==1)
{
NSMutableDictionary *favoriteDict=[[NSMutableDictionary alloc] init];
NSDate *dat=[NSDate date];
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString *favDate=[formatter stringFromDate:dat];
[formatter release];
[favoriteDict setObject:favDate forKey:@"date"];
[favoriteDict setObject:self.name forKey:@"name"];
[favoriteDict setObject:self.address forKey:@"address"];
[favoriteDict setObject:self.description forKey:@"description"];
[favoriteDict setObject:self.contact forKey:@"contact"];
[favoriteDict setObject:self.phone forKey:@"phone"];
[favoriteDict setObject:self.email forKey:@"email"];
[favoriteDict setObject:self.website forKey:@"website"];

//[favoriteDict setObject:self.code forKey:@"businesscode"];
[busniessCodeFavorite setObject:favoriteDict forKey:self.code];
[appDelegate.favoritesArray addObject:busniessCodeFavorite];

//Resetting favorites in NSUserDefaults

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:appDelegate.favoritesArray forKey:@"favorites"];
[NSUserDefaults resetStandardUserDefaults];
[favoriteDict release];

//NSLog(@"appdelegate favories array%@",appDelegate.favoritesArray);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message: @"Added To Favorites"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
}
}




The selected item is added in favorites Array now create a link where your favorite item list will be displayed




No comments:

Post a Comment