capacitor-community / privacy-screen

⚡️ Capacitor plugin that protects your app from displaying a screenshot in Recents screen/App Switcher.
MIT License
86 stars 23 forks source link

bug: Application tried to present modally a view controller <UIViewController: 0x10392e710> that is already being presented by <Capacitor.CAPBridgeViewController: 0x10390bf60> #102

Open yogo10803 opened 3 months ago

yogo10803 commented 3 months ago

Plugin version:

5.1.2

Platform(s):

iOS

Current behavior:

App crashed after pulling down the notification bar from the top left corner of the phone.

Steps to reproduce:

Pull down the notification bar from the top left corner of the phone in the app page which enabled privacy screen. Maybe the issue is same as https://github.com/capacitor-community/privacy-screen/issues/83#issue-2002693363

Related code:

@objc public func handleWillResignActiveNotification() {
        guard self.isEnabled else {
            return
        }
        DispatchQueue.main.async {
            let window = UIApplication.shared.connectedScenes
                .filter({$0.activationState == .foregroundActive || $0.activationState == .foregroundInactive})
                .compactMap({$0 as? UIWindowScene}).first?.windows.filter({$0.isKeyWindow}).first
            if var rootVC = window?.rootViewController {
                while let topVC = rootVC.presentedViewController {
                    rootVC = topVC
                }
                if rootVC.presentedViewController != self.privacyViewController {
                    rootVC.present(self.privacyViewController, animated: false, completion: nil)
                }
            } else {
                if self.plugin.bridge?.viewController?.presentedViewController != self.privacyViewController {
                    self.plugin.bridge?.viewController?.present(self.privacyViewController, animated: false, completion: nil)
                }
            }
        }
    }

The both version below are my adjustment, which solve the issue. Please check which adjustment is more appropriate and update it to the new version. Thanks a lot !!!

@objc public func handleWillResignActiveNotification() {
        guard self.isEnabled else {
            return
        }
        DispatchQueue.main.async {
            // ***  here
            guard self.privacyViewController.isBeingPresented else {
                return
            }
            let window = UIApplication.shared.connectedScenes
                .filter({$0.activationState == .foregroundActive || $0.activationState == .foregroundInactive})
                .compactMap({$0 as? UIWindowScene}).first?.windows.filter({$0.isKeyWindow}).first
            if var rootVC = window?.rootViewController {
                while let topVC = rootVC.presentedViewController {
                    rootVC = topVC
                }
                if rootVC.presentedViewController != self.privacyViewController {
                    rootVC.present(self.privacyViewController, animated: false, completion: nil)
                }
            } else {
                if self.plugin.bridge?.viewController?.presentedViewController != self.privacyViewController {
                    self.plugin.bridge?.viewController?.present(self.privacyViewController, animated: false, completion: nil)
                }
            }
        }
    }
@objc public func handleWillResignActiveNotification() {
        guard self.isEnabled else {
            return
        }
        DispatchQueue.main.async {
            let window = UIApplication.shared.connectedScenes
                .filter({$0.activationState == .foregroundActive || $0.activationState == .foregroundInactive})
                .compactMap({$0 as? UIWindowScene}).first?.windows.filter({$0.isKeyWindow}).first
            // *** here, add !self.privacyViewController.isBeingPresented
            if var rootVC = window?.rootViewController, !self.privacyViewController.isBeingPresented {
                while let topVC = rootVC.presentedViewController {
                    rootVC = topVC
                }
                if rootVC.presentedViewController != self.privacyViewController {
                    rootVC.present(self.privacyViewController, animated: false, completion: nil)
                }
            } else {
                if self.plugin.bridge?.viewController?.presentedViewController != self.privacyViewController {
                    self.plugin.bridge?.viewController?.present(self.privacyViewController, animated: false, completion: nil)
                }
            }
        }
    }
micbis commented 3 months ago

It seems like all the fixes so far didn't help. I can confirm the crashes are not fully gone.

nomadminded commented 2 months ago

I can confirm that I still experience random app crashes with version 5.2.1 and see same error in the stack trace (Application tried to present modally a view controller.. in handleWillResignActiveNotification). I'll try to post more info when I have other evidence.

yogo10803 commented 1 month ago

Unfortunately, Firebase Crashlytics shows that this issue still exists. Don't know what exact behavior leads to this issue. Stack trace as below, hope somebody can help QAQ

Using plugin version: 5.2.1

