react-native: TextInput crashes when any text is entered while running as iOS app on apple silicon mac ("My Mac (Designed for iPhone)" target)

Description

When running iOS apps on apple silicon macs using the “My Mac (Designed for iPhone)” target, apps crash once you enter any text into a TextInput component.

Running the same code on an actual iOS devices works but not when running the app on Mac.

Steps to reproduce

npm i
npx pod-install

Open the project in Xcode and select “My Mac (Designed for iPhone)” as the destination. You also need to select a signing profile to make it run of course.

Start metro:

npm start

Once the app launches click on the TextInput and press any key. You will see that the app crashes.

First when you only focus the input without entering any text, the following will long many times:

Failed to get or decode unavailable reasons
Can't find or decode reasons

After you actually enter text it will crash.

React Native Version

0.76.5

Affected Platforms

Runtime - iOS, Runtime - Desktop

Output of npx react-native info

System:
  OS: macOS 15.1.1
  CPU: (10) arm64 Apple M1 Pro
  Memory: 1.77 GB / 32.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 21.1.0
    path: /usr/local/bin/node
  Yarn:
    version: 1.22.22
    path: /usr/local/bin/yarn
  npm:
    version: 10.9.0
    path: /usr/local/bin/npm
  Watchman:
    version: 2024.11.11.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.16.2
    path: /Users/jobpaardekooper/.rbenv/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 24.1
      - iOS 18.1
      - macOS 15.1
      - tvOS 18.1
      - visionOS 2.1
      - watchOS 11.1
  Android SDK: Not Found
IDEs:
  Android Studio: 2023.2 AI-232.10300.40.2321.11567975
  Xcode:
    version: 16.1/16B40
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.1
    path: /usr/bin/javac
  Ruby:
    version: 3.2.2
    path: /Users/jobpaardekooper/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli":
    installed: 15.0.1
    wanted: 15.0.1
  react:
    installed: 18.3.1
    wanted: 18.3.1
  react-native:
    installed: 0.76.5
    wanted: 0.76.5
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: true
  newArchEnabled: true

Stacktrace or Logs

