Alipay+ DocsAlipay+ Docs

Obtain the mobile country code

The user's mobile country code is used as the value of the region parameter in the getPaymentCode API. The following section introduces a solution for obtaining the user's mobile country code without disturbing the user. This solution is only for your reference and applicable when you need the user's country information and do not want to dynamically apply for user permission.

iOS

The following code is a sample you can run in your app to obtain the user's country code:

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;
}

Android

The following code is a sample you can run in your app to obtain the user's country code:

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