简体   繁体   English

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

[英]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 oftiapp.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 .这可能不是一个完整的答案,因为您的问题很长,但有一个示例应用程序声称这样做

Here's his example . 这是他的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用钥匙串在 iOS 扩展程序及其包含的应用程序之间共享? - Share between an iOS extension and its containing app with the keychain? 无法通过共享扩展代码访问iOS中的离子插件钥匙串键值对。 - Not able to access ionic plugin keychain key value pair in iOS from share extension code. 如何在WatchKit扩展和iPhone应用程序之间共享钥匙串数据 - How to share keychain data between a WatchKit extension and an iPhone app IOS Share扩展:如何阅读笔记帖子 - IOS Share extension: how to read from notes posts 访问iOS 8共享扩展中的共享钥匙串返回nil - Access to shared keychain in iOS 8 share extension returns nil 在iOS中使用Mail扩展名共享Mail App中的附件 - Share attachment from Mail App with Share extension in iOS 从iOS键盘扩展名访问钥匙串 - Access the keychain from iOS keyboard extension WatchKit扩展:如何从主机iOS应用读取数据? - WatchKit extension: how to read data from host iOS app? App Extension iOS9:如何从共享视图向我的应用程序发送字符串/ URL? - App Extension iOS9: How can I send a string / URL from the share view to my the app? Appcelerator iTunes Sync已死。 我现在如何分享iOS应用构建? - Appcelerator iTunes Sync is dead. How do I share iOS app builds now?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM