Xamarin.Forms中使用SQLite.Net.Cipher

Xamarin.Forms中使用SQLite.Net.Cipher

需求是需要对Sqlite加密。

这里我选择了SQLite.Net.Cipher这个库来进行加密。

Github:https://github.com/has-taiar/SQLite.Net.Cipher

SQLite.Net.Platform.XamarinAndroidN:https://www.nuget.org/packages/SQLite.Net.Platform.XamarinAndroidN/

首先来看我们需要安装的库:

1. SQLite.Net.Cipher

2. SQLite.Net.Platform.XamarinAndroidN(从Nuget安装会失败,下载Nuget包,解压该包,再在Android项目用引用即可。)

3. Mono.Data.Sqlite.Portable (Android项目中安装)

为什么需要2和3两个库?

关于Mono.Data.Sqlite.Portable的使用:

官方文档这样说的:

https://developer.xamarin.com/releases/android/xamarin.android_6/xamarin.android_6.0/

Due to a change by Google, Android N will now only permit linking to NDK-provided native librarieslibsqlite.so is not an NDK-provided native library. Consequently, existing apps using e.g. Mono.Data.Sqlite.dll will crash when running on Android N. This may include other SQLite-using assemblies, not distributed with Xamarin.Android.

在andorid7.0后,不允许应用调用系统的sqlite,所以需要应用自己引入。

关于 SQLite.Net.Platform.XamarinAndroidN使用:

先看SQLite.Net.Cipher的Demo代码:

public class MyDatabase : SecureDatabase
{
    public MyDatabase(ISQLitePlatform platform, string dbfile, string someSalt): base(platform, dbfile, someSalt)
    {
    }
        
    protected override void CreateTables()
    {
        CreateTable<SampleUser>();
    }
}

MyDatabase在实例化的时候需要platform,这时后SQLite.Net.Platform.XamarinAndroidN就排上用场了。

在AndroidN即(7.0)以下版本设置 platform = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroid();

在AndroidN以上设置platform = new SQLite.Net.Platform.XamarinAndroid.SQLitePlatformAndroidN();

当然在iOS和UWP中和SQLite.Net.Platform.XamarinAndroidN这个库就无关了。

iOS中设置 platform = new SQLite.Net.Platform.XamarinIOS.SQLitePlatformIOS();

UWP中设置 platform = new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT();

这样就可以愉快的使用SQLite.Net.Cipher了。

 

posted @ 2018-06-03 15:15  Devin.Zhou  阅读(209)  评论(0编辑  收藏  举报