ChatGPT解决这个技术问题 Extra ChatGPT

After upgrading to Xcode 11.2 from Xcode 11.1, app crashes due to _UITextLayoutView

After upgrading to Xcode 11.2 from Xcode 11.1 my app crashes:

*** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named _UITextLayoutView because no class named _UITextLayoutView was found; the class needs to be defined in source code or linked in from a library (ensure the class is part of the correct target)'

Why is this happening? How can I prevent this crash?

Seems like the Xcode 11.2 bug. Check the thread forums.developer.apple.com/thread/125287 . Most likely it is related to the TextViews in the hierarchy
@DanielStorm unfortunately, that did not help. I've just added a plain UITextView to one screen which did not have it before (and did not crash before as well :) ). Now it crashes with the same error. The issue persists for all the devices/simulators except for the latest iOS 13.2
Does anybody know if this will crash production apps built with Xcode 11.2 when running on iOS 13.1.2 devices?
this is a perfectly good question, hence the 198 votes, so I am really not sure why someone marked it as "put on hold unclear"
@MikeVolmar I voted to close and no longer relevant. It's fixed with 11.2.1. This question had a very short window of usefulness.

M
Mojtaba Hosseini

Update: Fixed! 🎉🎊

The ONLY Solution is to update

This bug is fixed in Xcode 11.2.1. So you can download and use it from here.

Storyboards containing a UITextView will no longer cause the app to crash on operating system versions earlier than iOS 13.2, tvOS 13.2, or macOS 10.15.2. (56808566, 56873523)

Xcode 11.2 is deprecated by Apple on November 5, 2019

if you ever try to submit your app that build with Xcode 11.2 to the AppStore, you will be rejected:

App Store Connect Operation Warning

WARNING ITMS-90703: "Deprecated Xcode Build. Due to resolved app archives issues, we have deprecated Xcode 11.2 on November 5, 2019. Download Xcode 11.2.1 or newer, rebuild your app and resubmit."

So all workarounds done with the Xcode 11.2 is useless

It's a bug for Xcode 11.2, and fixed in Xcode 11.2.1.

Solution(s)

Roll back to previous Xcode release version from: Rollback is not an option anymore and AppStore will reject any build with Xcode below 11.2.1 take a look at this

https://developer.apple.com/services-account/download?path=/Developer_Tools/Xcode_11.1/Xcode_11.1.xip Note that you should use Safari to download it and you must first login to Apple developer portal.

You can find all other Xcode versions and other resources link (including release and beta versions) here at https://developer.apple.com/download/more

The workaround

This is very hard but working workaround. Replace all UITextViews in storyboards and Xibs with the pure code version.

Note that this bug is found and fixed by Apple

https://i.stack.imgur.com/OhSdh.png

Also earlier, the bug was confirmed by Apple Staff edford

https://i.stack.imgur.com/mptit.png

For those with iOS 13.2 and can not use Xcode 11.1 anymore:

Update macOS to 10.15.1 or later Install Xcode 11.2.1 or later It should be work now on the updated device.

For those with storyboard:

Subclass UITextView Assign it to all UITextView objects Don't forget to update any property changes that may lose in the subclassing.

For those comfortable with method swizzling (Objc and dynamic behavior)

Head to the @aftab muhammed khan answer for Objective-C and @MikRo answer for Swift adapted version

Just don't do it anymore:

Even if these last two swizzling workarounds are not using Apple private API, they will be rejected in AppStore because Apple will not accept builds with Xcode versions under 11.2.1!

And once again:

Xcode 11.2 is deprecated by Apple on November 5, 2019


Sad, but the solution is working for me. Thanks! 11.1 works. I would not suggest converting to pure code, because i bet there is a fix coming out soon. If you don't mind: Update this answer once there is a new version that works. Would be bad if people start downloading 11.1 event though there is a fixed version out there :)
I already have 11.1 but didn't work, i didn't upgrade to 11.2.
Please note that you should probably delete the app from your development device and/or clean Xcode's build folder (CMD + Shift + K)
How can I upgrade to v11.2.1 ? It's not showing up on the app store. And downloading the GM seed from the developer portal tries to download the app all over for about 8gb!
Also if it's deprecated, why doesn't apple just remove 11.2 on the app store for now?
c
cokeman19

Congratulation

The New version of Xcode (11.2.1) is available now which is the best way to get rid off this issue.

Workarounds

@Mojtaba Hosseini the solution I proposed was from the help and the participation from my side to my fellow developers over StackOverflow. You, me and all the rest of the developer here already know that when the new version is announced by Apple, this issue will be gone.

But Beside Everything

The solution aforementioned was definitely accepted by Apple Review as there is no private API involved at all. This approach is very similar to the creating property like

@interface UITextView (Layout)

Or

UITextView+Layout.h

