iosionic-frameworknsuserdefaultskeychainios8-share-extension

Not able to access ionic plugin keychain key value pair in iOS from share extension code.


I have created an hybrid app where we upload files using ionic.

For iOS we created a share extension to upload the file and right now trying to pass credentials from ionic application to shared extension in iOS.

I used iOS keychain mechanism as it is supported by ionic native and iOS both.

Share extension I have written in swift and key chain implementation here is using a wrapper like ionic plugin in ionic application.

I am able to save information in ionic plugin and fetch in ionic itself. Similar is the case with shared extension.

although I am not able to get the information which is been shared to shared extension from ionic.

this is what i am using in view controller in shared extension.

let keychainWrapper = KeychainWrapper.init(serviceName: "xxxx.xxx.xxx" )
let str = keychainWrapper.accessGroup
        let keyBool = keychainWrapper.accessibilityOfKey("key")
        let value = keychainWrapper.string(forKey: "key", withAccessibility:KeychainItemAccessibility.whenPasscodeSetThisDeviceOnly)
        let serviceName = keychainWrapper.serviceName

in ionic :

 this.keychain.set(key, value,false).then(() => {
      this.keychain.get(key)
        .then(value => console.log('Got value', value))
        .catch(err => console.error('Error getting', err));
    })
    .catch(err => console.error('Error setting', err));

Is there any other way share information like NSUserdefaults etc which is supported by ionic and iOS swift both?

This tutorial is what I have been following.


Solution

  • In ionic I had to use app preferences plugin. this gave me access to the user defaults although to fetch the group had to change the ionic plugin code so that I could share user defaults saved in ionic and fetch the same thing in app extension.

    Plugin https://ionicframework.com/docs/native/app-preferences/

    //ionic code
    let dict12 = this.appPref.suite('group.xxxxxxx')
            this.appPref.store('group.pmshare', 'keyNa', 'valueNa')
            console.log('dict: ' + dict12)
            this.appPref.fetch('group.pmshare', 'keyNa').then(str => {
            console.log('str: ' + str)
            })
    
    
    // shared extension code
    var defaults = UserDefaults.init(suiteName: "group.xxxxxxx")
            print(defaults?.string(forKey: "keyNa"))
            defaults?.set("fromExtValue", forKey: "fromExt")
    

    Always remember to create a group under target->app groups->ON

    Same group will be used to share data between both the applications.

    This can be actually used for Inter Application Communication in iOS. When the app is developed using Ionic.