# Crashlytics - Stack trace
# Application: XXX
# Platform: apple
# Version: XXXX
# Issue: 4732187d8b157b66c2daa4ab75c8fe6f
# Session: 8dd115fecf234140974d7e2b1f261cfb_DNE_0_v2
# Date: Sat Oct 05 2024 12:26:28 GMT+0800 (台北標準時間)

Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                     0x940c __exceptionPreprocess
1  libobjc.A.dylib                    0x15c28 objc_exception_throw
2  UIKitCore                          0x2fcc4c (缺少 UUID 1242978a2c2c37818d6c9777edce2804)
3  UIKitCore                          0x36c870 (缺少 UUID 1242978a2c2c37818d6c9777edce2804)
4  UIKitCore                          0x2e3e4 (缺少 UUID 1242978a2c2c37818d6c9777edce2804)
5  UIKitCore                          0x2fbb90 (缺少 UUID 1242978a2c2c37818d6c9777edce2804)
6  UIKitCore                          0x2dadf8 (缺少 UUID 1242978a2c2c37818d6c9777edce2804)
7  UIKitCore                          0xc6418 (缺少 UUID 1242978a2c2c37818d6c9777edce2804)
8  UIKitCore                          0xc6290 (缺少 UUID 1242978a2c2c37818d6c9777edce2804)
9  CapacitorCommunityPrivacyScreen    0x59d0 (缺少 UUID 5f671a965c973084a707a4313ef15173)
10 CapacitorCommunityPrivacyScreen    0x4cf0 (缺少 UUID 5f671a965c973084a707a4313ef15173)
11 libdispatch.dylib                  0x637a8 _dispatch_call_block_and_release
12 libdispatch.dylib                  0x64780 _dispatch_client_callout
13 libdispatch.dylib                  0x45e10 _dispatch_main_queue_drain
14 libdispatch.dylib                  0x45a88 _dispatch_main_queue_callback_4CF$VARIANT$armv81
15 CoreFoundation                     0x919ac __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
16 CoreFoundation                     0x75648 __CFRunLoopRun
17 CoreFoundation                     0x79d20 CFRunLoopRunSpecific
18 GraphicsServices                   0x1998 GSEventRunModal
19 UIKitCore                          0x37134c (缺少 UUID 1242978a2c2c37818d6c9777edce2804)
20 UIKitCore                          0x370fc4 (缺少 UUID 1242978a2c2c37818d6c9777edce2804)
21 App                                0x7718 main + 7 (AppDelegate.swift:7)
22 ???                                0x1ab680344 (缺少)

Crashed: com.google.firebase.crashlytics.ios.exception
0  App                            0xc2cf0 FIRCLSProcessRecordAllThreads + 392 (FIRCLSProcess.c:392)
1  App                            0xc30d0 FIRCLSProcessRecordAllThreads + 423 (FIRCLSProcess.c:423)
2  App                            0xd1164 FIRCLSHandler + 34 (FIRCLSHandler.m:34)
3  App                            0xd0f68 __FIRCLSExceptionRecord_block_invoke + 240 (FIRCLSException.mm:240)
4  libdispatch.dylib              0x64780 _dispatch_client_callout + 16
5  libdispatch.dylib              0x46a50 _dispatch_lane_barrier_sync_invoke_and_complete + 52
6  App                            0xcfee4 FIRCLSExceptionRecord + 242 (FIRCLSException.mm:242)
7  App                            0xd0a2c FIRCLSExceptionRecordNSException + 126 (FIRCLSException.mm:126)
8  App                            0xcfaec FIRCLSTerminateHandler() + 407 (FIRCLSException.mm:407)
9  libc++abi.dylib                0x13424 std::__terminate(void (*)()) + 12
10 libc++abi.dylib                0x133d4 std::terminate() + 52
11 libobjc.A.dylib                0x2ae80 objc::DenseMapBase<objc::DenseMap<objc_class*, PendingInitialize*, objc::DenseMapValueInfo<PendingInitialize*>, objc::DenseMapInfo<objc_class*>, objc::detail::DenseMapPair<objc_class*, PendingInitialize*>>, objc_class*, PendingInitialize*, objc::DenseMapValueInfo<PendingInitialize*>, objc::DenseMapInfo<objc_class*>, objc::detail::DenseMapPair<objc_class*, PendingInitialize*>>::FatalCorruptHashTables(objc::detail::DenseMapPair<objc_class*, PendingInitialize*> const*, unsigned int) const + 10
12 libdispatch.dylib              0x64794 _dispatch_client_callout + 36
13 libdispatch.dylib              0x45e10 _dispatch_main_queue_drain + 888
14 libdispatch.dylib              0x45a88 _dispatch_main_queue_callback_4CF$VARIANT$armv81 + 36
15 CoreFoundation                 0x919ac __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 12
16 CoreFoundation                 0x75648 __CFRunLoopRun + 2080
17 CoreFoundation                 0x79d20 CFRunLoopRunSpecific + 584
18 GraphicsServices               0x1998 GSEventRunModal + 160
19 UIKitCore                      0x37134c <redacted> + 868
20 UIKitCore                      0x370fc4 UIApplicationMain + 312
21 App                            0x7718 main + 7 (AppDelegate.swift:7)
22 ???                            0x1ab680344 (缺少)

