[英]Appcelerator iOS app: how to read Keychain value from Share Extension?
I have a test Appcelerator iOS-only App (to simplify things).我有一个测试 Appcelerator iOS-only 应用程序(以简化事情)。 You can get the whole (really small app) in this repo
你可以在这个repo 中获得整个(非常小的应用程序)
This app stores a value in the Keychain from Appcelerator side, without problems, usingTi.Identity
module .这个应用程序使用
Ti.Identity
模块从 Appcelerator 端在 Keychain 中存储一个值,没有问题。 The problem is that I can't read that value from Swift.问题是我无法从 Swift 中读取该值。
Code to save something in the keychain, inside alloy.js
:将一些东西保存在钥匙串中的代码,在
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');
This works, as after saving I print waht's inside the keychain with that key, and prints correctly s3cr3t_p4$$w0rd
.这是有效的,因为在保存后我用那个钥匙在钥匙串内打印
s3cr3t_p4$$w0rd
,并正确打印s3cr3t_p4$$w0rd
。
Now to read that from a Share Extension, I've added the extension to the project.现在要从共享扩展中读取该内容,我已将扩展添加到项目中。 Relevant parts of
tiapp.xml
: 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>
App is is <id>com.testapp</id>
应用程序是
<id>com.testapp</id>
The entitlement file in the app extension is:应用扩展中的授权文件是:
<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>
To read it from Swift I've tried to:要从 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"
}
Also I've used this Keychain Wrapper from Ti.Identity.我还使用了 Ti.Identity 的这个Keychain Wrapper 。 Without success.
没有成功。 I always get
-25300, /* The specified item could not be found in the keychain. */
我总是得到
-25300, /* The specified item could not be found in the keychain. */
-25300, /* The specified item could not be found in the keychain. */
What's wrong in the Swift side of things? Swift 方面有什么问题?
This might not be a complete answer as your question is quite long, but there's an example app that claims to do this .这可能不是一个完整的答案,因为您的问题很长,但有一个示例应用程序声称这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.