デバイス時刻の設定
接続中のデバイスの内蔵時計を、アプリから指定した日時に設定します。
前提条件
デバイス時刻を設定する前に、次の条件を確認してください。
- デバイスが接続済みで、安定した状態にあること
- デバイスが
DeviceInfoAPIプロトコルに対応していること - 対象の
Dateが、アプリから送信する意図した日時を表していること
API リファレンス
フレームワーク
AIBuds.xcframework
インポート
SDK を使用するファイルで、メインフレームワークをインポートします。
- Swift
- Objective-C
import AIBuds#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>プロトコル
setDeviceTime メソッドは DeviceInfoAPI で定義されています。このプロトコルは基底デバイス API プロトコルを継承します。
- Swift
- Objective-C
/// The protocol for device information related API.
protocol DeviceInfoAPI: DeviceAPI {
/// Sets the device's system time.
/// - Parameters:
/// - date: The target time to set on the device.
/// - completion: A closure that is called when the operation completes.
/// - success: `true` if the operation was successful; otherwise `false`.
/// - statusCode: The status code returned by the device. `nil` if the operation failed.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
func setDeviceTime(
to date: Date,
completion: AIBudsStatusCodeCompletionHandler?
)
}/// The protocol for device information related API.
@protocol AIBudsDeviceInfoAPI <AIBudsDeviceAPI>
/// Sets the device's system time.
/// - Parameters:
/// - date: The target time to set on the device.
/// - completion: A closure that is called when the operation completes.
/// - success: `true` if the operation was successful; otherwise `false`.
/// - statusCode: The status code returned by the device. `nil` if the operation failed.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the
/// operation was successful.
- (void)setDeviceTime:(NSDate *_Nonnull)date
completion:(AIBudsStatusCodeCompletionHandler _Nullable)completion;
@endインスタンスメソッド
デバイスのシステム時刻を指定日時に設定します。
- Swift
- Objective-C
/// Sets the device's system time.
/// - Parameters:
/// - date: The target time to set on the device.
/// - completion: A closure that is called when the operation completes.
/// - success: `true` if the operation was successful; otherwise `false`.
/// - statusCode: The status code returned by the device. `nil` if the operation failed.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the operation was successful.
func setDeviceTime(
to date: Date,
completion: AIBudsStatusCodeCompletionHandler?
)/// Sets the device's system time.
/// - Parameters:
/// - date: The target time to set on the device.
/// - completion: A closure that is called when the operation completes.
/// - success: `true` if the operation was successful; otherwise `false`.
/// - statusCode: The status code returned by the device. `nil` if the operation failed.
/// - error: An `NSError` object that describes the error that occurred, or `nil` if the
/// operation was successful.
- (void)setDeviceTime:(NSDate *_Nonnull)date
completion:(AIBudsStatusCodeCompletionHandler _Nullable)completion;パラメータ
| パラメータ | 型 | 説明 |
|---|---|---|
date | Date / NSDate | デバイスに設定する日時。 |
completion | AIBudsStatusCodeCompletionHandler? | 処理完了時に呼び出される任意の完了ハンドラー。 |
コールバックのパラメータ:
| 名前 | 型 | 説明 |
|---|---|---|
success | Bool / BOOL | 処理が成功した場合は true、それ以外は false。 |
statusCode | NSNumber? | デバイスが返すステータスコード。SDK の仕様では、処理失敗時は nil です。 |
error | NSError? | 処理失敗時のエラー詳細。成功時は nil。 |
戻り値
このメソッドは値を直接返しません。結果は完了ハンドラーに渡されます。
使用例
- Swift
- Objective-C
import AIBuds
final class DeviceManager {
/// The connected device
weak var device: DeviceConvertible?
/// Sets the connected device to the supplied date
func setDeviceTime(to date: Date) {
guard let device = device as? DeviceInfoAPI else {
print("Device does not support setting the time")
return
}
device.setDeviceTime(to: date) { success, statusCode, error in
if !success {
print(
"Failed to set device time: " + (error?.localizedDescription ?? "Unknown error")
)
return
}
print(
"Device time set successfully. Status code: " + (statusCode?.stringValue ?? "N/A")
)
}
}
}#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>
@interface DeviceManager ()
/// The connected device
@property(weak, nonatomic) id<AIBudsDeviceConvertible> device;
@end
@implementation DeviceManager
- (void)setDeviceTime:(NSDate *)date {
id<AIBudsDeviceInfoAPI> device = (id<AIBudsDeviceInfoAPI>)self.device;
if (![device conformsToProtocol:@protocol(AIBudsDeviceInfoAPI)]) {
NSLog(@"Device does not support setting the time");
return;
}
[device
setDeviceTime:date
completion:^(BOOL success, NSNumber *_Nullable statusCode, NSError *_Nullable error) {
if (!success) {
NSLog(@"Failed to set device time: %@",
error.localizedDescription ?: @"Unknown error");
return;
}
NSLog(@"Device time set successfully. Status code: %@", statusCode ?: @"N/A");
}];
}
@endエラー処理
処理結果は完了ハンドラーに渡されます。
- 指定日時が反映されたと判断する前に
successを確認します。 successがfalseの場合は、errorから失敗の詳細を取得します。statusCodeがある場合は、診断やデバイス固有の処理に備えて保持します。- 対象デバイスについて文書化されていないエラーやステータスコードを前提にしないでください。
ベストプラクティス
-
指定日時を検証する: アプリが意図した日時を送信していることを確認します。
-
プロトコル対応を確認する: メソッドを呼び出す前に、デバイスが
DeviceInfoAPIに対応していることを確認します。 -
接続完了後に呼び出す: デバイスが接続済みで操作可能になってから時刻を設定します。
-
すべての完了値を処理する:
success、statusCode、errorを確認します。 -
メインキューで UI を更新する: 完了ハンドラーからの UIKit 更新はメインキューへ切り替えます。
注意事項
- 公開 API は
Dateを受け取りますが、UTC 変換の仕様は定めていません。対象デバイスで規定されていないタイムゾーンの前提を追加しないでください。 - デバイスを現在時刻に合わせるだけの場合は
syncDeviceTime(_:)を使用します。 - 指定日時の設定への対応は、デバイスモデルやファームウェアによって異なる場合があります。