Skip to main content

Factory Reset

The factory reset operation restores the device to its original factory settings, erasing all user data and custom configurations. This operation is useful when preparing a device for resale or troubleshooting persistent issues.

Prerequisites

Before performing a factory reset, ensure:

  • The device is connected and in a stable state
  • All important data has been backed up
  • The user understands that all personal data will be erased

Implement with AI Assistance

Build with AI

Implement this workflow with AI

Use the official Implement AIBuds Factory Reset skill to adapt this workflow to your app.

Read and follow https://docs-aibuds.github.io/skills/implement-aibuds-factory-reset. Use it to implement Implement AIBuds Factory Reset in this iOS project and verify the result.
View official skill

API Reference

Framework

AIBuds.xcframework

Import

In the files where you want to use the SDK, import the main header:

Swift
import AIBuds

Protocol

The factoryReset method is defined in the following protocol. The protocol inherits from the base device API protocol.

Swift
/// Defines common device operations including factory reset
protocol DeviceCommonAPI: DeviceAPI {
    /// Factory reset
    /// - Parameters:
    ///   - completion: Completion callback that returns the operation result
    ///     - 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 factoryReset(_ completion: AIBudsCompletionHandler?)
}

Instance Method

Restores the device to its original factory settings, erasing all user data and custom configurations.

iOS 13.0+

Swift
/// Factory reset
/// - Parameters:
///   - completion: Completion callback that returns the operation result
///     - 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 factoryReset(_ completion: AIBudsCompletionHandler?)

Parameters

ParameterTypeDescription
completionAIBudsCompletionHandler?Optional completion callback that is called when the operation completes.

Callback Parameters:

NameTypeDescription
successBooltrue if the operation succeeded, false otherwise.
errorNSError?Contains error information if the operation failed, nil otherwise.

Return Value

This method does not return a value directly. The result is provided through the completion callback.

Usage Examples

Swift
import AIBuds

class DeviceManager {

    /// The connected device
    weak var device: DeviceConvertible?

    /// Performs factory reset on the connected device
    func performFactoryReset() {
        // Ensure the device supports factory reset protocol
        guard let device = device as? DeviceCommonAPI else {
            print("Device does not support factory reset")
            return
        }

        // Execute factory reset with completion handler
        device.factoryReset { [weak self] success, error in
            // Handle failure case
            if !success {
                let errorMessage = {
                    if let error = error {
                        return "\(error)"
                    }
                    return "Unknown error"
                }()
                print("Factory reset failed: \(errorMessage)")
                return
            }
            // Handle success case
            print("Factory reset completed successfully")
        }
    }
}

Error Handling

The completion handler may return the following error types:

Error Domain: AIBudsSDK.ErrorDomain

Error CodeDescriptionRecovery Suggestion
.deviceNotConnectedDevice is not connectedEnsure device is paired and connected
.bleCommandExecFailedDueToTimeoutOperation timed outRetry the operation
.deviceBusyDevice is busy with another operationWait for ongoing operations to complete
.deviceNotSupportFactory reset is not supported on this deviceCheck device capabilities before calling

Best Practices

  1. Confirm with User: Always display a confirmation dialog before initiating a factory reset, as this operation is irreversible.

  2. Handle Background Execution: Wrap the completion handler in a DispatchQueue.main.async block when updating UI.

  3. Weak Self Reference: Use [weak self] in the completion handler to prevent retain cycles.

  4. Check Protocol Conformance: Verify the device conforms to DeviceCommonAPI protocol before calling the method.

  5. Clean Up References: After a successful factory reset, you may need to re-pair the device.

Notes

  • Factory reset may take several seconds to complete
  • The device will disconnect and reset during this operation
  • All user data including paired devices, settings, and stored media will be erased
  • The device will restart automatically after the reset completes