Skip to content

Alerts not showing iOS 13+ Scenedelegate + Multiwindow Support enabled #31251

Closed
@tanakaderoy

Description

@tanakaderoy

Description

On an iOS project using the new SceneDelegate and have multiwindow support turned on the Alert.alert() function is not working anymore. Seems like the change to use the new RCTAlertController in #29295 introduced the issue. Making the window via _alertWindow = [[UIWindow alloc] initWithFrame:RCTSharedApplication().keyWindow.bounds]; seems to be the problem. I have found a workaround of replacing the _alertWindow initialisation with this:

if (@available(iOS 13.0, *)) {
        for (UIWindowScene *scene in RCTSharedApplication().connectedScenes) {
          if(scene.activationState == UISceneActivationStateForegroundActive && scene.class == UIWindowScene.class) {
            _alertWindow = [[UIWindow alloc] initWithWindowScene:scene];
            break;
          }
        }
      }
      if (_alertWindow == nil) {
        _alertWindow = [[UIWindow alloc] initWithFrame:RCTSharedApplication().keyWindow.bounds];
      }

That allows the Alerts to show on both projects using regular Appdelegate & projects using the Scenedelegate

React Native version:

Run react-native info in your terminal and copy the results here.

System:
    OS: macOS 10.15.7
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
    Memory: 2.65 GB / 32.00 GB
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 10.16.3 - ~/.nvm/versions/node/v10.16.3/bin/node
    Yarn: 1.22.10 - /usr/local/bin/yarn
    npm: 6.9.0 - ~/.nvm/versions/node/v10.16.3/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.10.1 - /Users/[USERNAME]/.rvm/gems/ruby-2.6.3/bin/pod
  SDKs:
    iOS SDK:
      Platforms: iOS 14.4, DriverKit 20.2, macOS 11.1, tvOS 14.3, watchOS 7.2
    Android SDK:
      API Levels: 28, 29, 30
      Build Tools: 28.0.3, 29.0.2, 29.0.3, 30.0.1, 30.0.3
      System Images: android-29 | Intel x86 Atom_64, android-29 | Google Play Intel x86 Atom, android-30 | Google APIs Intel x86 Atom
      Android NDK: Not Found
  IDEs:
    Android Studio: 4.1 AI-201.8743.12.41.6953283
    Xcode: 12.4/12D4e - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_275 - /usr/bin/javac
    Python: 2.7.16 - /usr/bin/python
  npmPackages:
    @react-native-community/cli: Not Found
    react: Not Found
    react-native: 0.63.4 => 0.63.4
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Steps To Reproduce

Provide a detailed list of steps that reproduce the issue.

  1. make a new RN project -- 0.64.0 as per usual
  2. convert it to use the Scenedelegate and enable multiwindow support (good Guide on adding scenedelegate)
  3. make a simple App.js that shows an alert on button press
  4. run on iOS 13+ Alert won't show
/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 *
 * format
 * flow strict-local
 */

import React from 'react';
import {SafeAreaView, StyleSheet, StatusBar, Alert, Button} from 'react-native';

const App: () => React$Node = () => {
  const showAlert = () => {
    Alert.alert('Alert', "Hey I'm an Alert!");
    //Now Not Working!!!
  };

  return (
    <>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView style={styles.container}>
        <Button onPress={showAlert} title="Show Alert" />
      </SafeAreaView>
    </>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'space-around',
  },
});

export default App;

Expected Results

Alerts should be showing on iOS 13+

Snack, code example, screenshot, or link to a repository:

Here is a small repo that just shows the problem RNiOSAlertIssue
it has 2 branches main and AlertWorkingNoMultiWindow. main has the Scenedelegate changes. AlertWorkingNoMultiWindow branch is the repo before any scenedelegate changes or enabling multi window.

You may provide a screenshot of the application if you think it is relevant to your bug report.
Here are some tips for providing a minimal example: https://stackoverflow.com/help/mcve

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs: Triage 🔍StaleThere has been a lack of activity on this issue and it may be closed soon.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions