ストレージ情報の取得
接続中のデバイスへ最新のストレージ情報を要求し、更新された storageInfo プロパティを読み取ります。
前提条件
ストレージ情報を取得する前に、次の条件を確認してください。
- デバイスが接続済みで、安定した状態にあること
- デバイスが
DeviceInfoAPIプロトコルに対応していること
API リファレンス
フレームワーク
AIBuds.xcframework
インポート
SDK を使用するファイルで、メインフレームワークをインポートします。
- Swift
- Objective-C
import AIBuds#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>プロトコル
要求メソッドと結果プロパティは DeviceInfoAPI で定義されています。
- Swift
- Objective-C
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?
)
}@protocol AIBudsDeviceInfoAPI <AIBudsDeviceAPI>
/// Device storage information.
@property(nonatomic, readonly, strong) AIBudsStorageInfoModel *_Nullable storageInfo;
/// 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.
- (void)requestQueryStorageInfoWithCompletion:(AIBudsCompletionHandler _Nullable)completion;
@endインスタンスメソッド
デバイスの最新ストレージ情報を要求します。
- Swift
- Objective-C
/// 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?
)/// 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.
- (void)requestQueryStorageInfoWithCompletion:(AIBudsCompletionHandler _Nullable)completion;パラメータ
| パラメータ | 型 | 説明 |
|---|---|---|
completion | AIBudsCompletionHandler? | 要求完了時に呼び出される任意の完了ハンドラー。 |
コールバックのパラメータ:
| 名前 | 型 | 説明 |
|---|---|---|
success | Bool / BOOL | 要求が成功した場合は true、それ以外は false。 |
error | NSError? | 要求失敗時のエラー詳細。成功時は nil。 |
結果プロパティ:
| プロパティ | 型 | 説明 |
|---|---|---|
usedSpaceInMB | NSNumber | 使用済みストレージ容量(MB)。 |
freeSpaceInMB | NSNumber | 利用可能な残りストレージ容量(MB)。 |
戻り値
このメソッドの完了ハンドラーはストレージデータを返しません。要求成功後に、更新された storageInfo プロパティを読み取ります。
使用例
- Swift
- Objective-C
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")
}
}
}#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>
@interface DeviceManager ()
@property(weak, nonatomic) id<AIBudsDeviceConvertible> device;
@end
@implementation DeviceManager
- (void)queryStorageInformation {
id<AIBudsDeviceInfoAPI> device = (id<AIBudsDeviceInfoAPI>)self.device;
if (![device conformsToProtocol:@protocol(AIBudsDeviceInfoAPI)]) {
NSLog(@"Device does not support storage queries");
return;
}
[device requestQueryStorageInfoWithCompletion:^(BOOL success, NSError *_Nullable error) {
if (!success) {
NSLog(@"Storage query failed: %@", error.localizedDescription ?: @"Unknown error");
return;
}
AIBudsStorageInfoModel *storage = device.storageInfo;
if (!storage) {
NSLog(@"The device did not provide storage information");
return;
}
NSLog(@"Used storage: %@ MB", storage.usedSpaceInMB);
NSLog(@"Free storage: %@ MB", storage.freeSpaceInMB);
}];
}
@endエラー処理
- 更新後のプロパティを読み取る前に
successを確認します。 - 要求に失敗した場合は、
errorから詳細を取得します。 - コールバック成功後でも、
nilとなるstorageInfoを処理します。 - 対象デバイスについて文書化されていない固定エラーコードを前提にしないでください。
ベストプラクティス
- 成功後に読み取る: 問い合わせが成功してから
storageInfoにアクセスします。 - SDK の単位を維持する: どちらの値も MB として扱い、バイトとして解釈しないでください。
- プロトコル対応を確認する: デバイスが
DeviceInfoAPIに対応していることを確認します。 - メインキューで UI を更新する: 完了ハンドラーからの UIKit 更新はメインキューへ切り替えます。
注意事項
- 完了ハンドラーが返すのは要求の状態であり、
StorageInfoModelの結果は含まれません。 StorageInfoModelが公開するのは、MB 単位の使用済み容量と空き容量だけです。- SDK はこのモデルに総容量の個別プロパティを公開していません。
- メディアの録音、取り込み、削除中は、ストレージ情報が変化する場合があります。