Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CryptoKit craches app on phones with iOS version below 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.

like image 970
Sebastian Avatar asked Oct 08 '19 14:10

Sebastian


1 Answers

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

like image 135
kikettas Avatar answered Oct 31 '22 18:10

kikettas