# FileManager

{% tabs %}
{% tab title="⭐️ 重點" %}
{% hint style="success" %}
document directory vs. bundle resources

* <mark style="color:red;">**document directory**</mark>: user contents, <mark style="color:green;">**read/write**</mark>.
* <mark style="color:orange;">**bundle resources**</mark>: app files, <mark style="color:red;">**readonly**</mark>.
{% endhint %}

{% hint style="info" %}
* **Apps** on iOS are constrained to a **sandbox**, they’ve got **no access** to **system files**.&#x20;
* **Files** on iOS have a **path**, but you usually only work with a [url](url "mention") object that contains that path.
{% endhint %}

{% hint style="warning" %}
all iOS apps are _**sandboxed**_, which means they run in their own container with a **hard to guess directory name**. As a result, we can’t (and shouldn’t try to) guess the directory where our app is installed, and instead need to rely on Apple’s **API** for finding our app’s documents directory.

👉 Paul ⟩ [Writing data to the documents directory](https://www.hackingwithswift.com/books/ios-swiftui/writing-data-to-the-documents-directory)
{% endhint %}
{% endtab %}

{% tab title="🖥️ 影片" %}
{% embed url="https://youtu.be/pUAcMruIBGg" %}
iOS Academy ⟩ FileManager Swift 5 Tutorial
{% endembed %}

{% embed url="https://youtu.be/Yiq-hdhLzVM" %}
Swiftful Thinking ⟩ How to Use FileManager in Swift
{% endembed %}

{% embed url="https://youtu.be/p4exolZztJc" %}
Pursuit Core iOS ⟩ FileManager and Documents Directory, Part 1
{% endembed %}
{% endtab %}

{% tab title="圖解" %}
📗 參考： [File System Programming Guide](https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW2)

```
read: 👁️ , write: ➕(new), ➖(delete), 📝(edit), 📁(new subfolder)

---------------------------------------------------------------------
directory          👁️➕➖📝 📁        description
---------------------------------------------------------------------
<AppName>.app      ✅❌❌❌ ❌    app’s bundle (app's files & resources)
Documents          ✅✅✅✅ ✅    user files
  └ Inbox          ✅❌✅❌ ❌    files app was asked to open
Library            ✅✅✅✅ ✅    any non-user files
  ├ Caches                      data used to improve performance
  └ Application Support         files app uses to run
tmp                ✅✅✅✅ ✅    temporary files
---------------------------------------------------------------------
```

![An iOS app operating within its own sandbox directory](https://1830103165-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-M5-JmwCZMKh_d7RfBaN%2Fuploads%2FIbi1s32dj2lqIueESQhi%2Fios_app_layout_2x.png?alt=media\&token=853c22c2-71fb-4a91-988a-d7086fb22056)
{% endtab %}

{% tab title="📘 手冊" %}
* [Foundation](https://developer.apple.com/documentation/foundation) ⟩ [File System](https://developer.apple.com/documentation/foundation/file_system) ⟩ [FileManager](https://developer.apple.com/documentation/foundation/filemanager) ⟩&#x20;
  * [default](https://developer.apple.com/documentation/foundation/filemanager/1409234-default)
  * [urls(for:in:)](https://developer.apple.com/documentation/foundation/filemanager/1407726-urls) - locate common **directories**.
{% endtab %}

{% tab title="📗 參考" %}
* [ ] Apple Archive ⟩&#x20;
  * [ ] [File System Programming Guide](https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/FileSystemOverview/FileSystemOverview.html#//apple_ref/doc/uid/TP40010672-CH2-SW2) ⭐️
  * [ ] [File System Advanced Programming Topics](https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemAdvancedPT/Introduction/Introduction.html#//apple_ref/doc/uid/TP40010765-CH1-SW1)
* [ ] Swift Playgrounds ⟩ Image Gallery ([Data Model: Gathering Image URLs](https://developer.apple.com/tutorials/sample-apps/imagegallery#Data-Model%3A-Gathering-Image-URLs))
* [ ] AppyPie ⟩ [Working with Files on iOS with Swift](https://www.appypie.com/filemanager-files-swift-how-to/) ⭐️
* [ ] Sundell ⟩ [Working with files and folders in Swift](https://www.swiftbysundell.com/articles/working-with-files-and-folders-in-swift/)
* [ ] Pursuit Core iOS ⟩ [Persisting Custom Objects to Documents Directory](https://github.com/joinpursuit/Pursuit-Core-iOS/blob/master/persistence-animation-autolayout/filemanager-persistence/README.md)
{% endtab %}

{% tab title="🗣 討論" %}
* [In iOS apps , what's the difference between the main bundle and the documents directory?](https://stackoverflow.com/a/25471152/5409815)
* [The file couldn’t be opened because you don’t have permission](https://stackoverflow.com/questions/60397003/swift-the-file-couldn-t-be-opened-because-you-don-t-have-permission-to-view-it)
{% endtab %}

{% tab title="👥 相關" %}
* app resources are stored in [bundle](bundle "mention").
* [file-system](../../../../file-system "mention")
{% endtab %}
{% endtabs %}
