ios 从 Xcode 11.1 升级到 Xcode 11.2 后,应用因 _UITextLayoutView 而崩溃
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/58657087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
After upgrading to Xcode 11.2 from Xcode 11.1, app crashes due to _UITextLayoutView
提问by Sudhakar Tharigoppula
After upgrading to Xcode 11.2 from Xcode 11.1 my app crashes:
从 Xcode 11.1 升级到 Xcode 11.2 后,我的应用程序崩溃了:
*** 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)'
*** 由于未捕获的异常“NSInvalidUnarchiveOperationException”而终止应用程序,原因:“无法实例化名为 _UITextLayoutView 的类,因为未找到名为 _UITextLayoutView 的类;该类需要在源代码中定义或从库中链接(确保该类是正确目标的一部分)'
Why is this happening? How can I prevent this crash?
为什么会这样?我怎样才能防止这种崩溃?
采纳答案by aftab muhammed khan
Congratulation
祝贺
The New version of Xcode (11.2.1) is available now which is the best way to get rid off this issue.
新版本的 Xcode (11.2.1) 现已推出,这是摆脱此问题的最佳方式。
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.
@Mojtaba Hosseini 我提出的解决方案来自我身边的开发人员通过 StackOverflow 的帮助和参与。你、我和这里的所有其他开发人员已经知道,当 Apple 发布新版本时,这个问题就会消失。
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
上述解决方案绝对被 Apple Review 接受,因为根本不涉及私有 API。这种方法与创建属性非常相似
@interface UITextView (Layout)
@interface UITextView(布局)
Or
或者
UITextView+Layout.h
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.
因此,当您创建属性时,您将直接使用 APPLE 私有组件并根据您的依赖或要求对它们进行重新调制。
The Simple Example is AMFNetworking classes
简单的例子是 AMFNetworking 类
- (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.
下面的答案只是我这边的一些帮助,使开发人员能够继续开发,因为我们最初建议开发人员回滚 Xcode。再次下载 8 GB Xcode 是一个不好的做法,因为我们都知道新版本的 Xcode 即将发布。
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:
虽然它在 Xcode 11.2.1 中得到修复,但我为 Xcode 11.2 提供了一个解决方案,您可以通过它摆脱这个崩溃:
*** 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)'
*** 由于未捕获的异常“NSInvalidUnarchiveOperationException”而终止应用程序,原因:“无法实例化名为 _UITextLayoutView 的类,因为未找到名为 _UITextLayoutView 的类;该类需要在源代码中定义或从库中链接(确保该类是正确目标的一部分)'
SOLUTION
解决方案
Go to the Build Setting search for "DEAD_CODE_STRIPPING" and set it to NO
转到 Build Setting 搜索“DEAD_CODE_STRIPPING”并将其设置为 NO
DEAD_CODE_STRIPPING = NO
Then
然后
create files UITextViewWorkaround
创建文件 UITextViewWorkaround
UITextViewWorkaround.h
UITextViewWorkaround.h
#import <Foundation/Foundation.h>
@interface UITextViewWorkaround : NSObject
+ (void)executeWorkaround;
@end
UITextViewWorkaround.m
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 :)
编译代码,你将有一个正在运行的应用程序:)
回答by Mojtaba Hosseini
Update: Fixed!
更新:固定!
The ONLY Solution is to update
唯一的解决方案是更新
This bugis fixedin Xcode 11.2.1. So you can download and use it from here.
此错误已在 Xcode 11.2.1 中修复。所以你可以从这里下载和使用它。
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)
包含 UITextView 的 Storyboard 将不再导致应用在 iOS 13.2、tvOS 13.2 或 macOS 10.15.2 之前的操作系统版本上崩溃。(56808566, 56873523)
Xcode 11.2 is deprecated by Apple on November 5, 2019
Apple 于 2019 年 11 月 5 日弃用了 Xcode 11.2
if you ever try to submit your app that build with Xcode 11.2 to the AppStore, you will be rejected:
如果您尝试将使用 Xcode 11.2 构建的应用程序提交到 AppStore,您将被拒绝:
App Store Connect Operation Warning
App Store Connect 操作警告
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."
警告 ITMS-90703:“已弃用 Xcode 构建。由于已解决应用存档问题,我们已于 2019 年 11 月 5 日弃用 Xcode 11.2。下载 Xcode 11.2.1 或更新版本,重建您的应用程序并重新提交。”
So all workarounds done with the Xcode 11.2 is useless
所以用 Xcode 11.2 完成的所有变通方法都没有用
It's a bug for Xcode 11.2, and fixed in Xcode 11.2.1.
这是 Xcode 11.2 的一个错误,并在 Xcode 11.2.1 中修复。
Solution(s)
解决方案
Roll back to previous Xcode releaseversion from:Rollback is not an option anymore and AppStore will reject any build with Xcode below 11.2.1 take a look at this
回滚到以前的 Xcode发布版本:回滚不再是一个选项,AppStore 将拒绝任何 Xcode 低于 11.2.1 的构建,看看这个
Note thatyou should use Safari to download it and you must first login to Apple developer portal.
请注意,您应该使用 Safari 下载它,并且您必须先登录 Apple 开发者门户。
You can find all other Xcode versions and other resources link (including release and beta versions) here at https://developer.apple.com/download/more
您可以在https://developer.apple.com/download/more 上找到所有其他 Xcode 版本和其他资源链接(包括发行版和测试版)
The workaround
解决方法
This is very hard but working workaround.
Replace all UITextView
s in storyboards and Xibs with the pure codeversion.
这是非常困难但可行的解决方法。将storyboard和Xib 中的所有UITextView
s替换为纯代码版本。
Note thatthis bug is found and fixed by Apple
请注意,此错误已由 Apple 发现并修复
Also earlier, the bugwas confirmed by Apple Staff edford
同样早些时候,该错误已由Apple Staff edford确认
For those with iOS 13.2 and can not use Xcode 11.1 anymore:
对于使用 iOS 13.2 且无法再使用 Xcode 11.1 的用户:
- Update macOS to 10.15.1 or later
- Install Xcode 11.2.1 or later
- It should be work now on the updated device.
- 将 macOS 更新到 10.15.1 或更高版本
- 安装 Xcode 11.2.1 或更高版本
- 它现在应该可以在更新的设备上运行。
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.
- 子类
UITextView
- 将其分配给所有
UITextView
对象 - 不要忘记更新在子类化中可能丢失的任何属性更改。
For those comfortable with method swizzling (Objc and dynamic behavior)
对于那些习惯于方法混合(Objc 和动态行为)的人
Head to the @aftab muhammed khananswer for Objective-Cand @MikRoanswer for Swift adapted version
前往@aftab muhammed khan回答 Objective-C和@MikRo回答 Swift 改编版本
Just don't do it anymore:
只是不要再这样做了:
Even if these last two swizzling workarounds are not using Apple private API, they will be rejectedin AppStore because Apple will not accept builds with Xcode versions under 11.2.1!
即使最后两个 swizzling 的解决方法没有使用Apple 私有 API,它们也会在 AppStore 中被拒绝,因为Apple 不会接受 Xcode 版本低于 11.2.1 的构建!
And once again:
再一次:
Xcode 11.2 is deprecated by Apple on November 5, 2019
Apple 于 2019 年 11 月 5 日弃用了 Xcode 11.2
回答by Hardy_Germany
The issue was fixed in Xcode 11.2.1.
该问题已在 Xcode 11.2.1 中修复。
EDIT:As the fix is now released, you should switch to that Xcode version and comment outthis workaround. As Mojtaba Hosseini in his answer mentioned:
编辑:随着修复程序现已发布,您应该切换到该 Xcode 版本并注释掉此解决方法。正如 Mojtaba Hosseini 在他的回答中提到的:
... these last two swizzling workarounds are using Apple private API and will be reject from Apple review!
...这最后两个 swizzling 的解决方法是使用 Apple 私有 API,将被 Apple 拒绝!
For the time until the fix was released by Apple, this was a good workaround to continue developing and testing.
在 Apple 发布修复程序之前,这是继续开发和测试的一个很好的解决方法。
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.
对于 Xcode 11.2,基于 Aftab Muhammed Khan 的想法并在 John Nimis 的帮助下,我刚刚测试了以下代码。
No change in the storyboard files necessary!
无需更改故事板文件!
Edited my AppDelegate.swift file and added this class
编辑了我的 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
}
}
}
}
and within the delegate call for "didFinishLaunchingWithOptions" call the workaround
并在“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()
}
回答by MikRo
I've adapted khan's Obj-C solution to Swift:
我已经将可汗的 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!)
}
}
}
}
Call it at the end of didFinishLaunchingWithOptions
in AppDelegate
.
在didFinishLaunchingWithOptions
in的末尾调用它AppDelegate
。
Thanks @Aftab!
谢谢@Aftab!
回答by 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
.
将此代码添加到某处,然后将所有故事板实例替换为FixedTextView
.
Note: you will lose any attributes created in the storyboards. This could have serious implications (e.g. delegate setting, size, etc.)
注意:您将丢失故事板中创建的所有属性。这可能会产生严重的影响(例如委托设置、大小等)
回答by anoo_radha
Updated Solution:Update to Xcode 11.2.1. It works on iOS 11, 12 or 13 devices for me.
更新的解决方案:更新到Xcode 11.2.1。它适用于我的 iOS 11、12 或 13 设备。
Refer to apple's documentationThis update fixes a critical issue that could cause apps using UITextView to crash.
请参阅苹果的文档此更新修复了可能导致使用 UITextView 的应用程序崩溃的关键问题。
Old Solution:Downloaded Xcode 11.1 from https://developer.apple.com/download/more/Switching back from 11.2 to 11.1 fixed the crash.
旧解决方案:从https://developer.apple.com/download/more/下载 Xcode 11.1 从 11.2 切换回 11.1 修复了崩溃。
Also, for me even with Xcode 11.2, when I upgraded my iPhone to 13.2, that fixed the crash.
此外,对于我来说,即使使用 Xcode 11.2,当我将 iPhone 升级到 13.2 时,也解决了崩溃问题。
回答by craft
11.2.1 GM seed resolves this issue
11.2.1 转基因种子解决了这个问题
(and it can be used to publish to App Store)
(它可以用来发布到 App Store)
Go to https://developer.apple.com/download/. Download Xcode 11.2.1 GM seed
转到https://developer.apple.com/download/。下载 Xcode 11.2.1 GM 种子
Release notesconfirm it fixes this error:
发行说明确认它修复了这个错误:
回答by BossOz
You can go to download the last Xcode beta release (11.2.1 GM) from Apple developer website.
您可以从 Apple 开发者网站下载最新的 Xcode 测试版 (11.2.1 GM)。
回答by Awsom3D
Improving on @garafajon answer. For me it works in most cases.
改进@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)
}
}
}
回答by 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.
作为“快速”修复,您可以UITextView
直接从代码而不是通过 IB 添加。至少它对我有用。虽然从我的角度来看,最好回滚到以前的 Xcode/等待新的。