MemfaultCloud iOS 库
示例应用
Xcode 工作区 Example/MemfaultCloud.xcworkspace
包含一个 DemoApp
目标。这是一个非常基础的 iOS 应用,展示了此库的功能。
在构建应用之前,请确保更新 AppDelegate.swift
中的项目密钥。要查找项目密钥,请登录至 https://app.memfault.com/ 并导航至设置。
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Enable debug logs:
gMFLTLogLevel = .debug
MemfaultApi.configureSharedApi([
kMFLTProjectKey: "<YOUR_PROJECT_KEY_HERE>",
])
return true
}
}
集成指南
将 MemfaultCloud 添加到您的项目中
Swift Package Manager
将此行添加到您的 Package.swift 依赖项列表中。
.package(name: "MemfaultCloud", url: "https://github.com/memfault/memfault-ios-cloud.git", from: "2.2.1"),
CocoaPods
如果您正在使用 CocoaPods,可以在您的 Podfile 中将 MemfaultCloud
添加为依赖项。
target 'MyApp' do
pod 'MemfaultCloud'
end
指定要使用的版本可能是个好主意。有关更多信息,请参阅 Podfile 文档。
添加新的依赖项后,在终端内或在 CocoaPods.app 中运行 pod install
。
无需依赖管理器
如果不想使用如 CocoaPods 这样的依赖管理器,只需克隆此存储库,并将 MemfaultCloud
文件夹中的 .h
和 .m
文件添加到您的项目中。
配置
MemfaultApi
类是 MemfaultCloud 库的主要类。建议使用返回单例实例的 MemfaultApi.sharedApi
属性,而不是“手动”创建实例。使用单例可以确保必要时按顺序向服务器发出请求。
在开始使用 MemfaultApi.sharedApi
之前,需要通过将配置字典传递给 MemfaultApi.configureSharedApi([...])
进行一次性且仅一次的配置。
项目密钥是配置的必备组件。要查找您的项目密钥,请登录到 https://app.memfault.com/ 并导航到设置。
MemfaultApi.configureSharedApi([
kMFLTProjectKey: "<YOUR_PROJECT_KEY_HERE>",
])
获取最新版本
api.getLatestRelease
可用于检查设备是否为最新版本,或者是否为其提供新的 OTA 更新有效负载。
该应用预期能够与设备通信,并获取其序列号、硬件版本、当前软件版本和类型。使用这些信息创建一个 MemfaultDeviceInfo
对象,并将其传递给 api.getLatestRelease
。
let deviceInfo = MemfaultDeviceInfo(
deviceSerial: "DEMO_SERIAL",
hardwareVersion: "proto",
softwareVersion: "1.0.0",
softwareType: "main")
MemfaultApi.sharedApi.getLatestRelease(for: deviceInfo) { (package, isUpToDate, error) in
if error != nil {
print("There was an error, handle it here.")
return
}
if package == nil {
print("Device is already up to date!")
return
}
print("Update available: \(package!.description)")
}
MemfaultOtaPackage
对象有一个 location
属性,其中包含指向 OTA 有效负载的 URL。
上传块
Memfault 固件 SDK 将需要发送回 Memfault 云的数据打包成“块”。有关设备/固件详情的更多信息,请参阅此教程。
此 iOS 库包含一个高级 API 用于提交块到 Memfault。
将块从设备传输到 iOS 应用程序是集成工作的一部分。假设您已经有一个可以在设备与 iOS 应用程序之间利用的通信机制。
// Array with Data objects, each with chunk bytes
// (produced by the Memfault Firmware SDK packetizer and sent
// to the iOS app to be posted to the cloud):
let chunks = [...]
MemfaultApi.shared.chunkSender(withDeviceSerial: "DEMO_SERIAL").postChunks(chunks)
自定义队列实现
默认队列实现基于 RAM。因此,如果主机应用被终止或 iOS 设备关机或重启,队列的内容将丢失。这可能是覆盖默认队列实现的原因。
库提供了一个“钩子”来覆盖默认队列实现,通过将 kMFLTChunkQueueProvider
配置选项设置为符合 MemfaultChunkQueueProvider
的对象。
class MyQueueProvider: MemfaultChunkQueueProvider {
func queue(withDeviceSerial deviceSerial: String) -> MemfaultChunkQueue {
// Get or create a queue for the given deviceSerial.
// The queue object needs to implement
// the MemfaultChunkQueue protocol.
let queue = ...
return queue
}
}
func bootMemfault() {
let queueProvider = MyQueueProvider()
MemfaultApi.configureSharedApi([
kMFLTProjectKey: "<YOUR_PROJECT_KEY_HERE>",
// Pass the custom queue provider in the configuration:
kMFLTChunkQueueProvider: queueProvider,
])
// If your queue implementation persists the contents of the
// queues between app restarts, you will need to call postChunks()
// after restarting the app. This re-registers the queues that were loaded
// from persistent storage and resumes the upload process again:
let deviceSerialsToResume = [ ... ]
for sn in deviceSerialsToResume {
MemfaultApi.shared.chunkSender(withDeviceSerial: sn).postChunks()
}
}
API 文档
MemfaultCloud.h
包含每个 API 的详细文档。
单元测试
Xcode 工作空间 Example/MemfaultCloud.xcworkspace
也包含一个 MemfaultCloud_Tests
方案。要运行测试,请选择此方案,然后选择 Product > Test (cmd + U)。
变更日志
请参阅CHANGELOG.md文件。