com.apple.uikit.eventfetch-thread
0  libsystem_kernel.dylib         0x1030 mach_msg2_trap + 8
1  libsystem_kernel.dylib         0x12b18 mach_msg2_internal + 76
2  libsystem_kernel.dylib         0x12db8 mach_msg_overwrite + 484
3  libsystem_kernel.dylib         0x1524 mach_msg + 20
4  CoreFoundation                 0x74148 __CFRunLoopServiceMachPort + 156
5  CoreFoundation                 0x752e0 __CFRunLoopRun + 1208
6  CoreFoundation                 0x79d20 CFRunLoopRunSpecific + 584
7  Foundation                     0x3eef8 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 208
8  Foundation                     0x3edf4 -[NSRunLoop(NSRunLoop) runUntilDate:] + 60
9  UIKitCore                      0x493818 <redacted> + 404
10 Foundation                     0x56a5c __NSThread__start__ + 704
11 libsystem_pthread.dylib        0x30ec _pthread_start + 116
12 libsystem_pthread.dylib        0x172c thread_start + 8

com.google.firebase.crashlytics.MachExceptionServer
0  libsystem_kernel.dylib         0x1030 mach_msg2_trap + 8
1  libsystem_kernel.dylib         0x12b18 mach_msg2_internal + 76
2  libsystem_kernel.dylib         0x12db8 mach_msg_overwrite + 484
3  libsystem_kernel.dylib         0x1524 mach_msg + 20
4  App                            0xd1718 FIRCLSMachExceptionServer + 192 (FIRCLSMachException.c:192)
5  libsystem_pthread.dylib        0x30ec _pthread_start + 116
6  libsystem_pthread.dylib        0x172c thread_start + 8

JavaScriptCore libpas scavenger
0  libsystem_kernel.dylib         0x187c __psynch_cvwait + 8
1  libsystem_pthread.dylib        0x260c _pthread_cond_wait$VARIANT$armv81 + 1220
2  JavaScriptCore                 0x10a53c <redacted> + 1380
3  libsystem_pthread.dylib        0x30ec _pthread_start + 116
4  libsystem_pthread.dylib        0x172c thread_start + 8

com.apple.NSURLConnectionLoader
0  libsystem_kernel.dylib         0x1030 mach_msg2_trap + 8
1  libsystem_kernel.dylib         0x12b18 mach_msg2_internal + 76
2  libsystem_kernel.dylib         0x12db8 mach_msg_overwrite + 484
3  libsystem_kernel.dylib         0x1524 mach_msg + 20
4  CoreFoundation                 0x74148 __CFRunLoopServiceMachPort + 156
5  CoreFoundation                 0x752e0 __CFRunLoopRun + 1208
6  CoreFoundation                 0x79d20 CFRunLoopRunSpecific + 584
7  CFNetwork                      0x229510 _CFURLStorageSessionDisableCache + 50860
8  Foundation                     0x56a5c __NSThread__start__ + 704
9  libsystem_pthread.dylib        0x30ec _pthread_start + 116
10 libsystem_pthread.dylib        0x172c thread_start + 8

