CLLocationSimulator SPM 📦

Swift Version License Platform

cllocationsimulator 是一个 Swift 包,它提供了一个方便的接口来模拟 CLLocation 对象。它允许您在开发和测试期间模拟 iOS、macOS、tvOS 和 watchOS 应用程序的位置数据。 当您需要测试应用程序中基于位置的功能,而无需实际移动到不同的位置时,这非常有用。

特性

要求

安装

Swift 包管理器

要使用 Swift 包管理器将 CLLocationSimulator 集成到您的 Xcode 项目中,请按照以下步骤操作:

  1. 在 Xcode 中打开您的项目。
  2. 转到“File”(文件)> “Swift Packages”(Swift 包)> “Add Package Dependency…”(添加包依赖项…)
  3. 输入包 URL:https://github.com/yourusername/cllocationsimulator.git(将 yourusername 替换为您的 GitHub 用户名)。
  4. 按照提示指定版本、分支或标签。
  5. 点击“Next”(下一步),然后点击“Finish”(完成)。

手动

您也可以手动将 CLLocationSimulator 添加到您的项目中

  1. 克隆或下载该存储库。
  2. CLLocationSimulator 目录拖到您的 Xcode 项目中。

用法

  1. 在您的 Swift 文件中导入 CLLocationSimulator 模块

    import CLLocationSimulator
  2. 创建 LocationSimulator 的一个实例

    let locationsToSimulate: [CLLocation] = []
    let locationSimulator = CLLocationBaseSimulator(locations: locationsToSimulate)

    Example 中,您可以查看如何解析 JSON GPS 数据到 CLLocation 并将其传递给位置模拟器构造函数

  3. 跟踪所需参数的更改

    /// Change of simulation status
    /// - Parameter value: is active
    func activeStateChanged(value: Bool) {}
     
    /// Change of progress of simulation
    /// - Parameter value: new progress
    func progressChanged(value: Double) {}
     
    /// Change of locations
    /// - Parameter value: new locations
    func locationsChanged(value: [CLLocation]) {}
  4. 用直接方法替换加载的 CLLocations

     func changeLocations(_ locations: [CLLocation])
  5. 您可以使用 CLLocationBaseSimulator 作为原始位置提供程序,但您已经可以使用 3 种常见的实现。 它们也可以在 SPM 中使用。

CLLocationCombineSimulator

组合实现,仅跟踪所需的属性。 例如,如果 SwiftUI 中的重绘性能至关重要。

/// Publisher for Locations update
public var locationsPublisher: AnyPublisher<[CLLocation], Never>
    
/// Publisher for Progress update
public var progressPublisher: AnyPublisher<Double, Never>

/// Publisher for Status update
public var isActivePublisher: AnyPublisher<Bool, Never>

CLLocationPublisherSimulator

也是 Combine 实现,但任何属性的更改都会触发 SwiftUI 的整个视图更新。 如果性能不是那么关键。

/// Actual locations Publisher
@Published
public var locations: [CLLocation] = []
    
/// Actual progress Publisher
@Published
public var progress: Double = 0.0
    
/// Actual active status Publisher
@Published
public var isActive: Bool = false

CLLocationObservableSimulator

适用于 iOS 17 的新 SwiftUI 实现。 与 ObservableObject 相同的语法糖,但仅跟踪属性的更改才会导致重绘。 更多信息请点击这里

@Observable
public final class CLLocationObservableSimulator: CLLocationBaseSimulator {
    
/// Actual locations Publisher
public var locations: [CLLocation] = []
    
/// Actual progress Publisher
public var progress: Double = 0.0
    
/// Actual active status Publisher
public var isActive: Bool = false
  1. 开始初始位置发出。 将发送传递给构造函数的位置的第一个点。
locationSimulator.initialLocationEmit()
  1. 切换模式。 默认情况下,emit on interval 已设置。
locationSimulator.simulationMode = .emitOnInterval(1.0)
//or
locationSimulator.simulationMode = .emitOnTimestamp
  1. 要控制模拟,您可以使用简单的接口。
func start()

func pause()

func reset()

有关更详细的使用说明和示例,请参阅存储库中提供的 示例

示例应用

简单的 SwiftUI 应用程序来跟踪进度和更改 + 适用于 iOS 17 的新地图功能(pin、path、scale)

Combine App Publisher App Observed App

许可证

此项目已获得 MIT 许可证的许可 - 有关详细信息,请参阅 LICENSE 文件。

致谢

联系方式

LinkedIn Badge Twitter Badge

如果您有任何问题或建议,请随时在 GitHub 上提出 issue