Alipay+ DocsAlipay+ Docs

Obtain location information

The topic introduces two methods for obtaining location information from the user's mobile device without disturbing the user. The methods are only for your reference and applicable when you need the user's location information and do not want to dynamically apply for user permission.

Method 1: Obtain the region code

The region code is a 2-character code that follows the ISO 3166 standard. You can refer to the following code snippets to obtain the user's region code from Android and iOS devices respectively:

Note:

  • Inaccuracies may occur if you use an emulator to get the region code.
  • Inaccuracies may occur if no SIM card is inserted in the device.
  • The method may be unreliable on CDMA networks.

Android

copy
public String getContryCode(Context context) {
    TelephonyManager tm = (TelephonyManager)context.getSystemService(getApplicationContext().TELEPHONY_SERVICE);
String countryCode = tm.getNetworkCountryIso();
return countryCode;
}

iOS

copy
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import <CoreTelephony/CTCarrier.h>

- (NSString *)getMobileCountryCode {
    CTTelephonyNetworkInfo *info = [[CTTelephonyNetworkInfo alloc] init];
    CTCarrier *carrier = [info subscriberCellularProvider];
    NSString *mcc = [carrier mobileCountryCode];
    return mcc;
}

Note: The above code snippet does not apply if the iOS version is 16.4 or later.

Method 2: Obtain the time zone

You can refer to the following code snippets to obtain the user's time zone from Android and iOS devices respectively:

Android

copy
TimeZone.getDefault().getID()

iOS

copy
+ (NSString *)getCurrentSystemTimeZoneName {
    [NSTimeZone resetSystemTimeZone];
    NSTimeZone *currentSystemTimeZone = [NSTimeZone systemTimeZone];
    return [currentSystemTimeZone name];
}