Article
Performing One-Time Setup for Your App
Ensure that your app environment is properly configured.
--这些一次设置是用来确保你正确地配置了app的环境。
Overview --概览
When the user launches your app for the first time, you might want to prepare your app environment by performing some one-time tasks. For example, you might want to:
--当用户第一次启动应用程序时,您可能希望通过执行一些一次性任务来准备应用程序环境。例如,您可能希望这样做:
-
Download required data from your server.
--从服务器下载所需的数据。 -
Copy document templates or modifiable data files from your app bundle to a writable directory.
--将文档模板或可修改的数据文件从应用程序包复制到可写目录中。 -
Configure default preferences for the user.
--为用户配置默认首选项。 -
Set up user accounts or gather other required data.
--建立用户帐户或收集其他需要的数据。
Perform any one-time tasks in your app delegate’s application(_:willFinishLaunchingWithOptions:)
or application(_:didFinishLaunchingWithOptions:)
method. Never block the app’s main thread for tasks that do not require user input. Instead, start tasks asynchronously using a dispatch queue, and let them run in the background while your app finishes launching. For tasks that require user input, make all changes to your user interface in the application(_: didFinishLaunchingWithOptions:)
method.
--在app delegate的application(_:willFinishLaunchingWithOptions:)
or application(_: didFinishLaunchingWithOptions:)
方法中执行一次性的任务。对于不是用户输入的任务,绝对不要阻塞app的主线程。而是,使用”分派队列 “异步地启动任务,并让这些任务在app完成启动期后 在后台运行。对于需要用户输入的任务,则所有修改用户界面的操作都在application(_: didFinishLaunchingWithOptions:)
方法进行。
Install Files in the Proper Locations --在适当的位置安装或存储文件
Your app has its own container directory for storing files, and you should always place app-specific files in the ~/Library
subdirectory. Specifically, store your files in the following ~/Library
subdirectories:
--您的app有自己的容器目录来存储文件,您应该将特定的app文件放在~/Library子目录中。具体来说,将文件存储在以下~/Library子目录中:
-
~/Library/Application Support/
—Store app-specific files that you want backed up with the user’s other content. (You can create custom subdirectories here as needed.) Use this directory for data files, configuration files, document templates, and so on.
--~/Library/Application Support/ ——存储那些要与用户的其他内容一起备份的app文件。(您可以根据需要在此处创建自定义子目录。)将此目录用于数据文件、配置文件、文档模板等 -
~/Library/Caches/
—Store temporary data files that can be easily regenerated or downloaded.
--~/Library/Caches/——存储可以轻松重新生成或下载的临时数据文件。
To obtain a URL for one of the directories in your app’s container, use the urls(for:in:)
method of FileManager
.
--要获取app容器中的一个目录的URL,请使用FileManager
的urls(for:in:)
方法。
Listing 1
Getting the location of app-specific directories --获取特定的app的目录的位置
let appSupportURL = FileManager.default.urls(for:
.applicationSupportDirectory, in: .userDomainMask)
let cachesURL = FileManager.default.urls(for:
.cachesDirectory, in: .userDomainMask)
Place any temporary files in your app’s tmp/
directory. Temporary files might include compressed files that you intend to delete once their contents have been extracted and installed elsewhere. Retrieve the URL for your app’s temporary directory using the temporaryDirectory
method of FileManager
.
--将任何临时文件都放在app的tmp/目录中。临时文件可能包含压缩文件,这些压缩文件一旦被解压并安装到其他位置,您就会删除这些压缩文件。你可以使用FileManager
的temporaryDirectory
方法来检索app临时目录的URL。
See Also
Launch Time
Learn the order in which your custom code is executed at launch time.
Preserving Your App's UI Across Launches
Return your app to its previous state after it is terminated by the system.