跳到主要内容

上报已找到 iPhone

响应由设备发起的查找 iPhone 请求,在应用中管理 iPhone 端提示,并在用户找到 iPhone 后通知请求设备。

该流程与查找设备方向相反:由已连接设备发起,宿主 App 负责播放声音、触发振动或显示 UI,帮助用户找到 iPhone。

前提条件

上报已找到 iPhone 前,请确保:

API 参考

框架

AIBuds.xcframework

导入

Swift
import AIBuds

协议

notifyPhoneFound 方法定义在 FindPhoneStateReportingAPI 中。

Swift
/// Reports to a connected device that its find-phone request has been resolved.
///
/// Find-phone is initiated by the device rather than by this API. Observe
/// `DeviceDelegate.deviceDidRequestStartFindingPhone(_:)` and
/// `DeviceDelegate.deviceDidRequestStopFindingPhone(_:)`, or the corresponding
/// `SDKDelegate` callbacks, to start and stop the app's phone-side alert.
///
/// After the user locates the phone, call `notifyPhoneFound(_:)` to notify the
/// requesting device. To make the connected device itself emit a locate
/// indication, use `DeviceFindAPI` instead.
public protocol FindPhoneStateReportingAPI: DeviceAPI {
    /// Notifies the connected device that the user has found the phone.
    ///
    /// Call this after handling a device-originated find-phone request and
    /// stopping the phone-side alert. The completion reports delivery and
    /// command processing; it does not represent a new find-phone request.
    /// - Parameters:
    ///   - completion: Called when command processing completes.
    ///     - success: `true` when the device accepted the report; otherwise,
    ///       `false`.
    ///     - error: The command or communication error, or `nil` on success.
    func notifyPhoneFound(_ completion: AIBudsCompletionHandler?)
}

实例方法

通知已连接设备用户已经找到 iPhone。

Swift
/// Notifies the connected device that the user has found the phone.
///
/// Call this after handling a device-originated find-phone request and
/// stopping the phone-side alert. The completion reports delivery and
/// command processing; it does not represent a new find-phone request.
/// - Parameters:
///   - completion: Called when command processing completes.
///     - success: `true` when the device accepted the report; otherwise,
///       `false`.
///     - error: The command or communication error, or `nil` on success.
func notifyPhoneFound(_ completion: AIBudsCompletionHandler?)

请在 API Reference 中查看 notifyPhoneFound

参数

参数类型说明
completionAIBudsCompletionHandler?上报传输和指令处理完成时调用的可选 Handler。

回调参数:

名称类型说明
successBool / BOOL设备接受已找到 iPhone 上报时为 true,否则为 false
errorNSError?指令或通信错误;成功时为 nil

返回值

该方法没有直接返回值。完成回调用于报告解决结果是否传递给请求设备,不会发起新的查找 iPhone 请求。

使用示例

监听设备发起的开始和停止请求。用户确认已经找到 iPhone 时,应先停止 iPhone 端提示,再上报解决结果。

Swift
import AIBuds

final class FindPhoneCoordinator: NSObject, DeviceDelegate {
    private weak var requestingDevice: DeviceConvertible?

    func deviceDidRequestStartFindingPhone(_ device: DeviceConvertible) {
        requestingDevice = device
        DispatchQueue.main.async {
            self.startPhoneAlert()
        }
    }

    func deviceDidRequestStopFindingPhone(_ device: DeviceConvertible) {
        DispatchQueue.main.async {
            self.stopPhoneAlert()
        }
    }

    func userConfirmedPhoneFound() {
        stopPhoneAlert()

        guard let reporter = requestingDevice as? FindPhoneStateReportingAPI else {
            print("Device cannot receive a phone-found report")
            return
        }

        reporter.notifyPhoneFound { success, error in
            guard success else {
                print(
                    "Failed to report phone found: \(error?.localizedDescription ?? "Unknown error")"
                )
                return
            }
            print("Phone-found report accepted")
        }
    }
}

错误处理

保持 iPhone 端提示状态与上报传输相互独立。当设备请求停止或用户确认找到 iPhone 时立即停止提示;如果 notifyPhoneFound 发生通信错误,应显示错误,但不要自动重新启动提示。

最佳实践

  1. 响应设备请求:仅在收到对应 Delegate 回调后启动 iPhone 端提示。
  2. 实现 iPhone 端提示:由宿主 App 负责声音、振动、权限、后台行为和 UI。
  3. 先停止再上报:调用 notifyPhoneFound(_:) 前停止本地提示资源。
  4. 保留请求设备:将上报发送给发起请求的同一已连接设备。
  5. 在主队列更新 UI:将 Delegate 和完成回调中的 UIKit 操作派发到主队列。

注意事项

  • notifyPhoneFound(_:) 用于确认请求已经解决,不会启动查找 iPhone 操作。
  • 设备发起的停止请求表示应用应停止 iPhone 端提示;它与用户确认找到 iPhone 的上报相互独立。
  • 当应用需要已连接设备自身发出定位提示时,请使用 DeviceFindAPI