搜尋分類、套件與程式碼儲存庫

套件詳情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)

所屬分類

原始碼儲存庫

使用此套件的專案