iOS development Fmdb Import sqlcipher encrypted database

Source: Internet
Author: User

Engineering use Fmdb do database operations, the latter to encrypt the database, there are two ways:

1. Encrypt the contents of the database, encrypt it when it is stored, and use it to decrypt it.

2. Encrypt the database file directly.

Here I chose the second kind, the reason does not elaborate, oneself decides.


After Baidu, decided to use Sqlcipher, and Fmdb is support Sqlcipher, the official configuration description address http://sqlcipher.net/ios-tutorial/, and then try, configuration failed, and make the app pack a lot of trillion, so

Not recommended.

Later, the official found this in Fmdb:


That is, you can use Cocoapods to install support Sqlcipher encrypted database Fmdb package, if you have not used Cocoapods installed third-party open Source Library, here is recommended a tutorial, very detailed

Thin: Http://code4app.com/article/cocoapods-install-usage

All we have to do here is to install the Fmdb/sqlcipher with Cocoapods in our own project.

The simple thing is (the next step is to install the cocoapods on your PC, so be sure to follow the link above to configure it, otherwise the following steps will fail):

1. Create a new file in your own project directory podfile, open terminal input command line $ VI podfile, then write to pod ' Fmdb/sqlcipher ' and save exit

2. Enter $pod update directly on the command line.

Then wait until it appears:

Analyzing dependenciesDownloading dependenciesInstalling FMDB (2.3)Generating Pods projectIntegrating client project[!] From now on use `SqliteHasCcode.xcworkspace`.

A hint like this appears, indicating that the installation was successful, and then the directory would look like this:

After entering the project all points to this. Xcworkspace, it will look like this after entering:


Then add the encryption code:



I am directly in FMDATABASE.M Riga, this see personal code need add place plus on OK, generally after the database open with this setkey method.


Then compile, run, no accident is OK, with a third-party database management tools to verify encryption, when the import of a dense database, will be directly prompt ' data is encrypted ' similar words, if the command line Sqlite3 open, it can be opened, but when using. Table to view, is not visible to any of the tables, into:



But in fact, this database has the contents of the table, which prevents the data leakage of our database.


Next is another point:

After installing with cocoapods, in fact, we added another project to our project pods, personal feeling very uncomfortable, so decided to integrate it into my project.

First look at my move after the success of another project structure (the project was originally Fmdb project files, but can not be encrypted, my goal is to make Fmdb to support encryption, should be a lot of people engineering is to this scene):



Not as Cocoapods installed, there are two Proj, from the file, only a lot of sqlite3.h and SQLITE3.M, and then Fmdb files or you originally used. This is what we are going to achieve, okay, it's over.


Begin:

Take a closer look at the engineering structure after installation with Cocoapods:



In fact, and we usually use fmdb compared, just a lot of sqlcipher this piece of support, then we just have to find a way to move this piece to us before we have fmdb the project can be


Sqlcipher directory, there are two subdirectories, support files is a configuration file, common is the main code file.

Common two files, drag directly into our project, then the key is the configuration here:

1.pods-sqlcipher.xcconfig:

See, Other_cflags and Other_ldflags respectively correspond to the project build settings:

Other C Flags:



This is primarily a compilation of macros.


and other Linker Flags

-framework and Security are two, separate, don't make one.

According to the configuration in the Pods-sqlcipher.xcconfig configuration file to change these two places, such as.

2.pods-sqlcipher-private.xcconfig



There's really nothing to change about the configuration here.

Gcc_preprocessor_definitions should be to set some of the compiler's what, and later set to Cocoapods = 1, we are to remove the Cocoapod installation form, so I feel this configuration we do not need to, so I also have no control.

Header_search_paths, without configuration, because we later only added Sqlite3.h, and SQLITE3.M in our engineering directory, then as long as the inclusion of #import "Sqlite3.h" OK

Other_cflags and Other_ldflags and Pods_root are actually used in the pods-sqlcipher.xcconfig of these several settings, so Pods-sqlcipher.xcconfig set up here is good.


Because after installing with Cocoapods, the configuration here is to make the Sqlcipher engineering and the main project some configuration unified, so after removing the form of cocoapods installation, do not care about these.


In fact, the simple steps are:

1. In your Fmdb project, join Sqlite3.h and SQLITE3.M

2. Modify two configurations in the project build setting

One is the other C flags, as in the figure above the other C flags

One is other link flags, into that other lilnk flags

Then the Fmdatabase Setkey method can be used, after opening the database, call this method, the database file is encrypted OK.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.