返回

解码苹果审核崩溃日志:了解SecItemDelete在iOS15.1中的崩溃原理

IOS

前言

作为移动开发者,我们常常会面临来自苹果审核团队的反馈,而其中不乏崩溃日志。解读和分析这些崩溃日志对于改进应用程序的稳定性和用户体验至关重要。本文将深入探讨一种常见的崩溃类型——SecItemDelete崩溃,尤其是在iOS 15.1中。我们将探究其成因,并提供切实可行的解决方案和预防措施,帮助开发者避免此类崩溃。

崩溃日志分析

SecItemDelete崩溃通常发生在调用SecItemDelete函数时,该函数用于从钥匙串中删除一项或多项密钥。在iOS 15.1中,此函数引入了一个错误,导致在某些情况下删除密钥失败,从而引发崩溃。

以下是崩溃日志中的典型堆栈跟踪:

*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not delete item in keychain'
*** First throw call stack:
(
    0   CoreFoundation                      0x1b1c84a18 __exceptionPreprocess + 188
    1   libobjc.A.dylib                     0x1b151b928 objc_exception_throw + 56
    2   CoreFoundation                      0x1b1c88a28 +[NSException raise:format:] + 192
    3   Security                              0x1b2f07404 SecItemDelete + 352
    4   MyApp                                0x1063b3e24 _T06MyAppC09MyKeychainC11deleteEntryyySiSgF + 152
    5   MyApp                                0x1063b4378 _T06MyAppC09MyKeychainC14deleteAllEntriesyyF + 60
    6   MyApp                                0x1063b3c1c _T06MyAppC09MyKeychainC10resetKeysyyF + 108
    7   MyApp                                0x1063b3044 _T06MyAppC16MyAppViewControllerC04testyyF + 68
    8   MyApp                                0x1063ab344 _T06MyAppC16MyAppViewControllerC24viewDidLoadFromRootViewF0yyF + 60
    9   UIKitCore                            0x2344b3c34 -[UIViewController loadViewIfRequired] + 1360
    10  UIKitCore                            0x2344b5f2c -[UIViewController __viewWillAppear:] + 140
    11  UIKitCore                            0x2344c8648 -[UINavigationController _startCustomTransition:] + 784
    12  UIKitCore                            0x234520b50 -[UINavigationController _showViewController:transition:shouldAnimate:] + 416
    13  UIKitCore                            0x234522b2c -[UINavigationController _pushViewController:needsKeyboard:transition:shouldAnimate:] + 816
    14  UIKitCore                            0x2344e7e34 -[UINavigationController pushViewController:animated:] + 656
    15  MyApp                                0x1063a9c3c _T06MyAppC16MyAppViewControllerC06launchyyyF + 84
    16  UIKitCore                            0x234567428 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1684
    17  UIKitCore                            0x23456b20c -[UIApplication workspaceDidEndTransaction:] + 220
    18  FrontBoardServices                  0x1b180984c __31-[FBSceneImpl _updateWithUpdatedScenes:withTransitionContext:completion:]_block_invoke_2 + 60
    19  FrontBoardServices                  0x1b18094e0 __31-[FBSceneImpl _updateWithUpdatedScenes:withTransitionContext:completion:]_block_invoke + 444
    20  FrontBoardServices                  0x1b1807d24 -[FBSceneImpl updateWithUpdatedScenes:withTransitionContext:completion:] + 612
    21  UIKitCore                            0x23456ab70 -[UIApplication workspaceDidEndTransaction:] + 272
    22  UIKitCore                            0x234567558 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1808
    23  UIKitCore                            0x234565f9c -[UIApplication _handleApplicationActivationWithScene:transitionContext:completion:] + 412
    24  UIKitCore                            0x234550888 -[UIApplication activateWithScene:transitionContext:completion:] + 324
    25  UIKitCore                            0x23453a5b4 -[UIApplication _activateWithScene:transitionContext:completion:] + 144
    26  UIKitCore                            0x2345394a0 -[UIApplication _activateScene:withTransitionContext:completion:] + 252
    27  UIKitCore                            0x2345571f4 -[UIApplication activateScene:withTransitionContext:completion:] + 496
    28  UIKitCore                            0x2345565b4 -[UIApplication activateScene:withTransitionContext:completion:] + 332
    29  FrontBoardServices                  0x1b1816e18 __86-[FBSceneInterface _activateWithTransitionContext:completion:]_block_invoke.1730 + 236
    30  UIKitCore                            0x2344e02f0 -[UIWindowScene _activateWithTransitionContext:completion:] + 224
    31  UIKitCore                            0x2344e0174 -[UIWindowScene activateWithTransitionContext:completion:] + 224
    32  UIKitCore                            0x2344df30c -[UIApplication workspaceDidEndTransaction:] + 840
    33  UIKitCore                            0x234567558 -[UIApplication _runWithMainScene:transitionContext:completion:] + 1808
    34  UIKitCore                            0x234568390 -[UIApplication run] + 640
    35  UIKitCore                            0x234478718 -[UIApplication _run] + 544
    36  UIKitCore                            0x2344b9bd8 UIApplicationMain + 164
    37  MyApp                                0x106391408 main + 72
    38  libdyld.dylib                        0x1b14685e0 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

从堆栈跟踪中可以看出,崩溃发生在SecItemDelete函数中,该函数用于从钥匙串中删除密钥。在iOS 15.1中,此函数存在一个错误,导致在某些情况下删除密钥失败。

解决方案

为了解决此崩溃问题,开发者可以采用以下解决方案:

  • 更新到iOS 15.2或更高版本: Apple已在iOS 15.2中修复了SecItemDelete中的错误。建议开发者更新到最新版本的iOS以避免此崩溃。
  • 检查钥匙串权限: 确保应用程序具有访问钥匙串的权限。在Info.plist文件中添加以下代码段:
<key>KeychainAccessGroups</key>
<array>
    <string>$(AppIdentifierPrefix)com.example.mykeychain</string>
</array>
  • 避免在并发线程上删除密钥: SecItemDelete函数不应在并发线程上调用。确保在主线程上调用此函数。
  • 使用替代方法: 如果可能,考虑使用其他方法从钥匙串中删除密钥。例如,可以使用SecItemUpdate函数将密钥的值设置为无效。

预防措施

除了实施解决方案外,开发者还可以采取以下预防措施来避免SecItemDelete崩溃:

  • 定期测试应用程序: 定期使用各种测试用例测试应用程序,以确保其在不同场景下正常运行。
  • 使用调试工具: 利用调试工具(