CryptoKit craches app on phones with iOS version below 13
Asked Answered
T

1

13

I've just started using CryptoKit to replace an old library. My plan was to replace it on phones running iOS 13 and above, and still use the old library on iOS versions below 13. However I can't get the app to even start in iOS 12.4 without crashing. I've removed everything else and only att these simple lines

if #available(iOS 13.0, *) {
    let nonce = try? AES.GCM.Nonce(data: iv)
}

The #available I thought was used to make sure that is was skipped if the condition wasn't fulfilled.

The stranger part is that the app crashes without me ever calling the meshing containing these lines, just having them somewhere in the app causes it to crash.

I have also imported CryptoKit at the top (with and without if canImport(CryptoKit)) and I have added @available(iOS 13.0, *) to the class.

Can anyone tell me the correct way to "exclude" the code, or whatever the correct way is, for lower iOS-versions. I guess it must be possible to use iOS 13-specific functionality while still maintaining backwards-compability through "old" methods?

Also, I'm using Xcode 11, but I guess that may be obvious.

And the error I'm getting is:

dyld: Library not loaded: /System/Library/Frameworks/CryptoKit.framework/CryptoKit

EDIT: Now I've also tried putting this around the entire class (including the import)

#if canImport(CryptoKit)
#endif

And also, I've notices that this line can exist without crashing the app:

let key = SymmetricKey(data: Data())

And symmetric key is also part of CryptoKit

EDIT 2: I've figured out that weak linking would solve the problem. I tried using one of the other new (iOS 13) frameworks, CryptoTokenKit, and had the same problem. But here I could add the framework and set the linking to optional, which solved the problem. However when adding frameworks to the app CryptoKit is not available for some reason.

Temporal answered 8/10, 2019 at 14:0 Comment(3)
How did you add the CryptoKit framework to your project? Did you add it as a dependency in the project settings?Leffen
I've not added it in any way, I just used import CryptoKit at the top of the file. CryptoKit is native from Apple since iOS 13. Do I still need to add it somehow?Temporal
I'm having the same problem. Did you find a way to solve this?Carmon
D
29

As pointed out here, Xcode is not properly adding CryptKit to the linked frameworks list. To solve this issue, you must manually add the -weak_framework flag in your app's target.

enter image description here

Durrell answered 16/11, 2019 at 12:17 Comment(1)
Linking CryptoKit manually also not working for me. Here is how I accessed the CryptoKit. Used a framework that required iOS 13 to run at the same time it use CryptoKit dyld: Library not loaded: /System/Library/Frameworks/CryptoKit.framework/CryptoKit Referenced from: /Users/****/Library/Developer/Xcode/DerivedData/some/Build/Products/Debug-iphonesimulator/A****.framework/A***** Reason: image not found.Dorothi

© 2022 - 2024 — McMap. All rights reserved.