搜索分类、插件与代码仓

插件详情v1.7.0

ua_client_hints

向 Flutter 提供用户代理客户端提示。

AndroidiOSWebmacOS
近 30 天下载
19.2万
喜欢数
52
Pub 分数
130

安装插件

flutter pub add ua_client_hints

插件介绍

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

ua_client_hints 包使 Flutter 应用能够访问 User-Agent Client Hints,通过标准化的 HTTP 头部提供详细的浏览器和设备信息。它简化了在 Android、iOS、Web 和 macOS 上获取客户端元数据(如平台、型号和品牌)的过程。这有助于开发者根据用户设备特性定制内容分发,而无需依赖对 User-Agent 字符串的字符串解析。该包与 HTTP 客户端无缝集成,并支持现代 Web 标准,实现保护隐私的设备检测。

适合场景

  • 设备特定的内容渲染
  • 浏览器和操作系统检测
  • 优化的资产交付
  • 注重隐私的分析
  • 跨平台兼容性测试

主要特点

  • 支持 Android、iOS、Web 和 macOS
  • 通过 HTTP 标头提供标准化的客户端提示
  • 减少对User-Agent字符串解析的依赖
  • 与现有 HTTP 客户端集成
  • 遵循现代网页标准进行设备检测

适合对象

  • 需要准确设备信息的开发者
  • 需要自适应用户界面/用户体验的应用程序
  • 专注于隐私和合规的项目
  • Web 和移动混合应用程序
  • 内容分发优化

使用建议

  • 需要兼容的服务器端支持
  • 仅限暴露客户端提示的设备
  • 并非所有浏览器都完全支持客户端提示
  • 可能需要额外的服务器配置
  • 取决于底层平台功能

原始 README

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

User-Agent Client Hints for Flutter

What's User-Agent Client Hints?

User-Agent Client Hints
Improving user privacy and developer experience with User-Agent Client Hints
User-Agent Client Hints Demo on Browser

⬇️ Response Accept-CH ⬆️ Request header ⬆️ RequestExample value Description
UA Sec-CH-UA "Chromium";v="84",
"Google Chrome";v="84"
List of browser brands and their significant version.
UA-Mobile Sec-CH-UA-Mobile ?1 Boolean indicating if the browser is
on a mobile device (?1 for true) or not (?0 for false).
UA-Full-Version Sec-CH-UA-Full-Version "84.0.4143.2" The complete version for the browser.
UA-Platform Sec-CH-UA-Platform "Android" The platform for the device,
usually the operating system (OS).
UA-Platform-Version Sec-CH-UA-Platform-Version "10" The version for the platform or OS.
UA-Arch Sec-CH-UA-Arch "ARM64" The underlying architecture for the device.
While this may not be relevant to displaying the page,
the site may want to offer a download which defaults to the right format.
UA-Model Sec-CH-UA-Model "Pixel 3" The device model.

Installation

This plugin is set the Null Safety.

Add this to your package's pubspec.yaml file by running the following command

$ flutter pub add ua_client_hints

Usage

Supported platforms:

  • Android
  • iOS
  • macOS
  • Web

On Web, values depend on browser support for User-Agent Client Hints. Some fields such as architecture, model, or platformVersion can be empty on browsers that don't expose high-entropy hints.

With Dio

Add to the request header.

class AppDio with DioMixin implements Dio {
  AppDio._([BaseOptions options]) {
    options = BaseOptions(
      baseUrl: 'https://wasabeef.jp',
    );

    this.options = options;
    interceptors.add(InterceptorsWrapper(
      onRequest: (options) async {
        // Add User-Agent Client Hints
        options.headers.addAll(await userAgentClientHintsHeader());
        return options;
      },
    ));
  }

  static Dio getInstance() => AppDio._();
}
APIs
final String ua = await userAgent();

print('User-Agent: $ua');  // e.g.. 'SampleApp/1.0.0 (Android 11; Pixel 4 XL; coral; arm64-v8a)'
// macOS example:
// 'SampleApp/1.0.0 (macOS 15.5.0; Mac; Mac15,12; arm64)'
// Web fallback example:
// 'SampleApp/1.0.0 (macOS 15.5.0; Google Chrome; arm64)'
final UserAgentData uaData = await userAgentData();

print('platform:        ${uaData.platform}');              // e.g.. 'Android'
print('platformVersion: ${uaData.platformVersion}');       // e.g.. '10'
print('model:           ${uaData.model}');                 // e.g.. 'Pixel 4 XL'
print('architecture:    ${uaData.architecture}');          // e.g.. 'arm64-v8a'
print('brand:           ${uaData.brand}');                 // e.g.. 'Google'
print('mobile:          ${uaData.mobile}');                // e.g.. true
print('device:          ${uaData.device}');                // e.g.. 'coral'
print('appName:         ${uaData.package.appName}');       // e.g.. 'SampleApp'
print('appVersion:      ${uaData.package.appVersion}');    // e.g.. '1.0.0'
print('packageName:     ${uaData.package.packageName}');   // e.g.. 'jp.wasabeef.ua'
print('buildNumber:     ${uaData.package.buildNumber}');   // e.g.. '1'
final Map<String, dynamic> header = await userAgentClientHintsHeader();

print("Sec-CH-UA-Arch:             ${header['Sec-CH-UA-Arch']}");             // e.g.. 'arm64-v8a'
print("Sec-CH-UA-Model:            ${header['Sec-CH-UA-Model']}");            // e.g.. 'Pixel 4 XL'
print("Sec-CH-UA-Platform:         ${header['Sec-CH-UA-Platform']}");         // e.g.. 'Android'
print("Sec-CH-UA-Platform-Version: ${header['Sec-CH-UA-Platform-Version']}"); // e.g.. '10'
print("Sec-CH-UA:                  ${header['Sec-CH-UA']}");                  // e.g.. '"SampleApp"; v="1.0.0"'
print("Sec-CH-UA-Full-Version:     ${header['Sec-CH-UA-Full-Version']}");     // e.g.. '1.0.0'
print("Sec-CH-UA-Mobile:           ${header['Sec-CH-UA-Mobile']}");           // e.g.. '?1' (true) or '?0' (false)

所属分类

源码仓库

使用此插件的项目