Reputation: 834
I understand I can share data between my share extension and its containing app by enabling app groups and using NSUserDefaults (see Sharing data between an iOS 8 share extension and main app).
However, the data I am storing is sensitive, so I hoped to use the keychain. So the user would enter account information in the containing app, and then the share extension would read that data to perform the intended sharing action.
Does anyone know if this is possible? My first crack at it suggests that the extension and the containing app have separate keychains (saving the data with a key in the containing app returns null when attempting to return data for that key in the extension).
Thanks!
P.S. Using Lockbox for Keychain access, but I could ditch it if it's too much of an abstraction to make it work. https://github.com/granoff/Lockbox
Upvotes: 29
Views: 18725
Reputation: 564
I enabled the app groups functionality for both the app and the target of the Share Extension.
Simply go to Singing&Capabilities and add the "App Group" Capability for both targets. Then you should create once a group id identifier (It's called "Create a new container") for example let's create group.lolrandomname
. Then you should select/enable this app group id in both targets.
Now in code, you must now add this attribute every time you want to use the shared keychain :
kSecAttrAccessGroup: "group.lolrandomname",
And it works correctly : I can access to same stored items from the App and from my Share Extension :)
Here is an example of storing something in the shared Keychain (all identical except one attribute in query) :
let myAwesomeString = "hello world"
let query = [kSecClass: kSecClassGenericPassword,
kSecAttrAccount: "com.myapp.an-awesome-string",
kSecAttrAccessGroup: "group.lolrandomname", // <<-- this!
kSecAttrAccessible: kSecAttrAccessibleWhenUnlocked,
kSecValueData: myAwesomeString.data(using: .utf8)!] as [String: Any]
let status = SecItemAdd(query as CFDictionary, nil)
guard status == errSecSuccess else {
print(status.humanReadable)
throw KeyStoreError("Unable to store item: \(status.humanReadable)")
}
For reading and deleting please see SecItemCopyMatching
and SecItemDelete
, but don't forget, as I said, to put the kSecAttrAccessGroup
key in the query
!
Upvotes: 3
Reputation: 4447
To make the Keychain shared in Xcode 8.
1) In your App target in Capabilities find and turn on "Keychain Sharing", add a Keychain Group key (a reverse-domain style string like com.myappdomain.myappname)
2) Do exactly the same for the extension target. Make sure the Keychain Group key is the same for both - the app and the extension.
Add and retrieve data from Keychain in your usual way, no special changes required in the code. For example, here's how I put data into Keychain in the main app (a little old-fashioned but still works in Swift 3):
let login = loginString
let domain = domainString
let passwordData: Data = passwordString.data(using: String.Encoding.utf8, allowLossyConversion: false)!
let keychainQuery: [NSString: NSObject] = [
kSecClass: kSecClassGenericPassword,
kSecAttrAccount: login as NSObject, // login and domain strings help identify
kSecAttrService: domain as NSObject, // the required record in the Keychain
kSecValueData: passwordData as NSObject]
SecItemDelete(keychainQuery as CFDictionary) //Deletes the item just in case it already exists
let keychainSaveStatus: OSStatus = SecItemAdd(keychainQuery as CFDictionary, nil)
And then retrieve it in the extension:
let keychainQuery: [NSString: NSObject] = [
kSecClass: kSecClassGenericPassword,
kSecAttrAccount: login as NSObject,
kSecAttrService: domain as NSObject,
kSecReturnData: kCFBooleanTrue,
kSecMatchLimit: kSecMatchLimitOne]
var rawResult: AnyObject?
let keychain_get_status: OSStatus = SecItemCopyMatching(keychainQuery as CFDictionary, &rawResult)
if (keychain_get_status == errSecSuccess) {
if let retrievedData = rawResult as? Data,
let password = String(data: retrievedData, encoding: String.Encoding.utf8) {
// "password" contains the password string now
}
}
Note that you will still need to pass "login" and "domain" over to the extension in order to identify the correct record. This can be done via NSUserDefaults. See this answer on how to do this.
Upvotes: 30
Reputation: 834
This can be done. It is a combination of creating a framework to do the Keychain access, and turning on "Activate Keychain Sharing" under "Capabilities". This link told me what I needed to know: http://swiftandpainless.com/ios8-share-extension-with-a-shared-keychain/
Upvotes: 9
Reputation: 1378
Using the standard Objective-C KeychainItemWrapper class and with an entry of #import "KeychainItemWrapper.h" in the bridging header:
func btnSaveAction() {
let appGroupID = "group.com.yourcompany.appid"
let keychain = KeychainItemWrapper(identifier: "Password", accessGroup:appGroupID)
keychain.setObject(self.txtfldPassword.text!, forKey:kSecValueData)
keychain.setObject(self.txtfldEmail.text!, forKey:kSecAttrAccount)
}
On the Watch extension side (Swift):
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
let appGroupID = "group.com.yourcompany.appid"
let keychain = KeychainItemWrapper(identifier: "Password", accessGroup:appGroupID)
println(keychain.objectForKey(kSecAttrAccount))
println(keychain.objectForKey(kSecValueData))
}
In Objective C, watchkit extension:
NSString *appGroupID = @"group.com.yourcompany.appid";
KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"Password" accessGroup:appGroupID];
[keychain setObject:(__bridge id)(kSecAttrAccessibleWhenUnlocked) forKey:(__bridge id)(kSecAttrAccessible)];
NSLog(@"account = %@", [keychain objectForKey:(__bridge id)(kSecAttrAccount)]);
NSLog(@"password =%@", [keychain objectForKey:(__bridge id)(kSecValueData)]);
Don't forget to turn on "Keychain Sharing" under "Capabilities" for both the phone app and watch kit extension for same keychain group: "group.com.yourcompany.appid"
Upvotes: 0
Reputation: 110
Use KeychainItemWrapper Class from the following link and pass your group identifier as accessgroup.
Upvotes: -1