...other code
this.saveUserDataToSharedStorage('userData');
......
async saveUserDataToSharedStorage(data) {
try {
await SharedGroupPreferences.setItem(
'savedData',
data,
Config.APP_GROUP_IDENTIFIER_FOR_IOS,
);
this.loadUsernameFromSharedStorage();
} catch (errorCode) {
// errorCode 0 = There is no suite with that name
console.log(errorCode);
}
}
async loadUsernameFromSharedStorage() {
try {
const loadedData = await SharedGroupPreferences.getItem(
'savedData',
Config.APP_GROUP_IDENTIFIER_FOR_IOS,
);
console.log('loadedData===', loadedData);
console.log('loadedData===', 'userData');
} catch (errorCode) {
// errorCode 0 = no group name exists. You probably need to setup your Xcode Project properly.
// errorCode 1 = there is no value for that key
console.log(errorCode);
}
}
ios 原生去获取
if let userData = userDefaults?.value(forKey: "savedData") as? String {
let string = "普通string"
print("userData===", userData)
print("普通string===", string)
}
RN 端去存储
ios 原生去获取
userData=== "userData"(理论上应该打印的结果是:userData,然而它的结果是:"userData",多了一对符号) 普通string=== 普通string
我现在必须多写如下代码:
let token = tokenStr.trimmingCharacters(in: CharacterSet(charactersIn: "\""))
,否则我就拿不到正确的token