メインコンテンツまでスキップ

ストレージ情報の取得

接続中のデバイスへ最新のストレージ情報を要求し、更新された storageInfo プロパティを読み取ります。

前提条件

ストレージ情報を取得する前に、次の条件を確認してください。

  • デバイスが接続済みで、安定した状態にあること
  • デバイスが DeviceInfoAPI プロトコルに対応していること

API リファレンス

フレームワーク

AIBuds.xcframework

インポート

SDK を使用するファイルで、メインフレームワークをインポートします。

Swift
import AIBuds

プロトコル

要求メソッドと結果プロパティは DeviceInfoAPI で定義されています。

Swift
protocol DeviceInfoAPI: DeviceAPI {
    /// Device storage information.
    var storageInfo: StorageInfoModel? { get }

    /// Request to query the device storage information.
    /// - Parameters:
    ///   - completion: A closure that is called when the operation completes.
    ///   - success: `true` if the operation was successful; otherwise `false`.
    ///   - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
    func requestQueryStorageInfo(
        _ completion: AIBudsCompletionHandler?
    )
}

インスタンスメソッド

デバイスの最新ストレージ情報を要求します。

Swift
/// Request to query the device storage information.
/// - Parameters:
///   - completion: A closure that is called when the operation completes.
///   - success: `true` if the operation was successful; otherwise `false`.
///   - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
func requestQueryStorageInfo(
    _ completion: AIBudsCompletionHandler?
)

パラメータ

パラメータ説明
completionAIBudsCompletionHandler?要求完了時に呼び出される任意の完了ハンドラー。

コールバックのパラメータ:

名前説明
successBool / BOOL要求が成功した場合は true、それ以外は false
errorNSError?要求失敗時のエラー詳細。成功時は nil

結果プロパティ:

プロパティ説明
usedSpaceInMBNSNumber使用済みストレージ容量(MB)。
freeSpaceInMBNSNumber利用可能な残りストレージ容量(MB)。

戻り値

このメソッドの完了ハンドラーはストレージデータを返しません。要求成功後に、更新された storageInfo プロパティを読み取ります。

使用例

Swift
import AIBuds

final class DeviceManager {
    weak var device: DeviceConvertible?

    func queryStorageInformation() {
        guard let device = device as? DeviceInfoAPI else {
            print("Device does not support storage queries")
            return
        }

        device.requestQueryStorageInfo { success, error in
            guard success else {
                print("Storage query failed: \(error?.localizedDescription ?? "Unknown error")")
                return
            }

            guard let storage = device.storageInfo else {
                print("The device did not provide storage information")
                return
            }

            print("Used storage: \(storage.usedSpaceInMB) MB")
            print("Free storage: \(storage.freeSpaceInMB) MB")
        }
    }
}

エラー処理

  1. 更新後のプロパティを読み取る前に success を確認します。
  2. 要求に失敗した場合は、error から詳細を取得します。
  3. コールバック成功後でも、nil となる storageInfo を処理します。
  4. 対象デバイスについて文書化されていない固定エラーコードを前提にしないでください。

ベストプラクティス

  1. 成功後に読み取る: 問い合わせが成功してから storageInfo にアクセスします。
  2. SDK の単位を維持する: どちらの値も MB として扱い、バイトとして解釈しないでください。
  3. プロトコル対応を確認する: デバイスが DeviceInfoAPI に対応していることを確認します。
  4. メインキューで UI を更新する: 完了ハンドラーからの UIKit 更新はメインキューへ切り替えます。

注意事項

  • 完了ハンドラーが返すのは要求の状態であり、StorageInfoModel の結果は含まれません。
  • StorageInfoModel が公開するのは、MB 単位の使用済み容量と空き容量だけです。
  • SDK はこのモデルに総容量の個別プロパティを公開していません。
  • メディアの録音、取り込み、削除中は、ストレージ情報が変化する場合があります。