我正在尝试在OS X上使用rubyMotion从钥匙串中检索密码

我尝试了这个:

#   passsword_data_pointer=Pointer.new(:object) #works but empty password
#   password_data_pointer=Pointer.new('^') #makes ruby crash and complain 'Can't find pointer description for type '^'
    password_data=NSMutableData.new #works but empty password

    password_length = Pointer.new('I')
    result=SecKeychainFindGenericPassword (
                                           nil,
                                           "some_service_string".length,
                                           "some_service_string",
                                           "some_username_string".length,
                                           "some_username_string",
                                           password_length,
                                           password_data_pointer,#or password_data.bytes
                                           nil
                                           )

#    password_string=NSMutableData.dataWithBytes(password_data.bytes, length:password_length[0])
    password_string=NSMutableData.dataWithBytes(password_data_pointer, length:password_length[0])

    p password_string


无论我做什么,都无法检索密码。

请帮忙 ;搜索了很长时间,Internet上到处都是macruby或cocoa或c的例子,但是没有关于rubymotion的内容。

最佳答案

我对SecKeychainFindGenericPassword不太熟悉,但是我知道您需要设置适当的权利才能使用RubyMotion Project Management Guide中讨论的钥匙串。

因此,请确保您的Rakefile中包含以下行:

app.entitlements['keychain-access-groups'] = [
  app.seed_id + '.' + app.identifier
]


如果您想要一个更好的钥匙串接口,我可以使用SSKeychain可可包装纸,可以通过Cocoapods插入。

在您的Gemfile中:

gem 'cocoapods',        '~> 0.23.0'
gem 'motion-cocoapods', '~> 1.3.6'


同样在Rakefile中:

app.pods do
  pod 'SSKeychain', '~> 1.2.0'
end


这是我用于通过SSKeychain存储和检索敏感数据的包装器的简化版本:

class CredentialStore
  SERVICE = 'YOUR_APP_NAME'

  def set_secure_value(value, for_key: key)
    if value
      SSKeychain.setPassword(value, forService: SERVICE, account: key)
    else
      SSKeychain.deletePasswordForService(SERVICE, account: key)
    end
  end

  def secure_value_for_key(key)
    SSKeychain.passwordForService(SERVICE, account: key)
  end
end


如果您还有其他问题,请告诉我。祝好运!

关于macos - RubyMotion:SecKeychainFindGenericPassword&找不到类型'^'的指针描述,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18241574/

10-12 05:40