搜尋套件、儲存庫與 APP 分類

套件詳情v0.2.0

intelligence

Apple 的 AppIntent 集成外掛程式。讓您的應用程式能被 Siri、捷徑應用程式、Apple Intelligence 等理解。

iOS
近 30 天下載
1.1萬
喜歡數
51
Pub 分數
140

安裝套件

flutter pub add intelligence

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

智慧套件是一項Flutter外掛程式,可讓iOS應用程式整合Apple的AppIntents框架,進而與Siri、捷徑應用程式及Apple智慧進行更深入的互動。它讓使用者能透過語音指令或捷徑觸發應用程式動作,提升可存取性與自動化功能。此外掛程式透過提供Dart層級的API來定義意圖、參數與結果,簡化將應用程式功能暴露給Apple生態系統的過程。此外掛專為iOS設計,需遵守Apple的AppIntents指南。

適用情境

  • 啟用Siri語音指令以執行應用程式動作
  • 與捷徑應用程式整合
  • 支援 Apple Intelligence 功能
  • 允許使用者自動化任務
  • 提升應用在蘋果生態系統中的可發現性

主要特色

  • 無縫整合 Apple AppIntents
  • 用於定義意圖的 Dart API
  • 支援 Siri 和快速指令
  • 僅限iOS平台相容性
  • 使用 Flutter pub add 輕鬆設定

適合對象

  • 面向 Apple Intelligence 的 iOS 開發者
  • 需要語音命令支援的應用程式
  • 面向自動化的應用程式
  • 在 iOS 上使用 Flutter 的開發者
  • 旨在實現更深入的 Siri 集成的應用程式

使用建議

  • 需要 iOS 17 或更高版本
  • 僅限蘋果生態系統
  • 需要在 Xcode 中設定 AppIntents
  • 不相容 Android
  • 取決於 Apple 不斷發展的 AppIntents 框架

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Intelligence

MIT license PRs Welcome Monterail's logo

Add support for Apple's AppIntents framework to your Flutter application. For details on how to add integration with Siri, Shortcuts app, and Apple Intelligence, see Recipes.

Installation

Copy and paste the following snippet into your shell when in the target project directory.

dart pub add intelligence

Usage

To add support for AppIntents framework, you will have to perform one-time setup which differs for different use-cases. See Recipes for more details.

iOS Configuration

After installing the package, you need to set the minimum iOS version to 16.0 on XCode.

For more details, follow these steps:

  • Open the iOS project in Xcode

    Open in Xcode
  • Click on Runner & then general tab

    Runner & general tab

  • Under the General tab & minimum deployments section, set the iOS version to atleast 16.0 iOS version


Once set up, the plugin will let you act on each App Intent trigger.

E.g. while setting the selection listener in a Stateful widget:

Intelligence().selectionsStream().listen(_handleSelection);

Note: the Intelligence class behaves like a singleton, so you do not have to maintain the same class instance throughout the app/use-case.

Recipes

List of practical applications of intelligence in your project. Click the Details dropdown to see the implementation.

Allow the Shortcuts app to open a specific page in your application

Will let your app to be automated via Shortcuts workflow.

  • Open the iOS project in Xcode
Open in Xcode
  • Add a new Swift file and paste:
import AppIntents
import intelligence

struct OpenHeartIntent: AppIntent {
  static var title: LocalizedStringResource = "Draw a Heart"
  static var openAppWhenRun: Bool = true
  
  @MainActor
  func perform() async throws -> some IntentResult {
    IntelligencePlugin.notifier.push("heart")
    return .result()
  }
}

Switch out the struct's name, title, and the .pushed value to ones that match your use-case.

Once added, your App Intent will show up in the Shortcuts app:

Example of a Shortcuts app workflow including an automation step declared in this guide
Optionally: Add a Siri voice shortcut to for the App Intent

To trigger the App Intent declared above by speaking a specific phrase to Siri, append:

struct OpenHeartShortcuts: AppShortcutsProvider {
  static var appShortcuts: [AppShortcut] {
    AppShortcut(
      intent: ExampleAppIntent(),
      phrases: [
        "Draw my favorite shape in \(.applicationName)"
      ]
    )
  }
}

Once deployed to the device, Siri can understand the trigger phrase and run the App Intent declared above:

Siri's response to a query 'What can Intelligence do?' including defined phrase to run the App Intent declared in this guide
Let Siri open a specific entity from your app domain

Siri is capable of understanding the entities your application revolves around, letting you implement App Intents with variables.

See full implementation in the example project.

  • Define an AppEntity. It should contain a unique identifier and a text representation fields, as follows:
import CoreSpotlight
import AppIntents

struct RepresentableEntity: AppEntity {
  static var defaultQuery: RepresentableQuery = RepresentableQuery()
  static var typeDisplayRepresentation = TypeDisplayRepresentation(name: "Shape")
  
  var displayRepresentation: DisplayRepresentation {
    DisplayRepresentation(stringLiteral: representation)
  }
  
  let id: String
  let representation: String
}

extension RepresentableEntity: IndexedEntity {
  var attributeSet: CSSearchableItemAttributeSet {
    let attributes = CSSearchableItemAttributeSet()
    attributes.displayName = self.representation
    return attributes
  }
}
  • Create a matching EntityQuery, like so:
import AppIntents
import intelligence

struct RepresentableQuery: EntityQuery {
  func entities(for identifiers: [String]) async throws -> [RepresentableEntity] {
    return IntelligencePlugin.storage.get(for: identifiers).map() { item in
      return RepresentableEntity(
        id: item.id,
        representation: item.representation
      )
    }
  }
  
  func suggestedEntities() async throws -> [RepresentableEntity] {
    return IntelligencePlugin.storage.get().map() { item in
      return RepresentableEntity(
        id: item.id,
        representation: item.representation
      )
    }
  }
}

extension RepresentableQuery: EnumerableEntityQuery {
  func allEntities() async throws -> [RepresentableEntity] {
    return IntelligencePlugin.storage.get().map() { item in
      return RepresentableEntity(
        id: item.id,
        representation: item.representation
      )
    }
  }
}
  • Create an AppIntent using the entity as a parameter.
import AppIntents
import intelligence

struct ExampleAppIntent: AppIntent {
  static var title: LocalizedStringResource = "Draw shape"
  static var openAppWhenRun: Bool = true
    
  @Parameter(title: "Shape")
  var target: RepresentableEntity
  
  @MainActor
  func perform() async throws -> some IntentResult {
    IntelligencePlugin.notifier.push(target.id)
    return .result()
  }
  
  static var parameterSummary: some ParameterSummary {
    Summary("Draw \(\.$target)")
  }
}


struct AppShortcuts: AppShortcutsProvider {
  static var appShortcuts: [AppShortcut] {
    AppShortcut(
      intent: ExampleAppIntent(),
      phrases: [
        "Draw a \(\.$target) in \(.applicationName)"
      ]
    )
  }
}

  • In your AppDelegate file, add the following code to the didFinishLaunchingWithOptions method:
  IntelligencePlugin.storage.attachListener {
    AppShortcuts.updateAppShortcutParameters()
  }
  if #available(iOS 18.0, *) {
    IntelligencePlugin.spotlightCore.attachEntityMapper() { item in
      return RepresentableEntity(
        id: item.id,
        representation: item.representation
      )
    }
  }
  • In your Dart code, use the .populate method to let the operating system know about the entities available in your app:
await IntelligencePlugin().populate(const [
  Representable(representation: 'Heart', id: 'heart'),
  Representable(representation: 'Circle', id: 'circle'),
  Representable(representation: 'Rectangle', id: 'rectangle'),
  Representable(representation: 'Triangle', id: 'triangle'),
]);

Note: each call to .populate overwrites previous entities. Call .populate([]) once the entities are not accessible anymore, e.g. after a logout.

Result:

Siri query usage example

Further reading

所屬分類

依賴

依賴清單 4 項

原始碼儲存庫

使用此套件的專案