Search code examples
iosswiftxcodeuitextviewxcode11

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?


Solution

  • 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 :)