Ich muss das aktuelle Land in den iPhone-Einstellungen abrufen. Kann mir jemand sagen, wie man das aktuelle Land in der iPhone-Anwendung bekommt.
Ich muss das aktuelle Land zum Parsen des RSS-Feeds verwenden, in dem ich das aktuelle Land übergeben muss.
Bitte helfen Sie mir, das Land zu finden.
Danke im Voraus.
So finden Sie das Land der vom Benutzer gewählten Sprache:
NSLocale *currentLocale = [NSLocale currentLocale]; // get the current locale.
NSString *countryCode = [currentLocale objectForKey:NSLocaleCountryCode];
// get country code, e.g. ES (Spain), FR (France), etc.
In Swift:
let currentLocale = NSLocale.currentLocale()
let countryCode = currentLocale.objectForKey(NSLocaleCountryCode) as? String
Wenn Sie den Ländercode der aktuellen Zeitzone ermitteln möchten, lesen Sie Antwort von @ chings228 .
Wenn Sie den Ländercode des physischen Standorts des Geräts ermitteln möchten, benötigen Sie CoreLocation mit umgekehrter Geokodierung. Einzelheiten finden Sie in dieser Frage: Wie kann ich den aktuellen Standort vom Benutzer in iOS abrufen?
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCode = [locale objectForKey: NSLocaleCountryCode];
NSString *country = [locale displayNameForKey: NSLocaleCountryCode value: countryCode];
Für Swift 4.0,
Sie können einfach verwenden
let countryCode = Locale.current.regionCode
print(countryCode)
Für Geräte mit SIM-Karte können Sie Folgendes verwenden:
CTTelephonyNetworkInfo * network_Info = [CTTelephonyNetworkInfo new];
CTCarrier * carrier = network_Info.subscriberCellularProvider;
NSString * countryCode = [carrier.isoCountryCode uppercaseString];
NSLocale *countryLocale = [NSLocale currentLocale];
NSString *countryCode = [countryLocale objectForKey:NSLocaleCountryCode];
NSString *country = [countryLocale displayNameForKey:NSLocaleCountryCode value:countryCode];
NSLog(@"Country Code:%@ Name:%@", countryCode, country);
//Country Code:IN Name:India
NSTimeZone *gmt = [NSTimeZone localTimeZone];
NSLog(@"timezone gmt %@",gmt.abbreviation);
Wenn Sie testen und eine andere Sprache als Ihr System verwenden möchten, ist es am einfachsten, die Optionen Application Language
Und Application Region
Ihres Schemas zu ändern, anstatt die Sprache und Region auf Ihrem Gerät oder Simulator zu ändern.
Gehen Sie zu Product
-> Scheme
-> Edit Scheme...
-> Run
-> Options
und wählen Sie Application Language
Und Application Region
, Den Sie testen möchten.
Aus der iOS-Dokumentation: Testen Ihrer internationalisierten App
Swift 3.0 ist der neueste Arbeitscode
if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
print(countryCode)
}
Swift 3. Lösung
if let countryCode = (Locale.current as NSLocale).object(forKey: .countryCode) as? String {
print(countryCode)
}