从Xcode 11.1升级到Xcode 11.2后,由于_UITextLayoutView,应用程序崩溃

时间:2019-11-01 09:47:39

标签: ios swift xcode uitextview xcode11.2

从Xcode 11.1升级到Xcode 11.2后,我的应用崩溃了:

  

***由于未捕获的异常'NSInvalidUnarchiveOperationException'而终止应用程序,原因:'无法实例化名为_UITextLayoutView的类,因为未找到名为_UITextLayoutView的类;该类需要在源代码中定义或从库中链接(确保该类是正确目标的一部分)'

为什么会这样?如何防止此崩溃?

15 个答案:

答案 0 :(得分:221)

更新:已修复! ??

唯一的解决方案是更新

bug在Xcode 11.2.1中是fixed。因此,您可以download and use it from here.

  

包含UITextView的故事板将不再导致该应用在iOS 13.2,tvOS 13.2或macOS 10.15.2之前的操作系统版本上崩溃。 (56808566,56873523)


Apple于2019年11月5日弃用Xcode 11.2

如果您尝试将使用Xcode 11.2生成的应用提交到AppStore,则会被拒绝:

App Store Connect操作警告

  
    

警告ITMS-90703 :“不建议使用Xcode Build。由于已解决的应用程序存档问题,我们已于2019年11月5日弃用了Xcode 11.2。下载Xcode 11.2.1或更高版本,重建您的应用程序并重新提交。”

  

因此,使用Xcode 11.2完成的所有解决方法都是无用的


这是Xcode 11.2的错误,已在Xcode 11.2.1中修复。

解决方案

从以下版本回滚到以前的Xcode 版本版本:不再提供回滚功能,AppStore将拒绝11.2.1 take a look at this以下的任何Xcode构建

  

https://developer.apple.com/services-account/download?path=/Developer_Tools/Xcode_11.1/Xcode_11.1.xip

请注意,您应该使用Safari下载它,并且必须首先login to Apple developer portal

您可以在https://developer.apple.com/download/more上找到所有其他Xcode版本和其他资源链接(包括发行版和Beta版)

解决方法

这非常困难,但是可以解决。 将故事板 XIB 中的所有UITextView替换为纯代码版本。


请注意bug is found and fixed by Apple

Fixed

更早之前,the bug已确认Apple Staff edford

Confirmation


对于使用iOS 13.2并且不能再使用Xcode 11.1的用户:

  1. 将macOS更新到10.15.1或更高版本
  2. 安装Xcode 11.2.1或更高版本
  3. 现在应该可以在更新的设备上工作了。

对于拥有故事板的人:

  1. 子类UITextView
  2. 将其分配给所有UITextView个对象
  3. 别忘了更新可能在子类中丢失的所有属性更改。

对于那些习惯方法混乱(OBJC和动态行为)的人

前往 @aftab muhammed khan answer for Objective-C @MikRo answer for Swift adapted version

请不要再这样做:

即使这两个令人费解的解决方法都没有使用 Apple私有API ,它们也会在Apple will not accept builds with Xcode versions under 11.2.1中被拒绝在AppStore中!

再次:

Apple于2019年11月5日弃用Xcode 11.2

答案 1 :(得分:143)

祝贺

新版本的Xcode(11.2.1)现在可用,这是摆脱此问题的最佳方法。

解决方法

@Mojtaba Hosseini,我提出的解决方案来自于Stackoverflow的帮助和我的同伴开发人员的参与。您,我以及此处的所有其他开发人员已经知道,当Apple宣布新版本时,此问题将消失。

但要紧紧抓住一切

由于完全不涉及私有API,因此上述解决方案已被Apple Review接受。这种方法与

之类的创建属性非常相似。
  

@interface UITextView(布局)

  

UITextView + Layout.h

因此,在创建属性时,您将直接使用APPLE私有组件并根据您的需要或要求对其进行重新调制。

简单的例子是AMFNetworking类

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

希望我已经完成了指控

下面的回答只是我方面的一些帮助,使开发人员可以继续开发,因为我们最初建议开发人员回滚Xcode。再次下载8 GB Xcode是一种不好的做法,因为我们都知道新版本的Xcode即将发布。

虽然Xcode 11.2.1已修复该问题,但我为Xcode 11.2提供了一个解决方案,您可以摆脱该崩溃:

  

***由于未捕获的异常'NSInvalidUnarchiveOperationException'而终止应用程序,原因:'无法实例化名为_UITextLayoutView的类,因为未找到名为_UITextLayoutView的类;该类需要在源代码中定义或从库中链接(确保该类是正确目标的一部分)'

解决方案

转到“构建设置”搜索“ DEAD_CODE_STRIPPING”并将其设置为“否”

DEAD_CODE_STRIPPING = NO

然后

  

创建文件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
  

在应用程序委托中执行

