2011/11/30更新

[XCODE] iPhone iOSのディレクトリ構成について学んだ

このエントリーをはてなブックマークに追加      

現在iPhoneアプリの開発奮闘中。今日は『iPhoneAppProgrammingGuide.pdf』を読んで、ファイルシステムについて学びました。学んだ事を書きたいと思います。

iPhoneアプリケーションは、サンドボックスという構造で各種ファイルを保持しています。フォルダによってiTunesでバックアップされるされないや、何を置くべきという指針があるようです。以下文章中にある引用は、全て以下ドキュメントからの引用です。iOS Application Programming Guide



サンボボックスとは

For security reasons, iOS restricts each application (including its preferences and data) to a unique location in the file system. This restriction is part of the security feature known as the application’s sandbox. The sandbox is a set of fine-grained controls limiting an application’s access to files, preferences, network resources, hardware, and so on. Each application has access to the contents of its own sandbox but cannot access other applications’ sandboxes.

[iOS Application Programming Guide p.14]

セキュリティ上の理由から、アプリケーションはそれぞれ一意の場所をシステム上に割り当てられる。これは、アプリケーションサンドボックスとして知られた手法である。サンドボックスは、ファイルアクセス、設定、ネットワークリソースやハードウエアリソースなど、動作に必要な権限をアプリケーションに一通り与える。アプリケーションは自身のサンドボックスにはアクセスできるが、他のアプリケーションのサンドボックスにはアクセスする事が出来ない。

アプリケーションを他のシステムやアプリから勝手に参照、更新などされるのは普通は困るので、サンドボックスはありがたい機能です。しかし、何かデータをアプリ間で連携したいという時はちょっと窮屈。ペーストボードやカスタムクエリなど限られた方法でデータをやりとりする必要があるみたい。まあ私みたいにセキュリティ勉強中であまり詳しくない人でもアプリを安全に開発できるような仕組みとは、ありがたいなぁ。



ディレクトリ構造

When an application is first installed on a device, the system creates the application’s home directory, sets up some key subdirectories, and sets up the security privileges for the sandbox. The path to home directory is of the following form:

/ApplicationRoot/ApplicationID/

[iOS Application Programming Guide p.16]

アプリケーションが最初にインストールされたときに、システムはそのアプリケーションのホームディレクトリ(やサブディレクトリ)をサンドボックス作成します。その作成するディレクトリは「/ApplicationRoot/ApplicationID/」です。

アプリケーションごとに、固有のディレクトリが作成されるんですね。


<Application_Home>/AppName.app

This is the bundle directory containing the application itself. Do not write anything to this directory. To prevent tampering, the bundle directory is signed at installation time. Writing to this directory changes the signature and prevents your application from launching again.

In iOS 2.1 and later, the contents of this directory are not backed up by iTunes. However, iTunes does perform an initial sync of any applications purchased from the App Store.

[iOS Application Programming Guide p.16]

このディレクトリはアプリケーション本体を含むバンドルです。だから、書き込んだり、許可無くいじってはだめ。OS2.1以降は、iTunesでのバックアップ対象外ディレクトリです。

アプリケーション本体があるディレクトリですね。うんうん、分かりやすい。



<Application_Home>/Documents/

Use this directory to store user documents and application data files. The contents of this directory can be made available to the user through file sharing, which is described in “Sharing Files with the User’s Desktop Computer” (page 18).

The contents of this directory are backed up by iTunes.

[iOS Application Programming Guide p.17]

ドキュメントやアプリケーションデータを保存するディレクトリ。このディレクトリ二存在するコンテンツは、ユーザーとのファイルシェアが行える。iTunesバックアップ対象ディレクトリ。

名前の通りで分かりやすいですね。ユーザーとファイルを連携するとは、iBooksやGoodReaderなどのアプリが行っている事でしょう。バックアップ対象という事で、もしiPhoneをバックアップから復元してもデータも復元できるという事で、大切なファイルはここに保存するのが良いという事でしょう。



<Application_Home>/Library/

