我们有一个以本机react-native-keychain使用(like in this issue)库的React-native应用程序:
import * as Keychain from 'react-native-keychain';
try {
Keychain.setInternetCredentials(
'device-id',
'RANDOMDATA',
DeviceInfo.getUniqueID().toString(),
{ accessGroup: 'org.reactjs.native.example.myreactapp'})
.then(() => console.log('Done'))
} catch (err) {
console.log("error save", err)
}
我希望能够在迅速制作的今天的小部件中检索此数据,我需要知道该存储密钥。 下面是我应该在小部件上找回它的方法:
let itemKey = "device-id"
let keychainAccessGroupName = "3P********.org.reactjs.native.example.myreactapp"
let queryLoad: [String: AnyObject] = [
kSecClass as String: kSecClassGenericPassword,
kSecAttrAccount as String: itemKey as AnyObject,
kSecReturnData as String: kCFBooleanTrue,
kSecMatchLimit as String: kSecMatchLimitOne,
kSecAttrAccessGroup as String: keychainAccessGroupName as AnyObject
]
var result: AnyObject?
let resultCodeLoad = withUnsafeMutablePointer(to: &result) {
SecItemCopyMatching(queryLoad as CFDictionary, UnsafeMutablePointer($0))
}
if resultCodeLoad == noErr {
if let result = result as? Data,
let keyValue = NSString(data: result,
encoding: String.Encoding.utf8.rawValue) as String? {
// Found successfully
print("device-id : ", keyValue)
}
} else {
print("Error loading from Keychain: \(resultCodeLoad)")
}
下面的代码来自this blog
但是它不起作用。您可能已经注意到,我需要将设备ID传递给小部件,因为与Android不同,UUID经常更改,并且我们在每个部件上使用不同的ID,这会阻止小部件正常工作
答案 0 :(得分:0)
在APP中更改此代码:
{ accessGroup: 'org.reactjs.native.example.myreactapp'})
到
{ accessGroup: '3P********.org.reactjs.native.example.myreactapp'})