SwiftUI onReceive 不适用于 UIPasteboard 发布者

问题描述

我想通过 onReceive 订阅 SwiftUI 中的 UIPasteboard 更改。 pHasstringsPublisher 不会在剪贴板中的某些内容发生变化时立即更新,我不明白为什么。

import SwiftUI

struct ContentView: View {
    let pasteboard = UIPasteboard.general
    
    @State var pString: String = "pString"
    @State var pHasstrings: Bool = false
    @State var pHasstringsPublisher: Bool = false

    var body: some View {
        vstack{
            Spacer()
            Text("b: '\(self.pString)'")
                .font(.headline)
            Text("b: '\(self.pHasstrings.description)'")
                .font(.headline)
            Text("p: '\(self.pHasstringsPublisher.description)'")
                .font(.headline)
            Spacer()
            Button(action: {
                self.pString = self.pasteboard.string ?? "nil"
                self.pHasstrings = self.pasteboard.hasstrings
            },label: {
                Text("read pb")
                    .font(.largeTitle)
            })
            Button(action: {
                self.pasteboard.items = []
            },label: {
                Text("clear pb")
                    .font(.largeTitle)
            })
            Button(action: {
                self.pasteboard.string = Date().description
            },label: {
                Text("set pb")
                    .font(.largeTitle)
            })
            
        }
        .onReceive(self.pasteboard
                    .publisher(for: \.hasstrings)
                    .print()
                    .receive(on: RunLoop.main)
                    .erasetoAnyPublisher(),perform:
                    { hasstrings in
                        print("pasteboard publisher")
                        self.pHasstringsPublisher = hasstrings
                    })
    }

}

解决方法

据我所知,UIPasteboard 的所有属性都没有记录为支持键值观察 (KVO),因此 publisher(for: \.hasStrings) 可能永远不会发布任何内容。

相反,您可以从默认 UIPasteboard.changedNotification 中监听 NotificationCenter。但是,如果您希望用户从另一个应用程序复制一个字符串,这仍然不够,因为如果在您的应用程序处于后台时更改了内容,粘贴板不会发布 changedNotification。所以你需要听UIApplication.didBecomeActiveNotification

让我们在 UIPasteboard 的扩展中将其全部包装起来:

extension UIPasteboard {
    var hasStringsPublisher: AnyPublisher<Bool,Never> {
        return Just(hasStrings)
            .merge(
                with: NotificationCenter.default
                    .publisher(for: UIPasteboard.changedNotification,object: self)
                    .map { _ in self.hasStrings })
            .merge(
                with: NotificationCenter.default
                    .publisher(for: UIApplication.didBecomeActiveNotification,object: nil)
                    .map { _ in self.hasStrings })
            .eraseToAnyPublisher()
    }
}

像这样使用它:

    var body: some View {
        VStack {
            blah blah blah
        }
        .onReceive(UIPasteboard.general.hasStringsPublisher) { hasStrings = $0 }
    }