An uncaught exception was raised
*** -[NSXPCEncoder _checkObject:]: This coder only encodes objects that adopt NSSecureCoding (object is of class 'RCTWeakEventEmitterWrapper').
(
	0   CoreFoundation                      0x00000001961bc300 __exceptionPreprocess + 176
	1   libobjc.A.dylib                     0x0000000195ca2cd8 objc_exception_throw + 88
	2   Foundation                          0x00000001972c9404 -[NSXPCEncoder _checkObject:] + 308
	3   Foundation                          0x00000001972c90d8 -[NSXPCEncoder _encodeUnkeyedObject:] + 40
	4   Foundation                          0x00000001972da128 -[NSXPCEncoder _encodeArrayOfObjects:forKey:] + 184
	5   Foundation                          0x00000001972f46fc -[NSDictionary(NSDictionary) encodeWithCoder:] + 568
	6   Foundation                          0x00000001972c9710 -[NSXPCEncoder _encodeObject:] + 488
	7   Foundation                          0x0000000197396210 -[NSAttributedString encodeWithCoder:] + 224
	8   Foundation                          0x00000001972c9710 -[NSXPCEncoder _encodeObject:] + 488
	9   RemoteTextInput                     0x00000001b5f0eda0 -[RTIDocumentState encodeWithCoder:] + 592
	10  Foundation                          0x00000001972c9710 -[NSXPCEncoder _encodeObject:] + 488
	11  Foundation                          0x00000001972ce400 _NSXPCSerializationAddInvocationWithOnlyObjectArgumentsArray + 116
	12  Foundation                          0x00000001972ce2b8 -[NSXPCEncoder _encodeInvocationObjectArgumentsOnly:count:typeString:selector:isReply:into:] + 212
	13  Foundation                          0x00000001972c7bd4 -[NSXPCConnection _sendInvocation:orArguments:count:methodSignature:selector:withProxy:] + 1208
	14  Foundation                          0x00000001972ce1a8 -[NSXPCConnection _sendSelector:withProxy:arg1:arg2:] + 128
	15  Foundation                          0x00000001972ce0d0 _NSXPCDistantObjectSimpleMessageSend2 + 68
	16  RemoteTextInput                     0x00000001b5f13854 __52-[RTIInputSystemClient _updateTextForSessionWithID:]_block_invoke + 72
	17  RemoteTextInput                     0x00000001b5f131b8 __58-[RTIInputSystemClient enumerateServices:force:withBlock:]_block_invoke.139 + 60
	18  CoreFoundation                      0x0000000196179aa4 __NSSET_IS_CALLING_OUT_TO_A_BLOCK__ + 24
	19  CoreFoundation                      0x000000019618c990 -[__NSSetM enumerateObjectsWithOptions:usingBlock:] + 276
	20  RemoteTextInput                     0x00000001b5f13088 -[RTIInputSystemClient enumerateServices:force:withBlock:] + 288
	21  RemoteTextInput                     0x00000001b5f137d8 -[RTIInputSystemClient _updateTextForSessionWithID:] + 244
	22  RemoteTextInput                     0x00000001b5f14f5c -[RTIInputSystemClient remoteTextInputSessionWithID:documentDidChange:] + 156
	23  UIKitCore                           0x00000001c884f99c __39-[UIKBRTIPartner documentStateChanged:]_block_invoke + 136
	24  UIKitCore                           0x00000001c88543d0 __48-[UIKBRTIPartner _updateRTIStateWithCompletion:]_block_invoke + 288
	25  UIKitCore                           0x00000001c885c5ec -[UIKBRTIPartner _queryUIKitDocumentRequest:completion:] + 4336
	26  UIKitCore                           0x00000001c885a208 -[UIKBRTIPartner _queryDocumentRequest:completion:] + 136
	27  UIKitCore                           0x00000001c885427c -[UIKBRTIPartner _updateRTIStateWithCompletion:] + 312
	28  UIKitCore                           0x00000001c8852ee4 -[UIKBRTIPartner updateStateWithCompletion:updateTraits:] + 144
	29  UIKitCore                           0x00000001c884f900 -[UIKBRTIPartner documentStateChanged:] + 176
	30  UIKitCore                           0x00000001c8143d44 -[_UIKeyboardStateManager setDocumentState:] + 208
	31  UIKitCore                           0x00000001c815a5d8 -[_UIKeyboardStateManager updateKeyboardStateForInsertion:] + 104
	32  UIKitCore                           0x00000001c850df94 -[UIKBInputDelegateManager insertText:updateInputSource:] + 464
	33  UIKit                               0x0000000272d743c4 -[UIKBInputDelegateManagerAccessibility insertText:updateInputSource:] + 112
	34  UIKitCore                           0x00000001c8508288 -[UIKBInputDelegateManager insertText:] + 92
	35  UIKitCore                           0x00000001c815150c -[_UIKeyboardStateManager performKeyboardOutput:checkingDelegate:forwardToRemoteInputSource:] + 3732
	36  UIKit                               0x0000000272d657c0 -[_UIKeyboardStateManagerAccessibility performKeyboardOutput:checkingDelegate:forwardToRemoteInputSource:] + 140
	37  UIKitCore                           0x00000001c8859e80 -[UIKBRTIPartner _performKeyboardOutputOperations:] + 608
	38  UIKit                               0x0000000272d68e6c -[UIKBRTIPartnerAccessibility _performKeyboardOutputOperations:] + 100
	39  UIKitCore                           0x00000001c8858430 -[UIKBRTIPartner _queued_performTextOperations:resultHandler:] + 780
	40  libdispatch.dylib                   0x0000000103258b64 _dispatch_call_block_and_release + 32
	41  libdispatch.dylib                   0x000000010325a8a4 _dispatch_client_callout + 20
	42  libdispatch.dylib                   0x000000010326dd38 _dispatch_main_queue_drain + 1092
	43  libdispatch.dylib                   0x000000010326d8e4 _dispatch_main_queue_callback_4CF + 44
	44  CoreFoundation                      0x0000000196188e60 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16
	45  CoreFoundation                      0x0000000196148a4c __CFRunLoopRun + 1996
	46  CoreFoundation                      0x0000000196147bc4 CFRunLoopRunSpecific + 588
	47  HIToolbox                           0x00000001a15b7f64 RunCurrentEventLoopInMode + 292
	48  HIToolbox                           0x00000001a15bdd54 ReceiveNextEventCommon + 636
	49  HIToolbox                           0x00000001a15bdeb8 _BlockUntilNextEventMatchingListInModeWithFilter + 76
	50  AppKit                              0x0000000199c73a08 _DPSNextEvent + 660
	51  AppKit                              0x000000019a5b3e0c -[NSApplication(NSEventRouting) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 688
	52  AppKit                              0x0000000199c66ae0 -[NSApplication run] + 480
	53  AppKit                              0x0000000199c3d364 NSApplicationMain + 888
	54  AppKit                              0x0000000199e8b870 +[NSWindow _savedFrameFromString:] + 0
	55  UIKitMacHelper                      0x00000001b097ab38 UINSApplicationMain + 972
	56  UIKitCore                           0x00000001c7a473a8 UIApplicationMain + 148
	57  ReproducerApp.debug.dylib           0x00000001062fc288 __debug_main_executable_dylib_entry_point + 96
	58  dyld                                0x0000000195ce0274 start + 2840
)

Reproducer

https://github.com/jobpaardekooper/rn-textinput-crash

Screenshots and Videos

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 9
  • Comments: 33 (1 by maintainers)

Commits related to this issue

Most upvoted comments

There have been three PRs now addressing this (severe) problem: https://github.com/facebook/react-native/pull/48583, https://github.com/facebook/react-native/pull/49320, https://github.com/facebook/react-native/pull/48225.

Why has this still not been included in any release yet?

The fix I mentioned above has now been reverted by PR https://github.com/facebook/react-native/pull/48868 and commit 9b06d0d so I reopened this issue.

For anyone who needs a workaround urgently: Patching react-native with the changes from https://github.com/facebook/react-native/commit/0511e2e49a51ab32aa6f40df7c6b3dde92b09031 works just fine.

Was the perfect opportunity to give bun patch a shot (and it was super smooth). I’ve attached the .patch file for anyone interested.

patches/react-native@0.77.1.patch:

diff --git a/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm b/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm
index d2ace9416ecd3a18ad024fb23e154bdedb053321..69f22a6cfbc35261cf2db1643ba25ff9475662ca 100644
--- a/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm
+++ b/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm
@@ -101,7 +101,7 @@ static NSSet<NSNumber *> *returnKeyTypesSet;
   NSMutableDictionary<NSAttributedStringKey, id> *defaultAttributes =
       [_backedTextInputView.defaultTextAttributes mutableCopy];
 
-#if !TARGET_OS_MACCATALYST
+#if !TARGET_OS_MAC && !TARGET_OS_MACCATALYST
   RCTWeakEventEmitterWrapper *eventEmitterWrapper = [RCTWeakEventEmitterWrapper new];
   eventEmitterWrapper.eventEmitter = _eventEmitter;
   defaultAttributes[RCTAttributedStringEventEmitterKey] = eventEmitterWrapper;
@@ -266,7 +266,7 @@ static NSSet<NSNumber *> *returnKeyTypesSet;
   if (newTextInputProps.textAttributes != oldTextInputProps.textAttributes) {
     NSMutableDictionary<NSAttributedStringKey, id> *defaultAttributes =
         RCTNSTextAttributesFromTextAttributes(newTextInputProps.getEffectiveTextAttributes(RCTFontSizeMultiplier()));
-#if !TARGET_OS_MACCATALYST
+#if !TARGET_OS_MAC && !TARGET_OS_MACCATALYST
     defaultAttributes[RCTAttributedStringEventEmitterKey] =
         _backedTextInputView.defaultTextAttributes[RCTAttributedStringEventEmitterKey];
 #endif
diff --git a/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm b/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm
index f2dde69b83f6ec141f211ba5561aa63ded64adc0..19bda813dc5819b84a7848dbec61c32064a6709d 100644
--- a/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm
+++ b/ReactCommon/react/renderer/textlayoutmanager/platform/ios/react/renderer/textlayoutmanager/RCTAttributedTextUtils.mm
@@ -405,7 +405,7 @@ static NSMutableAttributedString *RCTNSAttributedStringFragmentWithAttributesFro
 {
   auto nsAttributedStringFragment = RCTNSAttributedStringFragmentFromFragment(fragment, placeholderImage);
 
-#if !TARGET_OS_MACCATALYST
+#if !TARGET_OS_MAC && !TARGET_OS_MACCATALYST
   if (fragment.parentShadowView.componentHandle) {
     RCTWeakEventEmitterWrapper *eventEmitterWrapper = [RCTWeakEventEmitterWrapper new];
     eventEmitterWrapper.eventEmitter = fragment.parentShadowView.eventEmitter;

I am also having similar issue. In my case issue occurs when I am using production release scheme, On debug scheme things work fine. Not able to release new update to users due to this issue. @jobpaardekooper did you find any solution?

After upgrading to RN 77.2, input boxes work properly on OSX, however older iOS versions (such as latest supported 15.x) are now having similar problem. The text box can be focused, typing works, but in the moment the text box loses focus, the application “hangs” and eventually crashes.

@hems I assume you are using expo EAS. I personally don’t use but I think it does not support launching your app on your Mac directly. Your best bet is probably to create a new build (that uses a RN version that includes this fix) and upload this to TestFlight. Then you can use TestFlight on Mac to install your app and see if the crash is gone. Of course this is not a nice way to try and debug something but at least it is a way to test something. Otherwise you need to build locally with expo prebuild so that you can open xcode and select your mac as a target to run on.

It still here in react-native 0.77

Image

Hi, all, #49320 already tries to fix this issue, please wait for rn core team to review.

@shubhamdeol I have not found a work around for this. And I am in the same situation where my new app updates are being blocked by this issue.

What Mac OS, RN and Xcode version are you using? Are you on the new architecture?