Localization in iPhone not working on button click - iphone

I made an app for iPhone. In this user can change language on button click. but NSLoalizedString is not converting the value.
the code is
-(IBAction)btn1pressed:(id)sender {
SecViewController *sec = [[SecViewController alloc] initWithNibName:#"SecViewController" bundle:nil];
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
// NSLog(#"%#", [userDefaults objectForKey:#"AppleLanguages"]);
languages =#"en";
[[NSUserDefaults standardUserDefaults] setObject:languages forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"%#", [userDefaults objectForKey:#"AppleLanguages"]);
// NSLog(#"%#", NSLocalizedString(#"Subhash", nil));
[self.navigationController pushViewController:sec animated:YES];
}
-(IBAction)btn2pressed:(id)sender {
SecViewController *sec = [[SecViewController alloc] initWithNibName:#"SecViewController" bundle:nil];
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
// NSLog(#"%#", [userDefaults objectForKey:#"AppleLanguages"]);
languages = #"es";
[[NSUserDefaults standardUserDefaults] setObject:languages forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"%#", [userDefaults objectForKey:#"AppleLanguages"]);
// NSLog(#"%#", NSLocalizedString(#"Subhash", nil));
[self.navigationController pushViewController:sec animated:YES];
}
-(IBAction)btn3pressed:(id)sender {
SecViewController *sec = [[SecViewController alloc] initWithNibName:#"SecViewController" bundle:nil];
NSUserDefaults* userDefaults = [NSUserDefaults standardUserDefaults];
// NSLog(#"%#", [userDefaults objectForKey:#"AppleLanguages"]);
languages =#"ja";
[[NSUserDefaults standardUserDefaults] setObject:languages forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"%#", [userDefaults objectForKey:#"AppleLanguages"]);
// NSLog(#"%#", NSLocalizedString(#"Subhash", nil));
[self.navigationController pushViewController:sec animated:YES];
}

Try
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:languages,nil] forKey:#"AppleLanguages"];

Use NSLocalizedString(), for localization.
Also give corresponding localization string.

Related

UITextView text not updating

I have UIWebview for display article. I need to select some text and send to another viewcontroller UITextView. But when i install the app i select text and it loads to viewcontroller1. But then i select another some text when i go to viewcontroller1, the previous text only showing. It's not updating new text.
code:
viewcontroller:
NSString *selection = [wbCont stringByEvaluatingJavaScriptFromString:#"window.getSelection().toString()"];
NSUserDefaults *userData1 = [NSUserDefaults standardUserDefaults];
[userData1 setObject:selection forKey:#"preferenceName"];
[userData1 synchronize];
[self.navigationController pushViewController:viewcontroller1 animated:YES];
viewcontroller1:
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:#"preferenceName"];
textView.text = [NSString stringWithFormat:#"%#", savedValue];
[self.view addSubview:textView];
in your viewcontroller1 try to do it like this
NSUserDefaults *userData1 = [NSUserDefaults standardUserDefaults];
NSString *savedValue = [userData1 valueForKey:#"preferenceName"];
textView.text = [NSString stringWithFormat:#"%#", savedValue];
[self.view addSubview:textView];
Try this :
NSString *selection = [wbCont stringByEvaluatingJavaScriptFromString:#"window.getSelection().toString()"];
NSUserDefaults *userData1 = [NSUserDefaults standardUserDefaults];
[userData1 setValue:selection forKey:#"preferenceName"];
[NSUserDefaults standardUserDefaults] synchronize];
[self.navigationController pushViewController:viewcontroller1 animated:YES];
//View controller
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
valueForKey:#"preferenceName"];
UITextView *textView = [UITextView alloc] initWithFrame : cgRectMake(0,0,200,50)]//if not created textview
[self.view addSubview:textView];
textView.text = [NSString stringWithFormat:#"%#", savedValue];

NSUserDefaults not getting stored string value

I have UIWebView for reading content. After that i select some text from content stores in string using NSUserDefaults in Viewcontroller. Then i retrieve this string value and displays in UITextView to viewcontroller1. But the UITextView is not updating text from viewcontroller. The NSUserDefaults also not getting updating text. Im using [self.navigationController pushViewController:viewcontroller1 animated:YES]; for switch to viewcontroller1.
viewcontroller:
[wbCont loadHTMLString:webString baseURL:nil];
[self.view addSubview:wbCont];
NSMutableArray *items = [[[UIMenuController sharedMenuController] menuItems] mutableCopy];
if (!items) items = [[NSMutableArray alloc] init];
UIMenuItem *menuItem;
menuItem = [[UIMenuItem alloc] initWithTitle:#"BookMark" action:#selector(save:)];
[items addObject:menuItem];
[menuItem release];
menuItem = [[UIMenuItem alloc] initWithTitle:#"Note" action:#selector(note:)];
[items addObject:menuItem];
[menuItem release];
[[UIMenuController sharedMenuController] setMenuItems:items];
[items release];
- (BOOL) canPerformAction:(SEL)action withSender:(id)sender
{
if (action == #selector(save:))
{
return YES;
}
else if (action == #selector(note:))
{
return YES;
}
else if (action == #selector(copy:))
{
return NO;
}
return [super canPerformAction:action withSender:sender];
}
-(void)save:(id)sender{
NSString *selection = [wbCont stringByEvaluatingJavaScriptFromString:#"window.getSelection().toString()"];
NSLog(#"selection is %#",selection);
NSUserDefaults *userData1 = [NSUserDefaults standardUserDefaults];
[userData1 setObject:selection forKey:#"preferenceName"];
[userData1 synchronize];
[self.navigationController pushViewController:note animated:YES];
}
viewcontroller1:
textView = [[UITextView alloc]initWithFrame:CGRectMake(0,0,320,400)];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *savedValue = [prefs stringForKey:#"preferenceName"];
NSLog(#"saved is %#", savedValue);
textView.text = [NSString stringWithFormat:#"%#", savedValue];
Allocation of textView is in viewDidLoad.
Updating of textView is in viewWillAppear.
-(void) viewWillAppear:(BOOL)animated
[super viewWillAppear:animated];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *savedValue = [prefs stringForKey:#"preferenceName"];
NSLog(#"saved is %#", savedValue);
textView.text = [NSString stringWithFormat:#"%#", savedValue];
}
I was checked your code just change NSSTring to NSString like below it will working.
-(void)save:(id)sender{
// NSSTring *selection = [wbCont stringByEvaluatingJavaScriptFromString:#"window.getSelection().toString()"];
NSString *selection = [wbCont stringByEvaluatingJavaScriptFromString:#"window.getSelection().toString()"];
NSLog(#"selection is %#",selection);
NSUserDefaults *userData1 = [NSUserDefaults standardUserDefaults];
[userData1 setObject:selection forKey:#"preferenceName"];
[userData1 synchronize];
[self.navigationController pushViewController:note animated:YES];
}

Background location service tracking not stopping when i "OFF" the UISwitch

Am using UISwitch to turn "ON" and "OFF" the the location services. When i Switch "ON" the UISwitch the location services start to to track teh location. I can come out the app and location services are running good. My problem is when i switch "OFF" the tracking and move to any View Controller the location tracking still in "ON" state and its not stopping the tracking. below is my code.
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation* location = [locations lastObject];
latDeg = location.coordinate.latitude;
longDeg = location.coordinate.longitude;
}
-(IBAction)startTracking:(id)sender{
if(startTrackingButton.on){
[locationManager startUpdatingLocation];
[[NSUserDefaults standardUserDefaults] setBool:startTrackingButton.on forKey:#"switchValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else{
[[NSUserDefaults standardUserDefaults] setBool:startTrackingButton.on forKey:#"switchValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
[locationManager stopUpdatingLocation];
}
}
- (void)viewDidLoad{
[super viewDidLoad];
[startTrackingButton setOn:[[NSUserDefaults standardUserDefaults] boolForKey:#"switchValue"]];
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.navigationItem setHidesBackButton:YES];
}
Any suggestions?
I'm going to assume that startTrackingButton is actually a UISwitch. If that is the case, you should be checking the isOn property.
if (startTrackingButton.isOn) {
[locationManager startUpdatingLocation];
[[NSUserDefaults standardUserDefaults] setBool:startTrackingButton.on forKey:#"switchValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
} else {
[[NSUserDefaults standardUserDefaults] setBool:startTrackingButton.on forKey:#"switchValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
[locationManager stopUpdatingLocation];
}

Fail to addObject to NSMutableArray

I am building this 'Add to favorite' with NSUserDefault. I am having this issue when adding array to NSMutableArray. Does anyone knows what I did wrong? Thank you very much.
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSMutableArray *favoriteRecipes = [[NSMutableArray alloc] init];
if ([prefs objectForKey:#"myFavor"] == nil) {
//create the array
NSMutableArray *array = [[NSMutableArray alloc] init];
[prefs setObject:array forKey:#"myFavor"];
[array release];
}
NSMutableArray *tempArray = [[prefs objectForKey:#"myFavor"] mutableCopy];
favoriteRecipes = tempArray;
[tempArray release];
NSArray *charArray = [[NSArray alloc] initWithObjects: #"test1", #"test2" , nil];
//add the recipe
[favoriteRecipes addObject:[charArray objectAtIndex:0]];
//save the array to NSUserDefaults
[prefs setObject:favoriteRecipes forKey:#"myFavor"];
[prefs synchronize];
favoriteRecipes = tempArray;
instated of above line use be below line it will work fine
[favoriteRecipes addObjectsFromArray:tempArray];
done something like this yesterday. see my code below. im not sure if you want the same thing like I did.
NSDictionary *tempDictionary = [NSDictionary dictionaryWithObjectsAndKeys:productID,#"ID",productTitle,#"title",productPrice,#"price",imageUrl,#"image",shipping,#"shipping_day", nil];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *cartList = [[defaults objectForKey:#"CART_ITEMS"] mutableCopy];
if(!cartList)
cartList = [[NSMutableArray alloc] init];
if([cartList indexOfObject:tempDictionary] == NSNotFound)
{
NSLog(#"add favorite");
[cartList addObject:tempDictionary];
[defaults setObject:cartList forKey:#"CART_ITEMS"];
[defaults synchronize];
[sender setTitle:#"Remove" forState:UIControlStateNormal];
}
else
{
NSLog(#"remove favorite");
[cartList removeObject:tempDictionary];
[defaults setObject:cartList forKey:#"CART_ITEMS"];
[defaults synchronize];
[sender setTitle:#"Add to cart" forState:UIControlStateNormal];
}
this is what I do everytime I want my favorite list from NSUserDefault
-(void)getCartList
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *cartList = [[defaults objectForKey:#"CART_ITEMS"] mutableCopy];
productListArr = [NSMutableArray arrayWithArray:cartList];
NSLog(#"cart list data == %#", cartList);
}

How to edit stored value in NSUserDefaults

I am storing some string value in NSUserDefaults,
In the next button click I need to edit stored value and save new string in the place of old string.
How can I do this.
Can any one please help me.
Thank you in advance.
[[NSUserDefaults standardUserDefaults] setObject: newStringValue forKey: kMyKey];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"Password"];
[prefs setObject:#"" forKey:#"Password"];
NSString *passwordStr = [prefs stringForKey:#"Password"];
[[NSUserDefaults standardUserDefaults] synchronize];