Appcelerator iOS应用程序:如何从共享扩展中读取钥匙串值?



我有一个测试Appcelerator iOS专用应用程序(以简化事情(。你可以在这个回购中获得整个(非常小的应用程序(

此应用程序使用Ti.Identity模块从Appcelerator端在钥匙串中存储一个值,没有问题。问题是我无法从 Swift 读取该值。

在钥匙串中保存某些内容的代码,alloy.js

var Identity = require('ti.identity');
// Create a keychain item
var keychainItem = Identity.createKeychainItem({
identifier: 'mypassword',
accessGroup: 'group.test.projects'
});

keychainItem.addEventListener('save', function(e) {
Ti.API.info("Saved!!! ");
keychainItem.addEventListener('read', function(e) {
Ti.API.info("Read!!!");
if (e.success) {
Ti.API.info(JSON.stringify(e, null, 4));
} else {
Ti.API.info("Error" + e);
}
});
keychainItem.read();
});
// Write to the keychain
keychainItem.save('s3cr3t_p4$$w0rd');

这有效,因为保存后我使用该密钥打印钥匙串内的内容,并正确打印s3cr3t_p4$$w0rd.

现在,要从共享扩展中读取该扩展,我已将扩展添加到项目中。tiapp.xml的相关部分:

<ios>
...
<entitlements>
<dict>
<key>com.apple.security.application-groups</key>
<array>
<string>group.test.projects</string>
</array>
<key>keychain-access-groups</key>
<array>
<string>$(AppIdentifierPrefix)keychain.test.projects</string>
</array>
</dict>
</entitlements>
<extensions>
<extension projectPath="extensions/TestKeychain.xcodeproj">
<target name="ShareExtension">
<provisioning-profiles>
<devices/>
</provisioning-profiles>
</target>
</extension>
</extensions>

应用程序是<id>com.testapp</id>

应用扩展中的授权文件为:

<dict>
<key>com.apple.security.application-groups</key>
<array>
<string>group.test.projects</string>
</array>
<key>keychain-access-groups</key>
<array>
<string>$(AppIdentifierPrefix)keychain.test.projects</string>
</array>
</dict>

为了从 Swift 中阅读它,我尝试:

let domain = "group.test.projects"
let login = "mypassword"
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)
self.textView.text = "Reading something"
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
}
} else {
self.textView.text = "Error"
}

我也使用了Ti.Identity的这个钥匙串包装器。没有成功。我总是得到-25300, /* The specified item could not be found in the keychain. */

Swift 方面出了什么问题?

这可能不是一个完整的答案,因为您的问题很长,但有一个示例应用程序声称可以做到这一点。

这是他的例子。

最新更新