Как получить номер ICCID сим-карты внутри мобильного телефона во флаттере? Есть ли какой-нибудь пакет, который можно использовать для его получения? Я искал его, но смог найти пакеты, чтобы получить только номер, но не номер ICCID.
Привет, я не думаю, что существует пакет для получения номера ICCID с помощью флаттера, но вы можете получить то же самое, используя собственный код, который включает в себя некоторый собственный код.
вот дартс-код:
import 'dart:async';
import 'package:flutter/services.dart';
class SimInfo {
static const MethodChannel _channel = const MethodChannel('sim_info');
static Future<String> getIccid() async {
try {
final String iccid = await _channel.invokeMethod('getIccid');
return iccid;
} on PlatformException catch (e) {
print("Failed to get ICCID: '${e.message}'.");
return null;
}
}
}
Собственный код Android:
import android.content.Context;
import android.telephony.TelephonyManager;
import androidx.annotation.NonNull;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
public class SimInfoPlugin implements FlutterPlugin, MethodCallHandler {
private Context applicationContext;
@Override
public void onAttachedToEngine(@NonNull FlutterPluginBinding flutterPluginBinding) {
applicationContext = flutterPluginBinding.getApplicationContext();
final MethodChannel channel = new MethodChannel(flutterPluginBinding.getBinaryMessenger(), "sim_info");
channel.setMethodCallHandler(this);
}
@Override
public void onMethodCall(@NonNull MethodCall call, @NonNull Result result) {
if (call.method.equals("getIccid")) {
result.success(getIccid());
} else {
result.notImplemented();
}
}
private String getIccid() {
TelephonyManager telephonyManager = (TelephonyManager) applicationContext.getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager != null) {
return telephonyManager.getSimSerialNumber();
}
return null;
}
@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
}
}
Код Swfit (ios):
import Flutter
import UIKit
public class SwiftSimInfoPlugin: NSObject, FlutterPlugin {
public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "sim_info", binaryMessenger: registrar.messenger())
let instance = SwiftSimInfoPlugin()
registrar.addMethodCallDelegate(instance, channel: channel)
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
if call.method == "getIccid" {
result(getIccid())
} else {
result(FlutterMethodNotImplemented)
}
}
private func getIccid() -> String? {
let telephonyInfo = CTTelephonyNetworkInfo()
if let carrier = telephonyInfo.subscriberCellularProvider {
return carrier.iccAuthenticationKey
}
return nil
}
}
Надеюсь, что это работает!!!!! Приятного кодирования