डिवाइस का समय सेट करें
जुड़े डिवाइस की internal clock को ऐप द्वारा दी गई तारीख और समय पर set करें।
पहले की आवश्यकताएँ
डिवाइस का समय set करने से पहले सुनिश्चित करें:
- डिवाइस connected और stable state में है
- डिवाइस
DeviceInfoAPIprotocol का समर्थन करता है - लक्ष्य
Dateवही समय दिखाता है जिसे ऐप भेजना चाहता है
API संदर्भ
फ्रेमवर्क
AIBuds.xcframework
Import
SDK उपयोग करने वाली files में main framework import करें:
- Swift
- Objective-C
import AIBuds#import <AIBuds/AIBuds-Swift.h>
#import <AIBuds/AIBuds.h>प्रोटोकॉल
setDeviceTime method DeviceInfoAPI में परिभाषित है। यह protocol base device API protocol से inherit करता है।
- 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;
@endInstance method
डिवाइस का system time दी गई तारीख पर set करता है।
- 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 | डिवाइस पर set किया जाने वाला समय। |
completion | AIBudsStatusCodeCompletionHandler? | Operation पूरा होने पर call होने वाला optional completion handler। |
Callback पैरामीटर:
| नाम | प्रकार | विवरण |
|---|---|---|
success | Bool / BOOL | Operation सफल हो तो true, अन्यथा false। |
statusCode | NSNumber? | डिवाइस का status code। SDK के अनुसार operation असफल होने पर यह nil होता है। |
error | NSError? | Operation असफल हो तो error details, अन्यथा nil। |
Return value
यह method सीधे कोई value return नहीं करता। परिणाम completion handler से मिलता है।
उपयोग के उदाहरण
- 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");
}];
}
@endError handling
Completion handler operation का परिणाम देता है:
- लक्ष्य समय लागू मानने से पहले
successजाँचें। successfalseहो तो failure details के लिएerrorउपयोग करें।- उपलब्ध होने पर diagnostics या device-specific handling के लिए
statusCodeरखें। - लक्ष्य डिवाइस के लिए documented न होने वाला error या status code न मानें।
बेहतर तरीके
-
लक्ष्य तारीख जाँचें: सुनिश्चित करें कि ऐप इच्छित तारीख और समय भेज रहा है।
-
Protocol conformance जाँचें: Method call करने से पहले
DeviceInfoAPIsupport की पुष्टि करें। -
Connection के बाद call करें: डिवाइस connected और ready होने के बाद ही समय set करें।
-
सभी completion values संभालें:
success,statusCodeऔरerrorदेखें। -
UI main queue पर अपडेट करें: Completion से आने वाले UIKit updates main queue पर भेजें।
ध्यान देने योग्य बातें
- Public API
Dateस्वीकार करता है, पर UTC conversion contract नहीं बताता। लक्ष्य डिवाइस द्वारा न बताई timezone assumptions न जोड़ें। - डिवाइस को केवल मौजूदा समय से synchronize करना हो तो
syncDeviceTime(_:)उपयोग करें। - लक्ष्य समय set करने का support model और firmware के अनुसार बदल सकता है।