How to store the sqlite3 database file in the Library directory of an app? - iphone

Pretty much everyone and every tutorial seems to store it's private app data in the documents directory.
But in my image editing app I want the user to be able to use iTunes file sharing. The problem: It exposes everything in documents to the user, and he can accidentally delete important private app data folders like the SQLite database.
Apple recommends this:
If you do not want files to be shared
with the user, put them in your
application’s Library directory. If
none of the standard Library
subdirectories are appropriate, create
a private directory inside the Library
directory and give it the name of your
application’s bundle ID. You can then
use that directory to store any files
that are private to your application.
I have never heard of that Library directory. Now the question is: How can I access this directory? Where is it? Will it be backed up, just like the documents directory when the user syncs with iTunes?

Use NSLibraryDirectory in lieu of NSDocumentDirectory (in those examples you speak of) to get the library directory and then create a directory in there using NSFileManager.
Read this for more information on the application directory structure. The Library directory is backed up except the Caches directory.

Related

When to use assets in Flutter

What is the best way to use assets in Flutter , for example if i have a file for app configuration , should I store the file by getting the app directory using the path_provider plugin -without using assets- and store it ?, or should I add the file to my program folder -add the file to my assets- ?
the same question if I have a small Sqlite database.
and which of these methods is faster , and which is more secure ?
Assets are files that you add to your app during development. You can load them with rootBundle.load() or rootBundle.loadString() but you cannot modify or delete them.
In the app's directory you can store any files that your app downloads or generates from the internet while running. These files can then be opened, deleted, modified, etc. To access your app directory you need the package path_provider, which tells you the path to your app folder.
A sqlite database is normally stored in the app directory. An example package would be here sqflite.
For speed and security I can't make a difference. An app directory is designed so that only the app can access it. Assets are a part of the app, the application file can theoretically be unpacked by anyone. Therefore I would at least not store secret things in the assets.
Well, if by app configuration you mean the user's settings you can use Sqlite, SharedPreferences or Hive (Hive shows a benchmark that says that it is faster than SharedPreferences).
I believe that assets folder is used to store some common files for the app, like images, icons, fonts, etc. And I think that isn't recommended to store files with some kind of config file, mainly with critical info about the app configuration.

Whats the difference between saving content at document directory or temp folder?

I want to save multipel photos in my application, so that application run in background.
So what is the main difference between saving photos in doc dir or temp path.
And suggest me which is best way to save photos
Document Dir
Temp Folder
NSUserDefaults
Thanks in advance
Here is a reference: File System Programming Guide.
Temp folder:
Use this directory to write temporary files that do not need to
persist between launches of your app. Your app should remove files
from this directory when it determines they are no longer needed. (The
system may also purge lingering files from this directory when your
app is not running.)
Documents folder:
Use this directory to store critical user documents and app data
files. Critical data is any data that cannot be recreated by your app,
such as user-generated content.
Usually, I put files in temporary folder only when I cache something and I don't care if these files will be deleted. If I want to be sure these files should live long life, I put them to documents folder.
The main difference is the path: <sandbox>/Documents or <sandbox>/tmp.
Some more differences:
The Documents directory can be accessed via iTunes if your app has file sharing enabled.
The contents of the tmp directory is volatile, the OS is free to purge it in order to save space.
About NSUserDefaults: that's something completely different, it's a mechanism which stores app-specific configuration data in property lists, I can't imagine how and/or why you would use it for storing images.

How to copy a file from Documents Directory to application bundle?

Hi in my app launch i will invoke a web-service call and i will download some zip files now i need to save those files permanently in my application bundle so that when i launch my app for the second time i will just look for update if there is no update i will just use my previously saved files, so i thought to make use of document directory and get the zip file then unzip it and save it in application bundle, here my problem is how to save a file from DocumentsDirectory to my Application Bundle?
and does my approach is right & efficient way or do i need to follow some other ways to achieve this? Any help is thankful in advance.
You can't write to your application's bundle - it is readonly, only the documents directory is readwrite.
You don't need to move the files, as the documents directory provides permanent storage.
The resources which are modified at runtime should be part of Documents directory. iPhone application creates a sandbox environment which is signed. If you try to modify any of the bundle resource or try to add/remove the resource, it will not allow. It works fine with simulator but not with device.
So you should store your response to the documents directory and not to the bundle.

Is it possible to limit iOS file sharing functionality to a subfolder in the documents directory?

My image editing app is saving some important data in the documents directory. In a tutorial I was reading this:
iTunes will then display anything you
save to the Documents directory in
your app to the user, when they go to
the “Apps” page in iTunes and scroll
to the bottom:
I have a subfolder called userImages and it would be clever to restrict file sharing only to that folder and not to everything in documents. Otherwise the user would accidently (or on purpose) mess around with files that the app depends on to work properly. This would be bad.
Is there a way to restrict it to a subdirectory in documents?
No, what you should do instead is store anything you do not want users seeing in the "Library" directory for the app. Check here for a list of places you can store data:
How can I get a writable path on the iPhone?

iOS Documents with UIFileSharingEnabled

My iPhone app uses a sqlite3 database for storing the data that it generates. This data needs to persist, and I need to make sure the user cannot damage it. I also want to allow the user to export data as text/csv files, these would be shared through iTunes. I plan to put the database in the Library folder, write the CSV files to the Doucments folder, and turn on UIFileSharingEnabled.
Is there anyway I can stop the user from adding files to my apps Documents directory through iTunes?
If not, it is acceptable practice to have my app delete any files that it did not create?
Is there anyway I can stop the user from adding files to my apps Documents directory through iTunes?
No
If not, it is acceptable practice to have my app delete any files that it did not create?
Of course not. Why do you want to do this anyway? If you use the documents directory for exporting only, simply ignore anything that's inside.