So when you are creating property you are directly using APPLE Private Components and re-moduling them as per you depends or requirement.

The Simple Example is AMFNetworking classes

- (void)setImageWithURL:(NSURL *)url {
    [self setImageWithURL:url placeholderImage:nil];
}

Hope I am done with the Allegation

The answer below was just some help from my side to enable developer to continue developing as you we initially proposed developer to roll back Xcode. This was a bad practice to download 8 GB Xcode again since we all know that the new version of Xcode will be released soon.

While it is fixed in Xcode 11.2.1, I got one solution for Xcode 11.2 by which you can get rid off this crash:

*** Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named _UITextLayoutView because no class named _UITextLayoutView was found; the class needs to be defined in source code or linked in from a library (ensure the class is part of the correct target)'

SOLUTION

Go to the Build Setting search for "DEAD_CODE_STRIPPING" and set it to NO

DEAD_CODE_STRIPPING = NO

Then

create files UITextViewWorkaround

UITextViewWorkaround.h

    #import <Foundation/Foundation.h>


    @interface UITextViewWorkaround : NSObject
    + (void)executeWorkaround; 
@end

UITextViewWorkaround.m

#import "UITextViewWorkaround.h"
#import  <objc/runtime.h>



    @implementation UITextViewWorkaround

    + (void)executeWorkaround {
        if (@available(iOS 13.2, *)) {
        }
        else {
            const char *className = "_UITextLayoutView";
            Class cls = objc_getClass(className);
            if (cls == nil) {
                cls = objc_allocateClassPair([UIView class], className, 0);
                objc_registerClassPair(cls);
    #if DEBUG
                printf("added %s dynamically\n", className);
    #endif
            }
        }
    }

    @end

execute it in the app delegate

#import "UITextViewWorkaround.h"

        - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
            // Override point for customization after application launch.

            [UITextViewWorkaround executeWorkaround];
    return yes;
    }

Compile the code and you will have a running app :)


If its working for you kindly rank up so other can try this solution as well
@Hardy_Germany I tested this a little, for Swift you can try: class UITextViewWorkaround: NSObject { class func executeWorkaround() { if #available(iOS 13.2, *) { } else { let className = "_UITextLayoutView" var cls = objc_getClass(className) if cls == nil { cls = objc_allocateClassPair(UIView.self, className, 0) objc_registerClassPair(cls as! AnyClass) #if DEBUG print("added %@ dynamically\n", className); #endif } } } }
@DaveDude DEAD_CODE_STRIPPING = NO is needed if you use Swift Package Manager with Xcode 11.2
@pradipsutariya yes off-course why not we never change anything in APPLE CLASSES.
Only Xcode 11.2.1 GM seed(beta) is Released. Stable version may release soon.
H
Hardy_Germany

The issue was fixed in Xcode 11.2.1.

EDIT: As the fix is now released, you should switch to that Xcode version and comment out this workaround. As Mojtaba Hosseini in his answer mentioned:

... these last two swizzling workarounds are using Apple private API and will be reject from Apple review!

For the time until the fix was released by Apple, this was a good workaround to continue developing and testing.

For Xcode 11.2, based on the idea of Aftab Muhammed Khan and with the help of John Nimis I just tested the following code.

No change in the storyboard files necessary!

Edited my AppDelegate.swift file and added this class

//******************************************************************
// MARK: - Workaround for the Xcode 11.2 bug
//******************************************************************
class UITextViewWorkaround: NSObject {

    // --------------------------------------------------------------------
    // MARK: Singleton
    // --------------------------------------------------------------------
    // make it a singleton
    static let unique = UITextViewWorkaround()

    // --------------------------------------------------------------------
    // MARK: executeWorkaround()
    // --------------------------------------------------------------------
    func executeWorkaround() {

        if #available(iOS 13.2, *) {

            NSLog("UITextViewWorkaround.unique.executeWorkaround(): we are on iOS 13.2+ no need for a workaround")

        } else {

            // name of the missing class stub
            let className = "_UITextLayoutView"

            // try to get the class
            var cls = objc_getClass(className)

            // check if class is available
            if cls == nil {

                // it's not available, so create a replacement and register it
                cls = objc_allocateClassPair(UIView.self, className, 0)
                objc_registerClassPair(cls as! AnyClass)

                #if DEBUG
                NSLog("UITextViewWorkaround.unique.executeWorkaround(): added \(className) dynamically")
               #endif
           }
        }
    }
}

and within the delegate call for "didFinishLaunchingWithOptions" call the workaround

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

    // Override point for customization after application launch.

    // This is the workaround for Xcode 11.2
    UITextViewWorkaround.unique.executeWorkaround()
}

N
Nike Kov

I've adapted khan's Obj-C solution to Swift:

import UIKit

@objc
class UITextViewWorkaround : NSObject {

    static func executeWorkaround() {
        if #available(iOS 13.2, *) {
        } else {
            let className = "_UITextLayoutView"
            let theClass = objc_getClass(className)
            if theClass == nil {
                let classPair: AnyClass? = objc_allocateClassPair(UIView.self, className, 0)
                objc_registerClassPair(classPair!)
            }
        }
    }

}

Call it at the end of didFinishLaunchingWithOptions in AppDelegate.

Thanks @Aftab!


where we need to add this code? In delegate files ?
@SIDHARTHPU you can call it in didFinishLaunchingWithOptions
tried a variety of the fixes on this issue and this one did the trick until Xcode 11.3?! Thx!
This is most short and brilliant solution!
@LalKrishna Create new file named UITextViewWorkaround.swift and add above code to it. Then add UITextViewWorkaround.executeWorkaround() this to didFinishLaunchingWithOptions before return statement.
g
garafajon

A quicker fix:

///Substitute class for _UITextLayoutView bug
class FixedTextView: UITextView {
    required init?(coder: NSCoder) {
        if #available(iOS 13.2, *) {
            super.init(coder: coder)
        }
        else {
            let rect = CGRect(origin: .zero, size: CGSize(width: 100, height: 44*3))
            super.init(frame: rect, textContainer: nil)
        }
    }
}

Add this code somewhere and then replace all storyboard instances to FixedTextView.

Note: you will lose any attributes created in the storyboards. This could have serious implications (e.g. delegate setting, size, etc.)


This seems to be a good step towards a workaround, but not quite a solution yet...
a
anoo_radha

Updated Solution: Update to Xcode 11.2.1. It works on iOS 11, 12 or 13 devices for me.

Refer to apple's documentation This update fixes a critical issue that could cause apps using UITextView to crash.

Old Solution: Downloaded Xcode 11.1 from https://developer.apple.com/download/more/ Switching back from 11.2 to 11.1 fixed the crash.

Also, for me even with Xcode 11.2, when I upgraded my iPhone to 13.2, that fixed the crash.


This is the easiest fix - upgrade the iPhone iOS to 13.2
Upgrading iOS is clearly not a fix given that OP is talking under a developer perspective.
Apple confirmed that the bug occurs with iOS versions before 13.2 and Xcode 11.2.
Downgraded XCode to Version 11.1 (11A1027) -- still have the same crash (
if you upgrade or downgrade and it still crashes clean Xcode's build folder (CMD + Shift + K) per Wojteck's comment below, which fixed this for me on upgrade
C
Cœur

11.2.1 GM seed resolves this issue

(and it can be used to publish to App Store)

Go to https://developer.apple.com/download/. Download Xcode 11.2.1 GM seed

Release notes confirm it fixes this error:

https://i.stack.imgur.com/ZAgaf.png


Please note that after installing Xcode 11.2.1 GM seed you should probably delete the app from your development device and/or clean Xcode's build folder (CMD + Shift + K).
p
pkamb

You can go to download the last Xcode beta release (11.2.1 GM) from Apple developer website.

Here the direct link

https://i.stack.imgur.com/gQGMC.png


Please note that after installing Xcode 11.2.1 GM seed you should probably delete the app from your development device and/or clean Xcode's build folder (CMD + Shift + K).
installed and run without any other procedure. Problem fixed ✌️
how do I install this ? It gave me a folder, but there is no executable
just download zip from the link provided and extract it. Then copy the App in Application folder.
the problem is that when I extract it, I get two files, Content and Metadata ... thats why I asked what to do with them. I used an archive utility because clicking on it normally was failing. I'll try again
A
Awsom3D

Improving on @garafajon answer. For me it works in most cases.

///Substitute class for _UITextLayoutView bug
class FixedTextView: UITextView {
    required init?(coder: NSCoder) {
        if #available(iOS 13.2, *) {
            super.init(coder: coder)
        }
        else {
            super.init(frame: .zero, textContainer: nil)
            self.autoresizingMask = [.flexibleWidth, .flexibleHeight]
            self.contentMode = .scaleToFill

            self.isScrollEnabled = false   // causes expanding height

            // Auto Layout
            self.translatesAutoresizingMaskIntoConstraints = false
            self.font = UIFont(name: "HelveticaNeue", size: 18)
        }
    }
}

Works perfectly. @garafajon's code caused issues with boxes in unexpected places. This fixes those issues~ great job!
P
Pavel Stepanov

As a "quick" fix you can add the UITextView directly from the code and not via IB. At least it worked for me. Though from my point of view it's better to rollback to previous Xcode/wait for the new one.


C
Cœur

It is a bug with Xcode 11.2. Subclassed Textviews are crashing on all devices not having the neweset iOS build (13.2) installed. You probably better not build a release with that build.

You can now:

downgrade Xcode to 11.1 or