This directory is the top-level directory for files that are not user data files. You typically put files in one of several standard subdirectories but you can also create custom subdirectories for files you want backed up but not exposed to the user. (For information on how to get references to the standard subdirectories, see “Getting Paths to Standard Application Directories” (page 50).) You should not use this directory for user data files.

The contents of this directory (with the exception of the Caches subdirectory) are backed up by iTunes.

[iOS Application Programming Guide p.17]

データファイルでないファイルを保存するためのトップディレクトリ。Libraryディレクトリからサブディレクトリを作成する事も可。ユーザーには見せないけど、バックアップ対象として保存しておきたいファイルはここに保存する。(Cachesディレクトリを除き)iTunesでのバックアップ対象となる。

アプリケーションの設定(制御)情報や、IDとPASSWORDなどの重要情報を保存するディレクトリという事でしょうか。このディレクトリを使うようなアプリはまだ開発していませんが、これから使ってみよっと。



<Application_Home>/Library/Preferences

This directory contains application-specific preference files. You should not create preference files directly but should instead use the NSUserDefaults class or CFPreferences API to get and set application preferences; see also “Adding the Settings Bundle” (page 78).

The contents of this directory are backed up by iTunes.

[iOS Application Programming Guide p.17]

アプリケーション設定情報ファイルを保存するディレクトリ。開発者は直接設定ファイルを作成するのではなく、NSUserDefaultsクラスやCFPreferencesAPIを使ってアプリケーションの設定をセットしたり読んだりする。iTunesでのバックアップ対象となる。

あっ、ここが設定情報を保存するディレクトリですね。

<Application_Home>/Library/Caches

Use this directory to write any application-specific support files that you want to persist between launches of the application or during application updates. Your application is generally responsible for adding and removing these files. It should also be able to recreate these files as needed because iTunes removes them during a full restoration of the device.

In iOS 2.2 and later, the contents of this directory are not backed up by iTunes.

[iOS Application Programming Guide p.17]

アプリケーション起動中やアップデート中に保持しておきたい、アプリ固有の設定を保存するためのディレクトリ。アプリケーション自身が、それらファイルを書いたり消したりする必要がある。iTunesはデバイスのフルレストアをする際にこれらファイルは消去してしまうので、必要に応じてファイルを再作成する。iOS2.2以降、iTunesでのバックアップ対象外ディレクトリ。

アプリ固有設定という事で、設定アプリで設定しないようなアプリケーション固有の設定を保持するためのファイルを保存するように使えば良いのでしょう。



<Application_Home>/tmp/

Use this directory to write temporary files that you do not need to persist between launches of your application. Your application 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 application is not running.)

In iOS 2.1 and later, the contents of this directory are not backed up by iTunes.

[iOS Application Programming Guide p.17]

その名前の通り、一時的に必要なファイルを保存するディレクトリ。アプリケーションは不要になれば、一時ファイルを削除する必要がある。またシステムも、アプリケーションが動作していないときに、長時間放置された一時ファイルを消す事がある。iOS2.1以降、iTunesでのバックアップ対象外ディレクトリ

名前の通りのディレクトリ名で分かりやすい。一時的なデータという事で、インターネットなどを利用してデータをアップロードする際に

ちょっとだけ保存しておきたいデータなどを置くために使うというイメージでしょうか。



iTunesによるバックアップ

iTunesにデバイスをつなぐと同期処理が始まりますが、その時の最初のステップで行っている(バックアップ取得中・・・)という処理。あれが、iTunesでのバックアップです。上記の「ディレクトリ構成」にてiTunesの同期対象と記載されたディレクトリにあるファイルがバックアップされルト言う事で、バックアップ対象とするかしないかを判断して適切なディレクトリに保存する事は大切な事のようです。



最後に

今日は、iOSにおけるディレクトリ構成について学びました。まだアプリ開発経験は浅いので使った事のないディレクトリなどもありましたが、包括的に学習できたという事で良かった。バックアップ対象有無という情報も大変学習になりました。

iOSについて学んだらまた書きたいと思います。







こんな記事もいかがですか?

RSS画像

もしご興味をお持ち頂けましたら、ぜひRSSへの登録をお願い致します。