Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
75 lines
2.6 KiB
Swift
75 lines
2.6 KiB
Swift
import ExpoModulesCore
|
|
import UIKit
|
|
|
|
// Retain the latest continuation until JS is mounted, including cold launches.
|
|
private enum HandoffState {
|
|
static var activity: NSUserActivity?
|
|
static var pending: [String: String]?
|
|
static var notify: (() -> Void)?
|
|
|
|
// app.config.ts declares the one `.media` type per build profile; iOS and
|
|
// Catalyst must publish the same string for Handoff to pair them. Other
|
|
// plugins (expo-router) append their own types, so match by suffix.
|
|
static let activityType =
|
|
(Bundle.main.object(forInfoDictionaryKey: "NSUserActivityTypes") as! [String])
|
|
.first { $0.hasSuffix(".media") }!
|
|
}
|
|
|
|
// ExpoAppSceneDelegate forwards scene(_:continue:) and cold-start activities here.
|
|
public final class HandoffAppDelegateSubscriber: ExpoAppDelegateSubscriber {
|
|
public func application(
|
|
_ application: UIApplication,
|
|
continue userActivity: NSUserActivity,
|
|
restorationHandler: @escaping ([UIUserActivityRestoring]?) -> Void
|
|
) -> Bool {
|
|
guard userActivity.activityType == HandoffState.activityType,
|
|
let server = userActivity.userInfo?["server"] as? String,
|
|
let route = userActivity.userInfo?["route"] as? String
|
|
else { return false }
|
|
HandoffState.pending = ["server": server, "route": route]
|
|
HandoffState.notify?()
|
|
return true
|
|
}
|
|
}
|
|
|
|
public final class HandoffModule: Module {
|
|
public func definition() -> ModuleDefinition {
|
|
Name("Handoff")
|
|
Events("onActivity")
|
|
|
|
OnStartObserving {
|
|
HandoffState.notify = { [weak self] in self?.sendEvent("onActivity") }
|
|
if HandoffState.pending != nil { HandoffState.notify?() }
|
|
}
|
|
OnStopObserving { HandoffState.notify = nil }
|
|
|
|
AsyncFunction("takeActivity") { () -> [String: String]? in
|
|
defer { HandoffState.pending = nil }
|
|
return HandoffState.pending
|
|
}.runOnQueue(.main)
|
|
|
|
AsyncFunction("publish") { (server: String?, route: String?) in
|
|
HandoffState.activity?.invalidate()
|
|
HandoffState.activity = nil
|
|
guard let server, let route else { return }
|
|
let activity = NSUserActivity(activityType: HandoffState.activityType)
|
|
activity.title = "oTunes"
|
|
activity.userInfo = ["server": server, "route": route]
|
|
activity.requiredUserInfoKeys = ["server", "route"]
|
|
activity.isEligibleForHandoff = true
|
|
activity.isEligibleForSearch = false
|
|
activity.isEligibleForPublicIndexing = false
|
|
HandoffState.activity = activity
|
|
activity.becomeCurrent()
|
|
}.runOnQueue(.main)
|
|
|
|
OnDestroy {
|
|
DispatchQueue.main.async {
|
|
HandoffState.activity?.invalidate()
|
|
HandoffState.activity = nil
|
|
HandoffState.notify = nil
|
|
}
|
|
}
|
|
}
|
|
}
|