我收到粘贴板内容更改通知后收到很多崩溃

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我收到粘贴板内容更改通知后收到很多崩溃相关的知识,希望对你有一定的参考价值。

我收到粘贴板内容更改通知后收到许多崩溃。我只是监视剪贴板的更改,并尝试识别文本内容。

这是我的代码:

        class PasteboardMonitor 

            init() 
                addPasteboardMonitor()
            

            func addPasteboardMonitor() 

                handleContentFromPasteBoard(conent: UIPasteboard.general.string, identifier: "\(UIPasteboard.general.changeCount)")

                NotificationCenter.default.addObserver(self, selector: #selector(didReceivedPasteboardNotification(noti:)), name: UIPasteboard.changedNotification, object: nil)
                NotificationCenter.default.addObserver(self, selector: #selector(didReceivedPasteboardNotification(noti:)), name: UIApplication.willEnterForegroundNotification, object: nil)
                NotificationCenter.default.addObserver(self, selector: #selector(didReceivedApplicationNotification), name: UIApplication.willResignActiveNotification, object: nil)
            

           func handleContentFromPasteBoard(conent: String?, identifier: String? = nil) 
                //handle content
                ...
           

           @objc func didReceivedPasteboardNotification(noti: Notification) 
                handleContentFromPasteBoard(conent: UIPasteboard.general.string, identifier: "\(UIPasteboard.general.changeCount)")
            

崩溃日志是:

  • 崩溃:com.apple.UIKit.pasteboard.cache-queue
  • 0 libsystem_pthread.dylib 0x1963b5a38 ___chkstk_darwin + 64
  • 1 libdispatch.dylib 0x19635f49c _dispatch_mach_send_and_wait_for_reply + 440
  • 2 libdispatch.dylib 0x19635f904 dispatch_mach_send_with_result_and_wait_for_reply + 56
  • 3 libxpc.dylib 0x19629f594 xpc_connection_send_message_with_reply_sync + 240
  • 4 Foundation 0x196ba51fc NSXPCCONNECTION_IS_WAITING_FOR_A_SYNCHRONOUS_REPLY + 16
  • 5 Foundation 0x19696b090-[NSXPCConnection _sendInvocation:orArguments:count:methodSignature:selector:withProxy:] + 2704
  • 6 Foundation 0x19696a40c-[NSXPCConnection _sendSelector:withProxy:arg1:] + 128
  • 7基础0x196bac2ac _NSXPCDistantObjectSimpleMessageSend1 + 40
  • 8粘贴板0x1ac476aac-[PBServerConnection localGeneralPasteboard:]
  • 9 UIKitCore 0x19a6c3428 _getTemporaryLocalGeneralPasteboard + 64
  • 10 UIKitCore 0x19a6c2444 _pasteboardCacheQueue_existingItemCollectionWithName + 152
  • 11 UIKitCore 0x19a6c2160 __59 + [_ UIConcretePasteboard _pasteboardNamed:createIfNotFound:] _ block_invoke + 236
  • 12 libdispatch.dylib 0x196349fd8 _dispatch_client_callout + 20
  • 13 libdispatch.dylib 0x1963567f4 _dispatch_lane_barrier_sync_invoke_and_complete + 60...
  • 218 UIKitCore 0x19a6c1fcc + [_ UIConcretePasteboard _pasteboardNamed:createIfNotFound:] + 396
  • 219 TovApp 0x1014e4a54 PasteboardMonitor.didReceivedPasteboardNotification(noti :) + 123(PasteboardMonitor.swift:123)
  • 220 TovApp 0x1014e4c4c @objc PasteboardMonitor.didReceivedPasteboardNotification(noti :) + 4351249484(:4351249484)
  • 221 Foundation 0x196a6effc __57- [NSNotificationCenter addObserver:selector:name:object:] _ block_invoke_2 + 28
  • 222 CoreFoundation 0x1965fc99c CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER + 28
  • 223 CoreFoundation 0x1965fc9ec ___CFXRegistrationPost1_block_invoke + 68
  • 224 CoreFoundation 0x1965fbce4 _CFXRegistrationPost1 + 396
  • 225 CoreFoundation 0x1965fb97c ___CFXNotificationPost_block_invoke + 108
  • 226 CoreFoundation 0x196574910-[_ CFXNotificationRegistrar找到:对象:观察者:枚举器:] + 1424
  • 227 CoreFoundation 0x1965fb2ac _CFXNotificationPost + 1268
  • 228 Foundation 0x19695db8c-[NSNotificationCenter postNotificationName:object:userInfo:] + 64
  • 229 UIKitCore 0x19a6c1cd4 _sendPasteboardChangedNotification + 96
  • 230 UIKitCore 0x19a6c1fcc + [_ UIConcretePasteboard _pasteboardNamed:createIfNotFound:] + 396
  • 231 TovApp 0x1014e4a54 PasteboardMonitor.didReceivedPasteboardNotification(noti :) + 123(PasteboardMonitor.swift:123)
  • 232 TovApp 0x1014e4c4c @objc PasteboardMonitor.didReceivedPasteboardNotification(noti :) + 4351249484(:4351249484)
  • 233 Foundation 0x196a6effc __57- [NSNotificationCenter addObserver:selector:name:object:] _ block_invoke_2 + 28
  • 234 CoreFoundation 0x1965fc99c CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER + 28
  • 235 CoreFoundation 0x1965fc9ec ___CFXRegistrationPost1_block_invoke + 68
  • 236 CoreFoundation 0x1965fbce4 _CFXRegistrationPost1 + 396
  • 237 CoreFoundation 0x1965fb97c ___CFXNotificationPost_block_invoke + 108
  • 238 CoreFoundation 0x196574910-[_ CFXNotificationRegistrar找到:对象:观察者:枚举器:] + 1424
  • 239 CoreFoundation 0x1965fb2ac _CFXNotificationPost + 1268
  • 240 Foundation 0x19695db8c-[NSNotificationCenter postNotificationName:object:userInfo:] + 64
  • 241 UIKitCore 0x19a6c1cd4 _sendPasteboardChangedNotification + 96
  • 242 UIKitCore 0x19a6c1fcc + [_ UIConcretePasteboard _pasteboardNamed:createIfNotFound:] + 396
  • 243 TovApp 0x1014e4a54 PasteboardMonitor.didReceivedPasteboardNotification(noti :) + 123(PasteboardMonitor.swift:123)
  • 244 TovApp 0x1014e4c4c @objc PasteboardMonitor.didReceivedPasteboardNotification(noti :) + 4351249484(:4351249484)
  • 245 Foundation 0x196a6effc __57- [NSNotificationCenter addObserver:selector:name:object:] _ block_invoke_2 + 28
  • 246 CoreFoundation 0x1965fc99c CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER + 28
  • 247 CoreFoundation 0x1965fc9ec ___CFXRegistrationPost1_block_invoke + 68
  • 248 CoreFoundation 0x1965fbce4 _CFXRegistrationPost1 + 396
  • 249 CoreFoundation 0x1965fb97c ___CFXNotificationPost_block_invoke + 108
  • 250 CoreFoundation 0x196574910-[_ CFXNotificationRegistrar查找:对象:观察者:枚举器:] + 1424
  • 251 CoreFoundation 0x1965fb2ac _CFXNotificationPost + 1268
  • 252 Foundation 0x19695db8c-[NSNotificationCenter postNotificationName:object:userInfo:] + 64
  • 253 UIKitCore 0x19a6c1cd4 _sendPasteboardChangedNotification + 96
  • 254 UIKitCore 0x19a6c1fcc + [_ UIConcretePasteboard _pasteboardNamed:createIfNotFound:] + 396
  • 255 TovApp 0x1014e4a54 PasteboardMonitor.didReceivedPasteboardNotification(noti :) + 123(PasteboardMonitor.swift:123)

有人知道为什么会这样吗?以及如何解决?谢谢。

答案

您与NotificationCenter相关的代码似乎有风险。

尝试:

  1. 确保仅添加一次观察者:
var observersAdded: Bool = false

func addPasteboardMonitor() 
    ...
    if !observersAdded 
        NotificationCenter.default.addObserver(self ....
        observersAdded = true
    
    ...

  1. 删除PasteboardMonitor的析构函数中的观察者:NotificationCenter.default.removeObserver

以上是关于我收到粘贴板内容更改通知后收到很多崩溃的主要内容,如果未能解决你的问题,请参考以下文章

WatchOS 3 在收到推送通知时崩溃

使用@react-native-firebase/messaging 收到通知后应用程序崩溃

Worklight 应用程序在收到推送通知时崩溃

为啥我的 iOS 应用程序在收到推送通知时会崩溃?

收到推送通知时 iOS 13 崩溃?

应用程序未运行时处理远程通知时崩溃