iOS Question How to get the country Code

Brian Robinson

Active Member
Licensed User
Longtime User
Hi All,

I am attempting to get the country code of the device based off the ISO value.

I found this bit of code:

B4X:
NSString *countryCode = [[NSLocale currentLocale] objectForKey: NSLocaleCountryCode];

and I have attempted to write it like this, but I am unsure how to specify the NSLocaleCountryCode value. This is what I have so far:
B4X:
Private Sub GetCountryCode As String
    Dim no As NativeObject
    Return no.Initialize("NSLocale") _
        .RunMethod("currentLocale", Null).RunMethod("objectForKey:", Array("NSLocaleCountryCode")).AsString
End Sub

Any ideas?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Use the new inline Objective C feature. It is simpler:
B4X:
  Dim nativeMe As NativeObject = Me
   Log(nativeMe.RunMethod("getCountryCode", Null).AsString)
End Sub

#If OBJC
- (NSString*) getCountryCode {
   NSString *countryCode = [[NSLocale currentLocale] objectForKey: NSLocaleCountryCode];
   return countryCode;
}
#end if
 
Upvote 0
Top