搜索插件、代码仓与 APP 分类

插件详情v1.4.0

device_region

此插件使用平台特定的 API 返回 SIM 卡国家代码

AndroidiOSWindowsLinux
近 30 天下载
1.8万
喜欢数
26
Pub 分数
150

安装插件

flutter pub add device_region

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

device_region 包使用 Android、iOS、Windows 和 Linux 上的平台特定 API 检索 SIM 卡国家代码。它提供了一种简单且跨平台的方法,用于确定与设备 SIM 卡关联的国家,这在本地化、基于区域的内容分发或合规性检查中非常有用。该插件抽象了原生实现细节,为各个平台提供了统一的 Dart 接口。它不需要互联网连接,直接从设备级数据运行。

适合场景

  • 根据SIM卡确定用户所在国家
  • 按地区本地化应用内容
  • 强制执行区域特定限制
  • 在表单中自动设置默认国家
  • 支持多区域应用

主要特点

  • 跨平台支持(Android、iOS、Windows、Linux)
  • 使用原生SIM国家代码API
  • 无需互联网
  • 简单、轻量级的 API
  • 返回 ISO 3166-1 alpha-2 国家代码

适合对象

  • 无需GPS即可实现区域检测的应用
  • 需要国家上下文的旅行或支付应用
  • 本地化框架
  • 离线优先的应用程序
  • 面向多平台的开发者

使用建议

  • 依赖于SIM卡的存在;如果没有SIM卡,可能返回null
  • 在没有SIM卡的设备(例如平板电脑)上无法使用
  • 国家代码可能无法反映用户当前的位置
  • 仅限基于SIM卡的国家识别
  • 某些平台可能需要运行时权限

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

device_region

A Flutter plugin which uses platform-specific API to return a SIM country code. Supports iOS and Android.

Installation

In your Flutter project, in pubspec.yaml file add the dependency:

dependencies:
  device_region: ^1.4.0

Usage example

Import device_region.dart package.

import 'package:device_region/device_region.dart';
Get SIM country code

Call getSIMCountryCode method to receive SIM country code.

await DeviceRegion.getSIMCountryCode();
Android:

Using TelephonyManager class:

Returns simCountryIso if available. Otherwise, it should return networkCountryIso.

In case the value cannot be retrieved or an error occurs, null value will be returned

iOS:
iOS 16.0 and higher

Due to deprecated properties in CTTelephonyNetworkInfo class, plugin uses Locale class and returns Locale.current.region.identifier.

iOS up to 16.0

Using CTTelephonyNetworkInfo class:

Returns isoCountryCode if available.

In case the value cannot be retrieved or an error occurs, null value will be returned

Important notes

  • While call getSIMCountryCode on iOS (up to 16.0), it returns proper country code and can throw error log in debugger as below:
[Client] Updating selectors after delegate addition failed with: Error Domain=NSCocoaErrorDomain Code=4099 "The connection to service with pid 91 named com.apple.commcenter.coretelephony.xpc was invalidated from this process." UserInfo={NSDebugDescription=The connection to service with pid 91 named com.apple.commcenter.coretelephony.xpc was invalidated from this process.}

It's well known bug on iOS and it does not affect the operation of the application.

  • Method returns null in case if for some reason it was not possible to retrieve the information from the SIM slot. So it will return null i.e on iOS simulators and devices without SIM card inserted.

  • On iOS, it only works from 12.0 and above. For lower versions it will return null.

所属分类

源码仓库

使用此插件的项目