Ana içeriğe geç

Aygıt Saatini Ayarlama

Bağlı aygıtın dahili saatini uygulamanızın sağladığı belirli tarih ve saate ayarlayın.

Ön Koşullar

Aygıt saatini ayarlamadan önce şunları doğrulayın:

  • Aygıt bağlı ve kararlı durumdadır
  • Aygıt DeviceInfoAPI protokolünü destekler
  • Hedef Date, uygulamanızın göndermek istediği zamanı temsil eder

API Reference

Framework

AIBuds.xcframework

Import

SDK'yı kullanacağınız dosyalarda ana framework'ü içe aktarın:

Swift
import AIBuds

Protokol

setDeviceTime yöntemi DeviceInfoAPI içinde tanımlıdır. Bu protokol temel aygıt API protokolünden türetilir.

Swift
/// 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?
    )
}

Örnek Yöntemi

Aygıtın sistem saatini verilen tarihe ayarlar.

Swift
/// 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?
)

Parametreler

ParametreTürAçıklama
dateDate / NSDateAygıtta ayarlanacak hedef zaman.
completionAIBudsStatusCodeCompletionHandler?İşlem tamamlandığında çağrılan isteğe bağlı tamamlanma işleyicisi.

Geri Çağrı Parametreleri:

AdTürAçıklama
successBool / BOOLİşlem başarılıysa true; aksi halde false.
statusCodeNSNumber?Aygıtın döndürdüğü durum kodu. SDK, işlem başarısız olduğunda bu değerin nil olduğunu belirtir.
errorNSError?İşlem başarısızsa hata ayrıntıları; aksi halde nil.

Dönüş Değeri

Bu yöntem doğrudan bir değer döndürmez. Sonuç tamamlanma işleyicisi aracılığıyla iletilir.

Kullanım Örnekleri

Swift
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")
            )
        }
    }
}

Hata Yönetimi

Tamamlanma işleyicisi işlemin sonucunu bildirir:

  1. Hedef zamanı uygulanmış kabul etmeden önce success değerini kontrol edin.
  2. success değeri false ise hata ayrıntıları için error değerini kullanın.
  3. Varsa tanılama veya aygıta özgü işleme için statusCode değerini koruyun.
  4. Hedef aygıt için belgelenmedikçe belirli bir hata veya durum kodu varsaymayın.

En İyi Uygulamalar

  1. Hedef Tarihi Doğrulayın: Uygulamanın amaçlanan tarih ve saati gönderdiğinden emin olun.

  2. Protokol Uyumluluğunu Denetleyin: Yöntemi çağırmadan önce aygıtın DeviceInfoAPI desteğini doğrulayın.

  3. Bağlantıdan Sonra Çağırın: Saati yalnızca aygıt bağlanıp hazır olduktan sonra ayarlayın.

  4. Tüm Tamamlanma Değerlerini İşleyin: success, statusCode ve error değerlerini değerlendirin.

  5. Arayüzü Ana Kuyrukta Güncelleyin: Tamamlanma geri çağrısına bağlı UIKit güncellemelerini ana kuyruğa aktarın.

Notlar

  • Genel API bir Date kabul eder ancak UTC dönüşüm sözleşmesi tanımlamaz. Hedef aygıtın belirtmediği saat dilimi varsayımları eklemeyin.
  • Aygıtın yalnızca geçerli saatle eşitlenmesi gerekiyorsa syncDeviceTime(_:) kullanın.
  • Hedef zaman ayarlama desteği aygıt modeline ve firmware sürümüne göre değişebilir.