com.google.fira.worker
0  Foundation                     0xa4544 _NSXPCSerializationIterateArrayObject + 248
1  Foundation                     0x204c0 -[NSXPCDecoder _decodeArrayOfObjectsForKey:] + 212
2  Foundation                     0x1fd60 -[NSDictionary(NSDictionary) initWithCoder:] + 148
3  Foundation                     0x555230 _decodeObject + 1172
4  Foundation                     0x5555dc _decodeObjectAfterSettingAllowListForKey + 164
5  Foundation                     0x48d80 -[NSXPCDecoder decodeObjectOfClasses:forKey:] + 48
6  CoreServices                   0x194c8 -[NSCoder(LaunchServicesAdditions) ls_decodeObjectOfClasses:forKey:] + 148
7  CoreServices                   0x452fc -[NSCoder(LaunchServicesAdditions) ls_decodeDictionaryWithKeysOfClass:valuesOfClasses:forKey:] + 156
8  CoreServices                   0x481f4 -[LSRecord initWithCoder:] + 132
9  CoreServices                   0x53754 -[LSBundleRecord initWithCoder:] + 80
10 CoreServices                   0x48b54 -[LSApplicationRecord initWithCoder:] + 72
11 Foundation                     0x555230 _decodeObject + 1172
12 Foundation                     0x5555dc _decodeObjectAfterSettingAllowListForKey + 164
13 Foundation                     0x48d80 -[NSXPCDecoder decodeObjectOfClasses:forKey:] + 48
14 CoreServices                   0x194c8 -[NSCoder(LaunchServicesAdditions) ls_decodeObjectOfClasses:forKey:] + 148
15 CoreServices                   0x1940c -[NSCoder(LaunchServicesAdditions) ls_decodeObjectOfClass:forKey:] + 84
16 CoreServices                   0x67308 -[LSApplicationProxy initWithCoder:] + 100
17 Foundation                     0x555230 _decodeObject + 1172
18 Foundation                     0x5fdcc -[NSXPCDecoder _decodeObjectOfClasses:atObject:] + 144
19 Foundation                     0x5f85c _NSXPCSerializationDecodeTypedObjCValuesFromArray + 872
20 Foundation                     0xa361c _NSXPCSerializationDecodeInvocationArgumentArray + 400
21 Foundation                     0x555968 -[NSXPCDecoder __decodeXPCObject:allowingSimpleMessageSend:outInvocation:outArguments:outArgumentsMaxCount:outMethodSignature:outSelector:isReply:replySelector:] + 696
22 Foundation                     0x555684 -[NSXPCDecoder _decodeReplyFromXPCObject:forSelector:] + 72
23 Foundation                     0x6ca08 -[NSXPCConnection _decodeAndInvokeReplyBlockWithEvent:sequence:replyInfo:] + 156
24 Foundation                     0x550d8c __88-[NSXPCConnection _sendInvocation:orArguments:count:methodSignature:selector:withProxy:]_block_invoke_5 + 184
25 Foundation                     0x33cec -[NSXPCConnection _sendInvocation:orArguments:count:methodSignature:selector:withProxy:] + 2084
26 Foundation                     0x326e0 -[NSXPCConnection _sendSelector:withProxy:arg1:] + 112
27 Foundation                     0x3261c _NSXPCDistantObjectSimpleMessageSend1 + 56
28 CoreServices                   0xad10 +[LSBundleProxy bundleProxyForCurrentProcess] + 340
29 Foundation                     0x44a188 -[NSBundle appStoreReceiptURL] + 80
30 App                            0x104d08 +[APMAEU appStoreRURL] + 4301720840
31 App                            0x104f94 +[APMAEU isASRS] + 4301721492
32 App                            0x104d84 +[APMAEU isFAS] + 4301720964
33 App                            0x151040 -[APMMeasurement(Event) createRawEventMetadataWithUserAttributes:] + 4302032960
34 App                            0x153168 -[APMMeasurement(Event) writeEvent:isPublicEvent:isRealtime:] + 4302041448
35 App                            0x150f1c __57-[APMMeasurement(Event) writeFilteredEventOnWorkerQueue:]_block_invoke + 4302032668
36 App                            0x1a09a0 -[APMSqliteStore performTransactionWithError:block:] + 4302358944
37 App                            0x11822c -[APMDatabase performTransaction:] + 4301799980
38 App                            0x150c70 -[APMMeasurement(Event) writeFilteredEventOnWorkerQueue:] + 4302031984
39 App                            0x1509d8 -[APMMeasurement(Event) writeEventOnWorkerQueue:] + 4302031320
40 App                            0x1505b0 -[APMMeasurement(Event) handleEventOnWorkerQueue:] + 4302030256
41 App                            0x144d90 -[APMMeasurement logEventOnWorkerQueue:notifyEventListeners:] + 4301983120
42 App                            0x144c1c -[APMMeasurement logEventOnWorkerQueueWithOrigin:isPublicEvent:name:parameters:timestamp:enabled:ignoreEnabled:ignoreInterceptor:interceptor:addedScreenParameters:] + 4301982748
43 App                            0x144a4c __151-[APMMeasurement logEventWithOrigin:isPublicEvent:name:parameters:timestamp:enabled:ignoreEnabled:ignoreInterceptor:interceptor:addedScreenParameters:]_block_invoke + 4301982284
44 App                            0x197aac __51-[APMScheduler scheduleOnWorkerQueueBlockID:block:]_block_invoke + 4302322348
45 libdispatch.dylib              0x637a8 _dispatch_call_block_and_release + 24
46 libdispatch.dylib              0x64780 _dispatch_client_callout + 16
47 libdispatch.dylib              0x3f6fc _dispatch_lane_serial_drain$VARIANT$armv81 + 600
48 libdispatch.dylib              0x401b0 _dispatch_lane_invoke$VARIANT$armv81 + 380
49 libdispatch.dylib              0x49f14 _dispatch_workloop_worker_thread + 608
50 libsystem_pthread.dylib        0x1bd0 _pthread_wqthread + 284
51 libsystem_pthread.dylib        0x1720 start_wqthread + 8

