[英]How can I make a local storage storage file for mobile app
I'm building a Xamarin CrossPlatform app all of its database is in cloud api's, but I want a local storage file in mobile which stores all login credentials whenever someone is login.我正在构建一个 Xamarin CrossPlatform 应用程序,它的所有数据库都在云 API 中,但我想要一个本地存储文件,用于在有人登录时存储所有登录凭据。
Kindly help me through this!请帮助我解决这个问题!
If you want to just save the login credentials you should use Xamarin.Essentials Storage如果只想保存登录凭据,则应使用Xamarin.Essentials Storage
First, Add the Xamarin.Essentials NuGet package to each project.首先,将 Xamarin.Essentials NuGet 包添加到每个项目。
In Android在安卓中
protected override void OnCreate(Bundle savedInstanceState) {
//...
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState); // add this line to your code
//...
To handle runtime permissions on Android, Xamarin.Essentials must receive any OnRequestPermissionsResult.若要在 Android 上处理运行时权限,Xamarin.Essentials 必须接收任何 OnRequestPermissionsResult。 Add the following code to all Activity classes:将以下代码添加到所有 Activity 类:
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
And then to save:然后保存:
try
{
await SecureStorage.SetAsync("loginCredential", "loginCredentialValue");
}
catch (Exception ex)
{
// Possible that device doesn't support secure storage on device.
}
To retrieve:检索:
try
{
var loginCredential = await SecureStorage.GetAsync("loginCredential");
}
catch (Exception ex)
{
// Possible that device doesn't support secure storage on device.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.