InstantSearch 7.26.3

InstantSearch 7.26.3

由以下人员维护:aallamvladislavRobert MogosDhaya



  • Algolia

InstantSearch iOS

Pod Version Pod Platform Carthage compatible SwiftPM compatible Mac Catalyst compatible Licence

Algolia 提供。

InstantSearch 系列:InstantSearch iOS | InstantSearch Android | React InstantSearch | InstantSearch.js | Angular InstantSearch | Vue InstantSearch

InstantSearch iOS 是一个框架,提供组件和辅助工具,帮助您在 iOS 上使用 Algolia 构建最佳的即时搜索体验。它是建立在 Algolia 的 Swift API 客户端库 之上,为您提供快速构建各种搜索界面的高级解决方案。

结构

InstantSearch iOS 由三个产品组成

  • InstantSearch Insights - 允许开发者捕获搜索相关事件的库
  • InstantSearch Core - InstantSearch 的业务逻辑模块
  • InstantSearch - 完整的 InstantSearch 工具集,包括 UIKit 组件
  • InstantSearch SwiftUI - 在 Core 组件之上使用的 SwiftUI 数据模型和视图集

示例

您可以在 示例项目 中看到 InstantSearch iOS 的实际应用。它包含使用 InstantSearch 和用 Swift 编写的搜索组件和体验。

安装

Swift 包管理器

Swift 包管理器是一个用于管理 Swift 代码分发的工具。它集成于 Swift 构建系统,以自动化依赖项的下载、编译和链接的过程。自 Swift 5 和 Xcode 11 发布以来,SPM 与 iOS、macOS 和 tvOS 构建系统兼容,可创建应用程序。

要使用 SwiftPM,您应使用 Xcode 11 打开您的项目。点击 文件 -> Swift 包 -> 添加包依赖项,输入 InstantSearch 仓库的 URL。接下来,从提供的列表中选择您在项目中打算使用的产品。

如果您是框架作者,且将 InstantSearch 作为依赖项使用,请更新您的 Package.swift 文件

let package = Package(
    // 7.25.0 ..< 8.0.0
    dependencies: [
        .package(url: "https://github.com/algolia/instantsearch-ios", from: "7.25.0")
    ],
    // ...
)

CocoaPods

CocoaPods 是 Cocoa 项目的依赖管理器。

要安装 InstantSearch,只需将以下行添加到您的 Podfile 中

pod 'InstantSearch', '~> 7.25'
# pod 'InstantSearch/Insights' for access to Insights library only
# pod 'InstantSearch/Core' for access business logic without UIKit components
# pod 'InstantSearch/SwiftUI' for access to SwiftUI components

然后,运行以下命令

$ pod update

Carthage

Carthage 是 Cocoa 的简单、去中心化依赖管理器。

  • 要安装 InstantSearch,只需将以下行添加到您的 Cartfile 中
github "algolia/instantsearch-ios" ~> 7.25
  • 从项目目录中启动以下命令
carthage update
./Carthage/Checkouts/instant-search-ios/carthage-prebuild
carthage build

注意事项:目前,Carthage 无法提供仅构建特定存储库子组件(或与 CocoaPods 的 subspecs 等效)的选项。所有组件及其依赖项都将使用上述命令构建。然而,您不需要将未被使用的框架复制到您的项目中。例如,如果您没有使用来自 InstantSearch 的 UI 组件,您可以在执行 carthage update 操作完成后,从 Carthage 构建目录中删除该框架,只保留 InstantSearchCore。如果您只需要事件跟踪功能,删除除了 InstantSearchInsights 框架以外的所有框架。

如果您是第一次在项目中使用 Carthage,您需要按照解释进行一些额外的步骤,如 Carthage 中的说明

文档

您可以从 入门指南 开始。

专门的文档网站上 了解更多关于 instantSearch iOS 的信息。

基本用法

在您的 ViewController.swift

import InstantSearch

struct Item: Codable {
  let name: String
}

class SearchResultsViewController: UITableViewController, HitsController {
  
  var hitsSource: HitsInteractor<Item>?
    
  override func viewDidLoad() {
    super.viewDidLoad()
    tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
  }
      
  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    hitsSource?.numberOfHits() ?? 0
  }
  
  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
    cell.textLabel?.text = hitsSource?.hit(atIndex: indexPath.row)?.name
    return cell
  }
  
  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    if let _ = hitsSource?.hit(atIndex: indexPath.row) {
      // Handle hit selection
    }
  }
  
}

class ViewController: UIViewController {
      
  lazy var searchController = UISearchController(searchResultsController: hitsViewController)
  let hitsViewController = SearchResultsViewController()

  let searcher = HitsSearcher(appID: "latency",
                              apiKey: "1f6fd3a6fb973cb08419fe7d288fa4db",
                              indexName: "bestbuy")
  lazy var searchConnector = SearchConnector<Item>(searcher: searcher,
                                                    searchController: searchController,
                                                    hitsInteractor: .init(),
                                                    hitsController: hitsViewController)
  
  override func viewDidLoad() {
    super.viewDidLoad()
    searchConnector.connect()
    searcher.search()
    setupUI()
  }
  
  override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)
    searchController.isActive = true
  }
  
  func setupUI() {
    view.backgroundColor = .white
    navigationItem.searchController = searchController
    searchController.hidesNavigationBarDuringPresentation = false
    searchController.showsSearchResultsController = true
    searchController.automaticallyShowsCancelButton = false
  }
      
}

现在您可以构建并运行应用程序,以查看基本搜索体验的实际效果。您应该会看到每个按键输入后,结果都会发生变化。

为了获得更有意义的搜索体验,请按照 入门指南 进行操作。如果您正在构建 SwiftUI 应用程序,请查看 使用 SwiftUI 入门指南

如果您的项目中只需要业务逻辑模块,并使用 InstantSearchCore 框架,请将 import InstantSearchCore 添加到您的源文件中。

日志

库会产生 7 级别严重程度的日志。默认严重程度级别为 .info。您可以配置日志级别如下

Logs.logSeverityLevel = .debug

遥测

InstantSearch iOS 在运行时收集数据点。这有助于 InstantSearch 团队改进和优先安排未来的开发。

以下是收集到的数据的详尽列表

  • InstantSearch 版本
  • 实例化 InstantSearch 组件的名称,例如,HitsSearcherFilterState
  • 具有自定义参数(覆盖默认值)的组件的名称。InstantSearch 不收集这些参数的值。例如,FacetListInteractorfacets 参数的默认值是一个空列表。如果您用一个面相列表实例化它,然后遥测会跟踪 facets 参数接收到了自定义值,但不是该值本身。

InstantSearch 不收集任何敏感或个人信息。然而,您仍然可以使用以下代码选择退出遥测收集

InstantSearchTelemetry.shared.isEnabled = false

获取帮助

  • 需要帮助吗?Algolia 社区 或在 Stack Overflow 提出问题。
  • 遇到问题? 在联系支持之前,我们建议您查看我们的 FAQ,那里您可以找到关于框架中最常见问题和注意事项的答案。
  • 发现了一个 bug? 您可以打开 GitHub 问题
  • 关于 Algolia 的问题? 您可以在我们的网站上搜索我们的 FAQ

参与

  • 如果您 希望贡献,请随时 提交拉取请求
  • 如果您 有功能请求,请 打开问题
  • 如果您在您的应用中使用了 InstantSearch,我们非常希望听您分享相关信息!请在 讨论区Twitter 上与我们联系。

许可证

InstantSearch iOS遵循 Apache 2.0 许可证