Thread
0  libsystem_kernel.dylib         0x14f0 __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x1c1c _pthread_wqthread + 360
2  libsystem_pthread.dylib        0x1720 start_wqthread + 8

Thread
0  libsystem_kernel.dylib         0x14f0 __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x1c1c _pthread_wqthread + 360
2  libsystem_pthread.dylib        0x1720 start_wqthread + 8

Thread
0  libsystem_kernel.dylib         0x14f0 __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x1c1c _pthread_wqthread + 360
2  libsystem_pthread.dylib        0x1720 start_wqthread + 8

Thread
0  libsystem_kernel.dylib         0x14f0 __workq_kernreturn + 8
1  libsystem_pthread.dylib        0x1c1c _pthread_wqthread + 360
2  libsystem_pthread.dylib        0x1720 start_wqthread + 8
yogo10803 commented 1 month ago

Crash in UIViewController presentation possibly related to CapacitorCommunityPrivacyScreen

Hello, I'm experiencing a crash in my app that seems to be related to the CapacitorCommunityPrivacyScreen plugin. Here's an analysis of the crash log and some information that might help diagnose the issue:

Error Type: The main cause of the crash is an NSInvalidArgumentException, which typically indicates that an invalid argument was passed to a method. Crash Location: The crash occurs in UIKit's presentViewController method, suggesting the issue is related to view controller presentation. Relevant Stack Trace: 9 CapacitorCommunityPrivacyScreen 0x59d0 (Missing UUID 5f671a965c973084a707a4313ef15173) 10 CapacitorCommunityPrivacyScreen 0x4cf0 (Missing UUID 5f671a965c973084a707a4313ef15173) 11 libdispatch.dylib 0x213c _dispatch_call_block_and_release

Possible Causes:

Attempting to present a nil or invalid view controller Calling presentViewController on the wrong thread View controller lifecycle issues (e.g., trying to present another view controller immediately in viewDidLoad)

Questions and Requests: a. Are there any known issues with the CapacitorCommunityPrivacyScreen plugin related to view controller presentation? b. Does the plugin have any specific threading requirements for its operations? c. Are there any best practices or common pitfalls when using this plugin that could lead to this type of crash? d. Could you provide any guidance on how to properly implement the plugin to avoid such crashes? Steps I've Taken:

Ensured that view controller presentations are done on the main thread Checked for proper view controller initialization Reviewed the timing of view controller presentations in the app lifecycle

Any insights or suggestions would be greatly appreciated. I'm happy to provide any additional information that might be helpful in resolving this issue. Thank you for your time and assistance.

Crash log given below. ios.app_issue_22018f9ee6806d9a0fe3114bf36b1f16_crash_session_94058fb005384c1baf8f0ecc16e41b50_DNE_0_v2_stacktrace.txt

yogo10803 commented 1 week ago

Is there any update on this issue?

luca-bonamini commented 1 week ago

anyone have a fix?

yogo10803 commented 1 day ago

Is any progress on the issue so far? Or other specific information is needed to help investigate further?