upgrade your device to iOS 13.2


So does this bug only affect subclassed text views?
Updating iOS when updating Xcode resolves the issue.
The question is, if the crash will happen on a released app with every iOS smaller than 13.2... I am afraid to try...
E
Eric Lange

I used a successful workaround, but it was painful. This is the process that I followed:

Open the XIB in a text editor Find the offending TextView. In my case:

<textView clipsSubviews="YES" multipleTouchEnabled="YES" contentMode="scaleToFill" textAlignment="natural" translatesAutoresizingMaskIntoConstraints="NO" id="782-j1-88c" customClass="LCAnsiConsoleTextView">
  <rect key="frame" x="16" y="20" width="343" height="589"/>
  <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
  <fontDescription key="fontDescription" name="Menlo-Regular" family="Menlo" pointSize="12"/>
  <textInputTraits key="textInputTraits" autocapitalizationType="sentences"/>
</textView>

Note its id (in my case: id="782-j1-88c") Override the class as noted in the answers above and recreate the options (mine is Objective-C, sorry):

@implementation FixedTextView

- (id) initWithCoder:(NSCoder*)coder
{
    if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){13,2,0}])
        self = [super initWithCoder:coder];
    else {
        self = [super initWithFrame:CGRectMake(16, 3, 343, 605)];
        self.editable = YES;
        self.selectable = YES;
        self.insetsLayoutMarginsFromSafeArea = YES;
        self.clipsToBounds = YES;
        self.clearsContextBeforeDrawing = YES;
        self.autoresizesSubviews = YES;
        self.contentMode = UIViewContentModeScaleToFill;
        self.scrollEnabled = YES;
        self.userInteractionEnabled = YES;
        self.multipleTouchEnabled = YES;
        self.translatesAutoresizingMaskIntoConstraints = NO;
        self.font = [UIFont fontWithName:@"Menlo-Regular" size:12.0];
    }
    return self;
}

Note the constraints that include your text view id, and recreate those constraints against the other element ids in your view or view controller. In my case:

- (id) initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self xibSetup];
        [self initView];
/*
        <constraint firstItem="75C-lt-YtE" firstAttribute="top" secondItem="782-j1-88c" secondAttribute="bottom" constant="8" symbolic="YES" id="8SH-5l-FAs"/>
        <constraint firstItem="782-j1-88c" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leadingMargin" id="Mve-aZ-HCe"/>
        <constraint firstItem="782-j1-88c" firstAttribute="leading" secondItem="75C-lt-YtE" secondAttribute="leading" id="dPG-u3-cCi"/>
        <constraint firstItem="782-j1-88c" firstAttribute="trailing" secondItem="iN0-l3-epB" secondAttribute="trailingMargin" id="sjT-0Q-hNj"/>
        <constraint firstItem="782-j1-88c" firstAttribute="top" secondItem="vUN-kp-3ea" secondAttribute="top" id="vic-vZ-osR"/>
*/
        [self.command.topAnchor constraintEqualToAnchor:self.console.bottomAnchor constant:8].active = YES;
        [self.console.leadingAnchor constraintEqualToAnchor:self.layoutMarginsGuide.leadingAnchor].active = YES;
        [self.console.leadingAnchor constraintEqualToAnchor:self.command.leadingAnchor].active = YES;
        [self.console.trailingAnchor constraintEqualToAnchor:self.trailingAnchor].active = YES;
        [self.console.topAnchor constraintEqualToAnchor:self.safeAreaLayoutGuide.topAnchor].active = YES;

    }
    return self;
}

Doing this fixed the issue for me with no loss in desired functionality. Fortunately I had only one UITextView to replace. Otherwise, this gets untenable.


M
Mohammad Parvez

I had the same issue I just upgraded my Xcode 11.2 to 11.2.1 it worked fine.

After upgrade I have tested the same on iOs 13 and iOS 12 and it was working fine.


Duplicate of BossOz answer.
J
Jayprakash Dubey

1. Problem:

There is an issue with Xcode 11.2 wherein Storyboards containing a UITextView will cause the app to crash on OS versions earlier than iOS 13.2 if compiled with Xcode 11.2.

https://i.stack.imgur.com/ocZeY.png

Check this apple documentation.

2. Solution:

The only solution is to update your Xcode to 11.2.1 or 11.3.

Xcode 11.2.1 was particularly released to fix this crash issue.

https://i.stack.imgur.com/uCp04.png

3. Suggestion:

I would suggest you to go with latest version of Xcode 11.3 since this supports developing apps for iOS 13.3 and also there are many new features. Check this apple documentation.


p
pkamb

This issue was fixed in Xcode version 11.2.1, and called out in the release notes:

This update fixes a critical issue that could cause apps using UITextView to crash

https://i.stack.imgur.com/sy65R.png