0

Background: I have in-app language change in my application and I also have number formatting that is based on locale. I have written an extension to NSLocale to handle this. My locale contains the in-app language. All this is working fine for me.

Issue: In few places (ex keyboard) locale is still read by platform from [NSLocale locale]. When device & in-app languages are different this is causing issues to me. I have tried to fix this by setting in-app language to user default. This works but only after I kill the app & restart it again. After restart [NSLocale locale] is returning correct values and keyboard behaves like I want.

NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults]; 
NSString* lang = @"en_US"; 
[defaults setObject:@[lang] forKey:@"AppleLanguages"]; 
[defaults setObject:@[lang] forKey:@"NSLanguages"]; 
[defaults setObject:lang forKey:@"AppleLocale"]; 

Question1: Is there any legal way to kill/restart the app? Apple it self is doing this ex in language change or notification settings change. Is there any way for me to do this?

Question2: Would there be any other way fix my issue?

  • Just an idea, but what about instead of just an extension you swizzle the `locale` method on app start to include a check for your desired result? That would even work for places that don't use your extension without a restart, wouldn't it? I might be wrong, but perhaps it's worth a test to avoid trying to bypass the app review (which might be bad for you if they ever find out). Also note several answers in http://stackoverflow.com/questions/4399611/ that explain that in some designs, Apple might allow exiting with a plain old `exit(0)`. – Gero May 12 '17 at 07:26

1 Answers1

2
  1. While there is no legal way, it's certainly possible. Just call a non-existing selector. Some people even set a date conditional around this so that this functionality only comes into effect after it passes the apple review process.

  2. You need to swizzle the following system methods and override them with your own values:

     autoupdatingCurrentLocale
     systemLocale
     currentLocale
     preferredLanguages
     availableLocaleIdentifiers
    

If you don't know how to do that, check out http://nshipster.com/method-swizzling/ as well as JRSwizzle: https://github.com/rentzsch/jrswizzle

HTH

whyp
  • 603
  • 5
  • 14
  • Oh, look here, maybe I should read other answers before commenting on the question. :) I'd try the swizzling, as cheating the app review process sounds dangerous, imo (in terms of repercussions to your dev membership, even Apple won't send goons after you, I hope... ;D). – Gero May 12 '17 at 07:29