iOS崩溃错误分析记录

记录各种疑难杂症

1. Terminating app due to uncaught exception 'UIApplicationInvalidInterfaceOrientation', reason: 'Supported orientations has no common orientation with the application, and [UIAlertController shouldAutorotate] is returning YES'

由于APP只支持竖屏,而在其中某个页面中单独设置支持横屏,在此横屏页面下,如果触发了某个业务弹出了UIAlertController,则会导致崩溃。网上有说写一个UIAlertController分类,在其中重写shouldAutorotate方法处理,然而写了之后并不会执行。
当然不仅仅是UIAlertController这一种情况,UIImagePickerViewController这种系统的东西都有可能触发。
解决方案:在AppDelegate中实现以下方法根据实际业务判断处理

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    UIViewController *rootVC = window.rootViewController;
    if (rootVC.presentedViewController) {
        UIViewController *theVC = rootVC.presentedViewController;
        while (theVC.presentedViewController) {
            theVC = theVC.presentedViewController;
        }
        if ([theVC shouldAutorotate]) {
            return UIInterfaceOrientationMaskAll;
        }
        return theVC.supportedInterfaceOrientations;
    }
    return rootVC.supportedInterfaceOrientations;
}

2. iOS 16实时活动API,在iPhone上没问题,在M1上打开闪退。

闪退堆栈

* thread #26, queue = 'com.apple.root.user-initiated-qos.cooperative', stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
  * frame #0: 0x00000001a37e442c libswiftCore.dylib`swift::ResolveAsSymbolicReference::operator()(swift::Demangle::__runtime::SymbolicReferenceKind, swift::Demangle::__runtime::Directness, int, void const*) + 176
    frame #1: 0x00000001a380ee3c libswiftCore.dylib`swift::Demangle::__runtime::Demangler::demangleSymbolicReference(unsigned char) + 228
    frame #2: 0x00000001a380b1cc libswiftCore.dylib`swift::Demangle::__runtime::Demangler::demangleType(__swift::__runtime::llvm::StringRef, std::__1::function<swift::Demangle::__runtime::Node* (swift::Demangle::__runtime::SymbolicReferenceKind, swift::Demangle::__runtime::Directness, int, void const*)>) + 264
    frame #3: 0x00000001a37ec3e4 libswiftCore.dylib`swift_getTypeByMangledNameImpl(swift::MetadataRequest, __swift::__runtime::llvm::StringRef, void const* const*, std::__1::function<swift::TargetMetadata<swift::InProcess> const* (unsigned int, unsigned int)>, std::__1::function<swift::TargetWitnessTable<swift::InProcess> const* (swift::TargetMetadata<swift::InProcess> const*, unsigned int)>) + 504
    frame #4: 0x00000001a37e7a3c libswiftCore.dylib`swift_getTypeByMangledName + 832
    frame #5: 0x00000001a37e7d4c libswiftCore.dylib`swift_getTypeByMangledNameInContext + 164
    frame #6: 0x0000000103d2b1c4 XXXX`__swift_instantiateConcreteTypeFromMangledName at <compiler-generated>:0
    frame #7: 0x0000000103d2b408 XXXX`partial apply for closure #1 in static Bridge.stop(_:) at <compiler-generated>:0
    frame #8: 0x0000000103d2b784 XXXX`thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out A) at <compiler-generated>:0
    frame #9: 0x0000000103d2c1ac XXXX`partial apply for thunk for @escaping @callee_guaranteed @Sendable @async () -> (@out A) at <compiler-generated>:0

定位代码

Activity<WidgetAttributes>.activities

只要写了这个API,即便是不做其他事情,也会闪退,最终只能判断在iPad上不调用。
一开始无法定位,bugly上只有__swift_instantiateConcreteTypeFromMangledName,通过这个搜索到的都是说OC与Swift类型转换的问题,完全误导了排查方向。

3. dyld[41865]: Symbol not found: OBJC_CLASS$_Reachability

OC项目中使用到了Swift库,此库依赖了ReachabilitySwift库。同时之前也存在了ReachabilityOC版本库,编译时是两个名字的库未发生问题,运行时由于有相同类名导致找不到对应路径。只需要删除其中一个库便可。

4. DYLD, Library not loaded

Exception Type:  EXC_CRASH (SIGABRT)
Exception Codes: 0x0000000000000000, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Termination Description: DYLD, Library not loaded: /System/Library/Frameworks/SwiftUI.framework/SwiftUI | Referenced from: /var/containers/Bundle/Application/D24B9375-7B33-4578-A87F-69A052688796/XXX.app/XXX | Reason: image not found
Triggered by Thread:  0

