flutter_secure_storage icon indicating copy to clipboard operation
flutter_secure_storage copied to clipboard

Export iOS native methods

Open Albert221 opened this issue 4 years ago • 7 comments

Hi! I'm using your package, but my use case also needs to read the saved data on the Swift native part of the application. Do you think you could export the read (most important), write and other methods so that they could be simply called from the Swift code?

Albert221 avatar Apr 15 '21 10:04 Albert221

Did you find a way to accomplish this yet?

OGmetamonkey avatar Jul 01 '21 18:07 OGmetamonkey

I'm interested in reading directly in Swift as well.

FranWeidt avatar Jul 05 '21 23:07 FranWeidt

@Albert221 did you have any luck already? I need exactly your use-case.

And did you maybe already manage to do this for Android?

rubenaster avatar Nov 27 '21 14:11 rubenaster

I was using that for iOS only app, so no experience with Android side here.

I just dig through the flutter_secure_storage native source code and reused what's there. It's more or less like this:

private func getAuthToken() -> String? {
    let tokenKey = "some-token-key"

    let query = [
        kSecClass as String: kSecClassGenericPassword,
        kSecAttrAccessGroup as String: self.getAppGroup(),
        kSecAttrAccount as String: tokenKey,
        kSecReturnData as String: kCFBooleanTrue!
    ] as [String: Any]

    var dataRef: AnyObject? = nil

    let status: OSStatus = SecItemCopyMatching(query as CFDictionary, &dataRef)
    if status == noErr {
        if let data = dataRef as? Data? {
            if data == nil {
                return nil
            }

            let result = String(data: data!, encoding: String.Encoding.utf8)!
            return result
        }
    }

    return nil
}

Albert221 avatar Dec 01 '21 10:12 Albert221

Okay. Does that mean that you migrated most of the code to Swift by yourself?

rubenaster avatar Dec 02 '21 07:12 rubenaster

Sorry, what do you mean?

Albert221 avatar Dec 02 '21 07:12 Albert221

Ah, now I understand. All variables like kSecClassGenericPassword etc. are already existent since they're part of the key chain! Im' new to that iOS and Swift stuff, sorry :)

May I ask how you implemented the self.getAppGroup? From what I read there's no dynamic way to read the app group at runtime, at least not in a release version. Did you just hard code it?

rubenaster avatar Dec 03 '21 09:12 rubenaster

Fixes in 6.1.0-beta.1

juliansteenbakker avatar Sep 30 '22 21:09 juliansteenbakker