summaryrefslogtreecommitdiff
path: root/Shared/DomainDigShareInbox.swift
blob: 3a577e7c5daa4e1f0dcbce52991250d7aeeb4a1a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import Foundation

/// App Group handoff from the share extension to the app.
///
/// Share extensions cannot reliably open their host app, so the extension
/// drops the shared domain here and the app consumes it the next time it
/// becomes active, routing it into an inspection.
enum DomainDigShareInbox {
    private static let key = "pendingInspectDomain"

    private static var defaults: UserDefaults? {
        UserDefaults(suiteName: DomainDigWidgetStore.appGroupID)
    }

    static func write(domain: String) {
        defaults?.set(domain, forKey: key)
    }

    /// Returns and clears the pending domain, if any.
    static func consume() -> String? {
        guard let defaults,
              let domain = defaults.string(forKey: key),
              !domain.isEmpty
        else { return nil }
        defaults.removeObject(forKey: key)
        return domain
    }
}