Ably SDK

Ably是一个实时同步数字体验的平台。无论是参与虚拟场所的活动、接收实时金融信息,还是监控实时车辆性能数据,用户都期望获得标准的实时数字体验。Ably 提供一套 API,用于构建、扩展和交付强大的实时数字体验,每月为 80 个国家/地区的超过 2.5 亿台设备提供服务。像 Bloomberg、HubSpot、Verizon 和 Hopin 这样的组织都依赖 Ably 平台来卸载在全球范围内关键业务实时数据同步日益增长的复杂性。有关更多信息,请参阅 Ably 文档

Ably Asset Tracking SDKs for Swift

.github/workflows/check.yml

概述

Ably Asset Tracking SDK 提供了一种简便的方法来跟踪多个资产,通过 Ably 实时网络和带有位置增强功能的 Mapbox Navigation SDK 提供实时位置更新。

状态

此 SDK 是一个 alpha 版本。这意味着它包含最终 SDK 功能的一个子集,API 可能会发生变化,并且尚未准备好用于生产环境。SDK 的最新版本可在本仓库的 发布部分 中找到。

Ably Asset Tracking 是

此仓库包含一个 Xcode 工作区(Examples/AblyAssetTracking.workspace),其中包含

多个示例应用程序/ Xcode 项目,以及

一个 Swift 包(ably-asset-tracking-swift),其中包含三个库/ SDK

文档

请访问 Ably Asset Tracking 文档,获取完整的 API 参考和代码示例。

有用的资源

安装

要求

这些 SDK 支持 iOS 和 iPadOS。 未来可能会开发对 macOS/ tvOS 的支持,具体取决于兴趣/需求。

Mapbox 设置

为了安装 Ably Asset Tracking SDK,您需要首先配置您的开发机器,使其具有下载 Mapbox Navigation SDK 的权限。 为此,请按照 Mapbox 安装指南 中“配置您的秘密令牌”下的说明设置您的 ~/.netrc 文件。

Swift Package Manager

您可以在 发布 页面上找到版本。

示例应用程序

发布者应用程序中的 AWS S3 支持(可选)

发布者示例应用程序能够从 AWS S3 存储桶中获取位置历史记录文件,并使用它来重播先前记录的行程。 要启用此功能,您需要在 Examples/PublisherExample/PublisherExample/Optional Resources/amplifyconfiguration.json 放置一个 Amplify 配置文件。 示例应用程序配置为使用 Cognito 进行身份验证,并使用 S3 进行存储。

在真实设备上运行示例应用程序

在模拟器上运行示例应用程序时,无需进行额外的设置,但是如果您尝试在真实设备上运行它们,您会遇到代码签名错误。

要在设备上运行示例应用程序,您需要在 Xcode 中更改代码签名设置

如果您遇到错误 The operation couldn’t be completed. Unable to launch ... because it has an invalid code signature, inadequate entitlements or its profile has not been explicitly trusted by the user.,则需要在您的设备上“信任”您的开发帐户

Iphone Settings -> General -> VPN & Device Management -> Your development profile

用法

Publisher SDK

Asset Publisher SDK 可以有效地获取设备上的位置数据,并将位置更新实时发布给其他订阅者。 以下是如何使用 Asset Publisher SDK 的示例

import AblyAssetTrackingPublisher

// Initialise a Publisher using the mandatory builder methods

publisher = try? PublisherFactory.publishers() // get a Publisher Builder
    .connection(ConnectionConfiguration(apiKey: ABLY_API_KEY, clientId: CLIENT_ID)) // provide Ably configuration with credentials
    .delegate(self) // provide delegate to handle location updates locally if needed
    .start()
    
// Start tracking an asset

publisher?.track(trackable: Trackable(id: trackingId)) // provide a tracking ID of the asset

有关其他可选构建器方法,请参阅 PublisherBuilder 协议。

Subscriber SDK

Asset Subscriber SDK 可用于从发布者实时接收位置更新。 以下是如何使用 Asset Subscribing SDK 的示例

import AblyAssetTrackingSubscriber

// Initialise a Subscriber using the mandatory builder methods

subscriber = try? SubscriberFactory.subscribers() // get a Subscriber Builder
    .connection(ConnectionConfiguration(apiKey: ABLY_API_KEY, clientId: CLIENT_ID)) // provide Ably configuration with credentials
    .trackingId(trackingId) // provide a Tracking ID for the asset to be tracked
    .delegate(self) // provide delegate to handle location updates locally if needed
    .start() // start listening to updates

有关其他可选构建器方法,请参阅 SubscriberBuilder 协议。

身份验证

Subscriber 和 Publisher SDK 都支持基本身份验证(API 密钥)和令牌身份验证。 通过将 ConnectionConfiguration 传递给 Subscriber 或 Publisher 构建器来指定此内容:SubscriberFactory.subscribers().connection(connectionConfiguration)

要使用基本身份验证,请在 Subscriber 或 Publisher 构建器上设置以下 ConnectionConfiguration

let connectionConfiguration = ConnectionConfiguration(apiKey: ABLY_API_KEY, clientId: clientId)

要使用令牌身份验证,您可以传递一个闭包,当 Ably 客户端需要进行身份验证或重新身份验证时,将调用该闭包

let connectionConfiguration = ConnectionConfiguration() { tokenParams, resultHandler in

    // Implement a request to your servers which provides either a TokenRequest (simplest), TokenDetails, JWT or token string.

    getTokenRequestJSONFromYourServer(tokenParams: tokenParams) { result in
        switch result {
        case .success(let tokenRequest):
            resultHandler(.success(.tokenRequest(tokenRequest)))
        case .failure(let error):
            resultHandler(.failure(error))
        }
    }
}

Ably Asset Tracking UI(位置动画器)

Location Animator 可以插值和动画地图注释视图。

// Instantiate Location Animator
let locationAnimator = DefaultLocationAnimator()
// Subscribe for `Location Animator` position updates
locationAnimator.subscribeForFrequentlyUpdatingPosition { position in
    // Update map view annotation position here
}
// Additionally you can subscribe for infrequently position updates
locationAnimator.subscribeForInfrequentlyUpdatingPosition { position in
    // Update map camera position
}
var locationUpdateIntervalInMilliseconds: Double

func subscriber(sender: Subscriber, didUpdateDesiredInterval interval: Double) {
    locationUpdateIntervalInMilliseconds = interval
}
// Feed animator with location changes from the `Subscriber SDK`
func subscriber(sender: Subscriber, didUpdateEnhancedLocation locationUpdate: LocationUpdate) {
    locationAnimator.animateLocationUpdate(location: locationUpdate, expectedIntervalBetweenLocationUpdatesInMilliseconds: locationUpdateIntervalInMilliseconds)
}

资源

功能支持

iOS 客户端库 功能支持矩阵

支持、反馈和故障排除

请访问 http://support.ably.com/ 以访问我们的知识库并寻求任何帮助。

您还可以查看 社区报告的 Github 问题

要查看最新版本中的更改,请参阅 CHANGELOG

已知限制

这些 SDK 支持 iOS 和 iPadOS。 未来可能会开发对 macOS/ tvOS 的支持,具体取决于兴趣/需求。

贡献

有关如何为此项目做出贡献的指南,请参阅 CONTRIBUTING.md