【react-native】--------------移动端事件-------------【劉】

在react-naitve检测手机是否安装SIM卡

1、Android检测

  import android.telephony.TelephonyManager;
    /**
      * 判断是否包含SIM卡
      *
      * @return 状态
      */
  public static boolean hasSimCard(Context context) {
    TelephonyManager telMgr = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    int simState = telMgr.getSimState();
    boolean result = true;
    switch (simState) {
    case TelephonyManager.SIM_STATE_ABSENT:
      result = false; // 没有SIM卡
      break;
    case TelephonyManager.SIM_STATE_UNKNOWN:
      result = false;
      break;
    }
    Log.e("try", result ? "有SIM卡" : "无SIM卡");
    return result;
  }

2、IOS检测

  RCT_EXPORT_METHOD(isSIMInstalled:(RCTResponseSenderBlock)complete){
    CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];

    CTCarrier *carrier = [networkInfo subscriberCellularProvider];
     
    if (!carrier.isoCountryCode) {
        NSLog(@"未安装SIM卡");
      }else{
        NSLog(@"存在SIM卡");
      }
}

  

猜你喜欢

转载自www.cnblogs.com/lstcon/p/11953345.html