I'm trying to look for an array of all languages written specifically in English.
I've read several questions about this subject, and I noticed this line of code:
NSArray *languages = [[NSUserDefaults standardUserDefaults] objectForKey:#"AppleLanguages"];
"languages" array-object holds all langauges string, but the problem is that it's written in the language that's defined in my iPhone. I want it to be in English for all devices.
Is there a way to accomplish that?
Force the default language to be "en" then query the languages list and set it back to the previous one. Something like this (not tested thought)
NSArray *languages = [[NSUserDefaults standardUserDefaults] objectForKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:#"en", nil] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
//your query here
//restore the previous
[[NSUserDefaults standardUserDefaults] setObject:languages forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSArray *localIdentifiers = [NSLocale availableLocaleIdentifiers];
NSMutableArray *lanuages = [[NSMutableArray alloc]init];
for (int i = 0; i < [localIdentifiers count]; i++) {
[lanuages addObject:[[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:[localIdentifiers objectAtIndex:i]]];
}
NSLog(#"%#", lanuages);
Try this or You can use another method
NSArray* languages = [[NSUserDefaults standardUserDefaults] objectForKey:#"AppleLanguages"];
for (int i = 0; i < [languages count]; i++) {
NSLog(#"%#", [[NSLocale currentLocale] displayNameForKey:NSLocaleIdentifier value:[languages objectAtIndex:i]]);
}
Related
I have an NSMutableArray in which I am adding objective and storing in user defaults, but while adding it is crashing.
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"bssidObserverArray"]) {
bssidObserverArray = [[NSUserDefaults standardUserDefaults] objectForKey:#"bssidObserverArray"];
}
if (bssidObserverArray.count > 0) {
if (![bssidObserverArray containsObject:vendorID]) {
NSLog(#"bssidObserverArray %#",bssidObserverArray);
// In this line app is getting crashed
[bssidObserverArray addObject:vendorID];
[[NSUserDefaults standardUserDefaults] setObject:bssidObserverArray forKey:#"bssidObserverArray"];
}
}else{
[bssidObserverArray addObject:vendorID];
[[NSUserDefaults standardUserDefaults] setObject:bssidObserverArray forKey:#"bssidObserverArray"];
}
Any suggestions would be more helpful.
Assuming bssidObserverArray is NSMutable array then come the problem here .
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"bssidObserverArray"]) {
bssidObserverArray = [[NSUserDefaults standardUserDefaults] objectForKey:#"bssidObserverArray"];
}
since NSUserdefaults returns immutable. you can do like this
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"bssidObserverArray"]) {
NSArray *someArray = [[NSUserDefaults standardUserDefaults] objectForKey:#"bssidObserverArray"];
bssidObserverArray = [someArray mutableCopy];
}
I need to update / add a key -> value pair in a NSMutableArray.
I would like to add a key with a fix value if the key isn't set already.
I tried the following - but the app is crashing at addObject with a "mutating method sent to immutable object" error:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *bookmarks = [defaults mutableArrayValueForKey:#"bookmarks"];
for (id bookmark in bookmarks) {
if ([bookmark objectForKey:#"type"] == NULL){
[bookmark addObject:#"old" forKey:#"type"];
}
}
[[NSUserDefaults standardUserDefaults] synchronize];
Your outer array contains immutable dictionaries. One option would be the following:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *bookmarks = [defaults mutableArrayValueForKey:#"bookmarks"];
[booksmarks enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSDictionary *bookmark, NSUInteger index, BOOL *stop) {
if (!bookmark[#"type"]) {
NSMutableDictionary *mutable = [bookmark mutableCopy];
mutable[#"type"] = #"old";
[bookmarks replaceObjectAtIndex:index withObject:[mutable copy]];
}
}];
// Update NSUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:bookmarks forKey:#"bookmarks"];
This should be nice an efficient since it can update multiple dictionaries concurrently.
Try this:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
//SAVING
NSMutableArray *bookmarksSaving = [NSMutableArray new];
[bookmarksSaving addObject:[[NSMutableDictionary alloc] initWithObjects:#[#"red"] forKeys:#[#"color"]]];
[bookmarksSaving addObject:[[NSMutableDictionary alloc] initWithObjects:#[#"yellow"] forKeys:#[#"color"]]];
[bookmarksSaving addObject:[[NSMutableDictionary alloc] initWithObjects:#[#"green",#"new"] forKeys:#[#"color",#"type"]]];
[defaults setObject:bookmarksSaving forKey:#"bookmarks"];
[[NSUserDefaults standardUserDefaults] synchronize];
//MODIFY
NSMutableArray *bookmarks = [defaults mutableArrayValueForKey:#"bookmarks"];
NSUInteger index = 0;
for (NSDictionary *bookmark in bookmarks) {
NSMutableDictionary *bookmarkMutable = [bookmark mutableCopy];
if ([bookmarkMutable objectForKey:#"type"] == nil)
{
[bookmarkMutable setValue:#"old" forKey:#"type"];
[bookmarks replaceObjectAtIndex:index withObject:bookmarkMutable];
}
index++;
}
[[NSUserDefaults standardUserDefaults] synchronize];
You have to save your bookmarks as NSMutableDictionary to be able to add/remove keys/value
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSArray *bookmarks = [defaults objectForKey:#"bookmarks"];
NSMutableArray *newBookMarks = [bookmarks mutableCopy];
for (NSDictionary *bookmark in bookmarks) {
NSMutableDictionary *newBookmark = [bookmark mutableCopy];
if (![bookmark objectForKey:#"type"]){
[newBookmark setObject:#"old" forKey:#"type"];
}
[newBookMarks addObject:newBookmark];
}
[defaults setObject:newBookMarks forKey:#"bookmarks"];
[defaults synchronize];
HOW To set user default again to dictionary.
Create the dictionary object and set value to it.
NSMutableDictionary * loginDictionary = [[NSMutableDictionary alloc] init];
[loginDictionary setValue:#"My Address" forKey:#"ADDRESS_KEY"];
[loginDictionary setValue:#"1234567890" forKey:#"PHONE_KEY"];
[[NSUserDefaults standardUserDefaults] setObject:newDictionary forKey:#"INFO_KEY"];
[[NSUserDefaults standardUserDefaults] synchronize];
Now I asses those dictionary value and assign to some other object.
NSDictionary *RetrievedDictionary = [[NSUserDefaults standardUserDefaults] dictionaryForKey:#"INFO_KEY"];
txt_Address.text = [RetrievedDictionary objectForKey:#"ADDRESS_KEY"];
txt_Mobile.text = [RetrievedDictionary objectForKey:#"PHONE_KEY"];
How to update?
This is what I try.
[[NSUserDefaults standardUserDefaults] setObject:#"Changed Address" forKey:#"ADDRESS_KEY"];
[[NSUserDefaults standardUserDefaults] setObject:#"0987654321" forKey:#"PHONE_KEY"];
[[NSUserDefaults standardUserDefaults] synchronize];
Its not working for me, Need your small suggestion what i am doing wrong.
#Thanks In Advance.
NSMutableDictionary* changedDictionary = [NSMutableDictionary dictionaryWithDictionary:[[NSUserDefaults standardUserDefaults] dictionaryForKey:#"INFO_KEY"]];
[changedDictionary setObject:#"Changed Address" forKey:#"ADDRESS_KEY"];
[changedDictionary setObject:#"0987654321" forKey:#"PHONE_KEY"];
[[NSUserDefaults standardUserDefaults] setObject:changedDictionary forKey:#"INFO_KEY"];
[[NSUserDefaults standardUserDefaults] synchronize];
How about this?
NSMutableDictionary *changedDictionary = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:#"INFO_KEY"] mutableCopy];
[changedDictionary setObject:txt_Address.text forKey:#"ADDRESS_KEY"];
[changedDictionary setObject:txt_Mobile.text forKey:#"PHONE_KEY"];
[[NSUserDefaults standardUserDefaults] setObject:changedDictionary forKey:#"INFO_KEY"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSMutableDictionary * loginDictionary = [[NSMutableDictionary alloc] init];
[loginDictionary setValue:#"My Address" forKey:#"ADDRESS_KEY"];
[loginDictionary setValue:#"1234567890" forKey:#"PHONE_KEY"];
[[NSUserDefaults standardUserDefaults] setObject:newDictionary forKey:#"INFO_KEY"];
[[NSUserDefaults standardUserDefaults] synchronize];
Your are creating loginDictionary, but adding newDictionary to the NSUserDefaults. Also I would suggest to use constant keys.
I saved a nsmutableArray inside a NSUserDefaults.
In the following case, it seems that all the elements from the array are equal to 0, even though in this case position 1 and position 5 should have 1 instead of 0 as a value. I know that NSUserDefaults elements are immutable but ...I did add that mutableCopy when retrieving the value.
Where am I wrong?
//create array
NSMutableArray *objArray = [[NSMutableArray alloc] init];
for (int i=0; i< 100;i++) [objArray addObject:#"0"];
[objArray replaceObjectAtIndex:1 withObject:#"1"];
[[NSUserDefaults standardUserDefaults] setObject:objArray forKey:name];
// update
elementPosition = 5;
NSMutableArray *objArray = [[NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:name]] mutableCopy];
[objArray replaceObjectAtIndex:elementPosition withObject:#"1"];
//check the array
NSMutableArray *objArray = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:nameFile]];
BOOL displayContent = true;
for (int i=0; i<[objArray count];i++)
{
if ([[objArray objectAtIndex:i] isEqualToString:#"0"])
{
displayContent = false;
}
}
I think when you retrieve and your mutable array from UserDefault and updated but you didn't set that new updated object to UserDefault, its having the old object which was set before. you have to store your update array again to userdefault with same key which update your UserDefautlt.
elementPosition = 5;
NSMutableArray *objArray = [[NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:name]] mutableCopy];
[objArray replaceObjectAtIndex:elementPosition withObject:#"1"];
[[NSUserDefaults standardUserDefaults] setObject:objArray forKey:name];
You should call the synchronize method
[[NSUserDefaults standardUserDefaults] synchronize];
I think you should fix your code follow my bellow code:
[[NSUserDefaults standardUserDefaults] setObject:objArray forKey:name];
[[NSUserDefaults standardUserDefaults] synchronize];
...
NSMutableArray *objArray = [[NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:name]] mutableCopy];
[objArray replaceObjectAtIndex:elementPosition withObject:#"1"];
[[NSUserDefaults standardUserDefaults] setObject:objArray forKey:nameFile];
[[NSUserDefaults standardUserDefaults] synchronize];
Replace your code with this:
//create array
NSMutableArray *objArray = [[NSMutableArray alloc] init];
for (int i=0; i< 100;i++){
[objArray addObject:#"0"];
}
[objArray replaceObjectAtIndex:1 withObject:#"1"];
[[NSUserDefaults standardUserDefaults] setObject:objArray forKey:name];
//First mistake. Missing this. Without this line the data is not saved in NSUserDefaults
[[NSUserDefaults standardUserDefaults] synchronize];
// update
elementPosition = 5;
NSMutableArray *objArray = [[NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:name]] mutableCopy];
[objArray replaceObjectAtIndex:elementPosition withObject:#"1"];
//Second Mistake. You did not update the NSUserDefaults again.
[[NSUserDefaults standardUserDefaults] setObject:objArray forKey:name];
[[NSUserDefaults standardUserDefaults] synchronize];
//check the array
NSMutableArray *objArray = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:name]];
BOOL displayContent = true;
for (int i=0; i<[objArray count];i++)
{
if ([[objArray objectAtIndex:i] isEqualToString:#"0"])
{
displayContent = false;
}
//Third mistake. Once the BOOL is made false in an if block you have to make it true in the else block, otherwise the value of the BOOL will remain false even if it does not enter the if block.
else{
displayContent = true;
}
NsLog(#"ArrayIndex=[%d];DisplayContent=[%d]",i,displayContent);
}
Happy coding.
When using my app in a foreign country, the google GMSGeocoder is returning the response in local language automatically. how can I set it to always return the the response in English?
Im using GMS SDK 1.7 and my code is something like this:
GMSGeocoder *geoCoder = [[GMSGeocoder alloc] init];
[geoCoder reverseGeocodeCoordinate:self.cellLocation.coordinate completionHandler:^(GMSReverseGeocodeResponse *respones, NSError *err) {
if([respones firstResult]) {
GMSAddress* address = [respones firstResult];
NSString* fullAddress = [NSString stringWithFormat:#"%#, %#",address.thoroughfare, address.locality];
self.theTextField.text = fullAddress;
} else {
self.theTextField.text = #"";
}
}];
Using a GMSGeocoder category can solve this issue, inspired by #DaNLtR
After that , It can set geocoder result as English .
#implementation GMSGeocoder (Load)
+(void)load {
[[self class] setUserLanguage:#"en-CN"];// set your wanted language.
NSLog(#"GMSGeocoder + load!");
}
- (void)dealloc {
[[self class] resetSystemLanguage];
NSLog(#"GMSGeocoder + dealloc!");
}
+ (void)setUserLanguage:(NSString *)userLanguage
{
if (!userLanguage.length) {
[[self class] resetSystemLanguage];
return;
}
[[NSUserDefaults standardUserDefaults] setValue:userLanguage forKey:#"UserLanguage"];
[[NSUserDefaults standardUserDefaults] setValue:#[userLanguage] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
+ (void)resetSystemLanguage
{
[[NSUserDefaults standardUserDefaults] removeObjectForKey:#"UserLanguage"];
[[NSUserDefaults standardUserDefaults] setValue:nil forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
#end
Why should in category?
A:I tested setLanguage: before GMSGeocoder reverseGeocodeCoordinate method, it can't affect geocoder result. After I saw DaNLtR's answer , I think we can setLanguge in load method.
Why should reset language?
A:Avoide affect other module or framework .
Just change it to your language parameters
int main(int argc, char * argv[])
{
#autoreleasepool {
//For Google Maps hebrew response
//[[NSUserDefaults standardUserDefaults] setObject:#[#"en"] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] setObject:#[#"he",#"he-IL"] forKey:#"AppleLanguages"];
[[NSUserDefaults standardUserDefaults] synchronize];
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}