proyecto26 / react-native-inappbrowser

📱InAppBrowser for React Native (Android & iOS) 🤘
https://www.npmjs.com/package/react-native-inappbrowser-reborn
MIT License
1.32k stars 227 forks source link

iOS iPad crash on InAppBrowser.open() when `modalPresentationStyle: 'popover'` #290

Open zacharyweidenbach opened 3 years ago

zacharyweidenbach commented 3 years ago

Which platform(s) does your issue occur on?

Please, provide the following version numbers that your issue occurs with:

Please, tell us how to recreate the issue in as much detail as possible.

Describe the steps to reproduce it.

The crash occurs when attempting to open the inappbrowser in with modalEnabled: true on iOS with specifically an iPad. The crash does not occur when using an iphone. Also of note, no error is thrown in the JS Runtime layer, the crash as seen is from the log output in xcode. The crash:

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (<UIPopoverPresentationController: 0x7fab150fdd50>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff20422fba __exceptionPreprocess + 242
    1   libobjc.A.dylib                     0x00007fff20193ff5 objc_exception_throw + 48
    2   UIKitCore                           0x00007fff23e56a58 __66-[UIPopoverPresentationController presentationTransitionWillBegin]_block_invoke + 0
    3   UIKit                               0x000000011641d04d -[UIPopoverPresentationControllerAccessibility presentationTransitionWillBegin] + 41
    4   UIKitCore                           0x00007fff23e62b14 __80-[UIPresentationController _initViewHierarchyForPresentationSuperview:inWindow:]_block_invoke + 2632
    5   UIKitCore                           0x00007fff23e602dc __56-[UIPresentationController runTransitionForCurrentState]_block_invoke.466 + 511
    6   UIKitCore                           0x00007fff24bb1e36 -[_UIAfterCACommitBlock run] + 54
    7   UIKitCore                           0x00007fff246d01f8 _runAfterCACommitDeferredBlocks + 333
    8   UIKitCore                           0x00007fff246c01e4 _cleanUpAfterCAFlushAndRunDeferredBlocks + 221
    9   UIKitCore                           0x00007fff246f1a36 _afterCACommitHandler + 85
    10  CoreFoundation                      0x00007fff2038fd31 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    11  CoreFoundation                      0x00007fff2038a542 __CFRunLoopDoObservers + 541
    12  CoreFoundation                      0x00007fff2038aaf5 __CFRunLoopRun + 1129
    13  CoreFoundation                      0x00007fff2038a1a7 CFRunLoopRunSpecific + 567
    14  GraphicsServices                    0x00007fff2b874d85 GSEventRunModal + 139
    15  UIKitCore                           0x00007fff246c14df -[UIApplication _run] + 912
    16  UIKitCore                           0x00007fff246c639c UIApplicationMain + 101
    17  PrimeWellOnTarget-Test              0x000000010f51d0c0 main + 112
    18  libdyld.dylib                       0x00007fff2025abbd start + 1
    19  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'UIPopoverPresentationController (<UIPopoverPresentationController: 0x7fab150fdd50>) should have a non-nil sourceView or barButtonItem set before the presentation occurs.'
terminating with uncaught exception of type NSException
CoreSimulator 757.5 - Device: iPad Pro (12.9-inch) (5th generation) (B48EA0CC-DD58-49B9-B64C-22E94E869CCB) - Runtime: iOS 14.5 (18E182) - DeviceType: iPad Pro (12.9-inch) (5th generation)

Is there any code involved?

export const useOpenInBrowser = () => {
  return React.useCallback(async (url: string) => {
    try {
      const hasInAppBrowser = await InAppBrowser.isAvailable();

      if (hasInAppBrowser) {
        InAppBrowser.close();
        return InAppBrowser.open(url, {
          readerMode: false,
          modalEnabled: true,
          animated: true,
          modalPresentationStyle: 'popover',
        }).catch(swallowResponse);
      } else {
        const canOpenInStandardBrowser = await Linking.canOpenURL(url);
        if (canOpenInStandardBrowser) {
          return Linking.openURL(url);
        }
      }
    } catch (e) {
      logRuntimeError('openInBrowser error', e);
    }
  }, []);
};

By switching modalEnabled: false the .open() method does not cause a crash. Or switching modalPresentationStyle: 'popover' to 'automatic' does not cause a crash.

jdnichollsc commented 3 years ago

Hello mate, if I understand, you want to use popover when modal option is enabled, right?

zacharyweidenbach commented 3 years ago

Hello mate, if I understand, your want to use popover when modal option is enabled, right?

Correct.

jdnichollsc commented 3 years ago

Ok, let me check, any pull request is welcome in the meantime! <3

trithien98 commented 3 years ago

Ok, let me check, any pull request is welcome in the meantime! <3

Hi @jdnichollsc . i trying make inappbrower in ios device use safari like ui this picture from your github repo page but not work .
Can you teach me how can i making the inapp browers same you .My device is Simulator Iphone11 my code is `InAppBrowser.openAuth( 'https://github.com/proyecto26/react-native-inappbrowser', deepLink, {

        dismissButtonStyle: 'cancel',
        preferredBarTintColor: '#453AA4',
        preferredControlTintColor: 'white',
        readerMode: false,
        animated: true,
        modalPresentationStyle: 'fullScreen',
        modalTransitionStyle: 'coverVertical',
        modalEnabled: true,
        enableBarCollapsing: false,

        showTitle: true,
        toolbarColor: '#6200EE',
        secondaryToolbarColor: 'black',
        navigationBarColor: 'black',
        navigationBarDividerColor: 'white',
        enableUrlBarHiding: true,
        enableDefaultShare: true,
        forceCloseOnRedirection: false,
      },
    ).then((response) => {
      if (response.type === 'success' && response.url) {
        Linking.openURL(response.url);
      }
    });`

Simulator Screen Shot - iPhone 11 - 2021-08-06 at 09 43 55

asdsad12
jdnichollsc commented 3 years ago

No idea, let me know if you find any Apple documentation about this issue

sergibondarenko commented 1 year ago

I hit this error too. I tried to put the InAppBrowser calls in the try ... catch to call the normal browser openLink as a fallback but the app crashed on the InAppBrowser.open call nevertheless. Because the exception is thrown by the native code.

try {
  if (!(await InAppBrowser.isAvailable())) {
    throw new Error()
  }

  await InAppBrowser.open(widgetUrl, BROWSER_CONFIG)
} catch (error) {
  await openLink(widgetUrl)
}
Abhishek2250 commented 1 year ago

Hi, I'm also facing the same issue with iPad Another issue: https://github.com/wix/react-native-navigation/issues/6344 Issue on apple dev: https://developer.apple.com/forums/thread/685031

Abhishek2250 commented 1 year ago

modalPresentationStyle: isIpad() ? "pageSheet" : "popover"

workaround