#import "UITextViewWorkaround.h"

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

            [UITextViewWorkaround executeWorkaround];
    return yes;
    }

编译代码,您将拥有一个正在运行的应用程序:)

答案 2 :(得分:40)

此问题已在Xcode 11.2.1中修复。

编辑::此修补程序现已发布,您应切换到该Xcode版本并注释此替代方法。正如Mojtaba Hosseini在他的回答中提到的:

  

...这最后两个令人毛骨悚然的解决方法正在使用Apple私有API,并将被Apple审查拒绝!

在Apple发布此修复程序之前,这是继续开发和测试的好方法。


对于Xcode 11.2,基于Aftab Muhammed Khan的想法,并在John Nimis的帮助下,我仅测试了以下代码。

情节提要文件中无需更改!

编辑了我的AppDelegate.swift文件并添加了此类

//******************************************************************
// 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
           }
        }
    }
}

并在“ didFinishLaunchingWithOptions”的委托调用中调用解决方法

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()
}

答案 3 :(得分:34)

我已经将可汗的Obj-C解决方案调整为 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!)
            }
        }
    }

}

didFinishLaunchingWithOptionsAppDelegate的末尾调用它。

感谢@Aftab!

答案 4 :(得分:22)

更快的解决方法:

///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)
        }
    }
}

将此代码添加到某处,然后将所有故事板实例替换为FixedTextView

注意:您将丢失在情节提要中创建的所有属性。这可能会带来严重的影响(例如,代理设置,大小等)

答案 5 :(得分:18)

更新的解决方案: 更新到 Xcode 11.2.1 。我可以在iOS 11、12或13设备上使用它。

请参阅apple's documentation 此更新解决了一个严重问题,该问题可能导致使用UITextView的应用程序崩溃。

旧解决方案:https://developer.apple.com/download/more/下载了Xcode 11.1 从11.2切换回11.1可以解决崩溃问题。

对我来说,即使使用Xcode 11.2,当我将iPhone升级到13.2时,也可以解决崩溃问题。

答案 6 :(得分:17)

11.2.1 GM种子解决了这个问题

(并且可以用于发布到App Store)

转到https://developer.apple.com/download/。下载Xcode 11.2.1 GM种子

Release notes确认它已解决此错误:

enter image description here

答案 7 :(得分:13)

您可以从Apple开发人员网站下载最新的Xcode beta版本(11.2.1 GM)。

Here the direct link

Xcode 11.2.1 GM seed

答案 8 :(得分:12)

改善@garafajon的答案。对我来说,它在大多数情况下都有效。

///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)
        }
    }
}

答案 9 :(得分:8)

作为“快速”修复程序,您可以直接从代码中添加UITextView,而无需通过IB。至少对我有用。虽然从我的角度来看,最好回滚到以前的Xcode /等待新的Xcode。

答案 10 :(得分:6)

这是Xcode 11.2的错误。子类化的Textviews在未安装neweset iOS版本(13.2)的所有设备上崩溃。您最好不要使用该版本构建发行版。

您现在可以:

  • 将Xcode降级为11.1或
  • 将设备升级到iOS 13.2

答案 11 :(得分:6)

我使用了成功的解决方法,但这很痛苦。这是我遵循的过程:

  1. 在文本编辑器中打开XIB
  2. 找到有问题的TextView。就我而言:
<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>
  1. 请注意其id(在我的情况下为id="782-j1-88c"
  2. 按照上面的答案覆盖该类并重新创建选项(我的是Objective-C,抱歉):
@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;
}
  1. 请注意包含文本视图ID的约束,然后针对视图或视图控制器中的其他元素ID重新创建这些约束。就我而言:
- (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;
}

这样做可以为我解决此问题,而不会损失所需的功能。幸运的是,我只有一个UITextView可以替换。否则,这将变得站不住脚。

答案 12 :(得分:2)

我遇到了同样的问题,我刚刚将我的Xcode 11.2升级到11.2.1,效果很好。

升级后,我已经在iOs 13和iOS 12上进行了测试,并且运行良好。

答案 13 :(得分:0)

1。问题:

Xcode 11.2存在一个问题,其中包含UITextView的情节提要如果使用Xcode 11.2编译,则该应用程序将在iOS 13.2之前的OS版本上崩溃。

Xcode 11.2

选中此apple documentation

2。解决方案:

唯一的解决方案是将您的Xcode更新为11.2.111.3.

Xcode 11.2.1特别发布,以解决此崩溃问题。

enter image description here 选中此apple documentation.

3。建议:

我建议您使用最新版本的Xcode 11.3,因为它支持为iOS 13.3开发应用程序,并且还有许多新功能。 选中此apple documentation

答案 14 :(得分:-1)

此问题已在Xcode版本11.2.1中修复,并在发行说明中指出:

  

此更新解决了一个关键问题,该问题可能导致使用UITextView的应用程序崩溃

Xcode release notes screenshot