这个问题发生的场景是Xcode 15打包,在iOS 13(不含)之前系统无法启动APP。由于提示原因是SwiftUI.framework找不到,而且项目确实引用了这个库,就针对引用的项目都做了适配。奇怪的是后来没有引用这个库的APP也无法打开,为了研究这个原因,新建项目测试,最终发现OC主项目中只要包含了一个Swift文件,那么编译的包就会包含一堆Swift库。

    /System/Library/Frameworks/CoreTelephony.framework/CoreTelephony (compatibility version 1.0.0, current version 0.0.0)
    /System/Library/Frameworks/Foundation.framework/Foundation (compatibility version 300.0.0, current version 2202.0.0)
    /System/Library/Frameworks/UIKit.framework/UIKit (compatibility version 1.0.0, current version 7209.1.102)
    @rpath/YBKit.framework/YBKit (compatibility version 1.0.0, current version 1.0.0)
    /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1336.62.1)
    /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation (compatibility version 150.0.0, current version 2202.0.0)
    /System/Library/Frameworks/SwiftUI.framework/SwiftUI (compatibility version 1.0.0, current version 5.2.12)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1600.157.0)
    /usr/lib/swift/libswiftCore.dylib (compatibility version 1.0.0, current version 5.9.2)
    /usr/lib/swift/libswiftCoreFoundation.dylib (compatibility version 1.0.0, current version 120.100.0, weak)
    /usr/lib/swift/libswiftCoreImage.dylib (compatibility version 1.0.0, current version 2.0.0, weak)
    /usr/lib/swift/libswiftDarwin.dylib (compatibility version 1.0.0, current version 0.0.0, weak)
    /usr/lib/swift/libswiftDataDetection.dylib (compatibility version 1.0.0, current version 757.6.0, weak)
    /usr/lib/swift/libswiftDispatch.dylib (compatibility version 1.0.0, current version 34.0.2, weak)
    /usr/lib/swift/libswiftFileProvider.dylib (compatibility version 1.0.0, current version 1703.62.4, weak)
    /usr/lib/swift/libswiftMetal.dylib (compatibility version 1.0.0, current version 341.35.0, weak)
    /usr/lib/swift/libswiftOSLog.dylib (compatibility version 1.0.0, current version 4.0.0, weak)
    /usr/lib/swift/libswiftObjectiveC.dylib (compatibility version 1.0.0, current version 8.0.0)
    /usr/lib/swift/libswiftQuartzCore.dylib (compatibility version 1.0.0, current version 3.0.0, weak)
    /usr/lib/swift/libswiftUniformTypeIdentifiers.dylib (compatibility version 1.0.0, current version 794.2.2, weak)
    /usr/lib/swift/libswiftos.dylib (compatibility version 1.0.0, current version 1040.0.0, weak)
    /usr/lib/swift/libswiftFoundation.dylib (compatibility version 1.0.0, current version 1.0.0, weak)
    /usr/lib/swift/libswiftUIKit.dylib (compatibility version 1.0.0, current version 1.0.0, weak)
    /usr/lib/swift/libswiftCoreGraphics.dylib (compatibility version 1.0.0, current version 120.100.0, weak)

解决方法有两种:

1. Other Linker Flags下面添加两行
-weak_framework
"SwiftUI"

2. Link Binary With Libraries 中添加对应的 framework
在 Status 中选择为 Optional

个人倾向于第一种方案,因为第二种方案项目中确实没有使用这个库。

5. OC与Swift混编闪退情况

① Swift.*._unconditionallyBridgeFromObjectiveC

* thread #1, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
    frame #0: 0x00000001e5236198 libsystem_kernel.dylib`__pthread_kill + 8
    frame #1: 0x00000001f47c15f8 libsystem_pthread.dylib`pthread_kill + 208
    frame #2: 0x00000001b08804b8 libsystem_c.dylib`abort + 124
    frame #3: 0x00000001a3bd8998 libswiftCore.dylib`swift::fatalErrorv(unsigned int, char const*, char*) + 132
    frame #4: 0x00000001a3bd89b4 libswiftCore.dylib`swift::fatalError(unsigned int, char const*, ...) + 28
    frame #5: 0x00000001a3bd06ec libswiftCore.dylib`swift::swift_dynamicCastFailure(void const*, char const*, void const*, char const*, char const*) + 76
    frame #6: 0x00000001a3bd0764 libswiftCore.dylib`swift::swift_dynamicCastFailure(swift::TargetMetadata<swift::InProcess> const*, swift::TargetMetadata<swift::InProcess> const*, char const*) + 120
    frame #7: 0x00000001a3bd46ac libswiftCore.dylib`swift_dynamicCast + 280
    frame #8: 0x00000001a3f56b18 Foundation`generic partial specialization <Signature = @escaping @convention(thin) <τ_0_0, τ_0_1 where τ_0_0 == Swift.AnyObject> (@guaranteed Swift.Array<Swift.AnyObject>) -> (@owned Swift.Array<τ_0_1>)> of Swift._arrayForceCast<τ_0_0, τ_0_1>(Swift.Array<τ_0_0>) -> Swift.Array<τ_0_1> + 336
    frame #9: 0x00000001a3f87d50 Foundation`static Swift.Array._unconditionallyBridgeFromObjectiveC(Swift.Optional<__C.NSArray>) -> Swift.Array<τ_0_0> + 260
    frame #10: 0x00000001002b95e0 TestBugly`@objc static Test.array(_:) at <compiler-generated>:0
  * frame #11: 0x00000001002b82a8 TestBugly`-[ViewController crash:](self=0x0000000129f0c620, _cmd="crash:", sender=0x0000000129e0f2b0) at ViewController.m:102:5

此情况是由于OC调用Swift方法传入参数为nil或集合类内部元素类型不匹配

Example:
func url(_ url: URL) {}
[Test url:nil];

func array(_ array: [String]) {}
[Test array:@[@1]];
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,108评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,699评论 1 296
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,812评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,236评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,583评论 3 288
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,739评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,957评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,704评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,447评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,643评论 2 249
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,133评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,486评论 3 256
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,151评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,108评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,889评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,782评论 2 277
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,681评论 2 272

推荐阅读更多精彩内容