iOS App Programming Guide => State Preserve/Restore & Resources

本文介绍了应用程序如何在进入后台时保存其状态,并在重新启动时恢复这些状态。主要讨论了应用代理、视图控制器和自定义视图三个层面的状态保存,并详细解释了状态保存与恢复的过程。

State Preservation and Restoration

There are three places where you have to think about state preservation in your app:

  • Your app delegate object, which manages the app’s top-level state

  • Your app’s view controller objects, which manage the overall state for your app’s user interface

  • Your app’s custom views, which might have some custom data that needs to be preserved

The Preservation and Restoration Process

State preservation occurs when your app moves to the background. During the restoration process, UIKit uses the preserved data to reconstitute your interface. The creation of actual objects is handled by your code. After those objects are created, UIKit uses the on-disk data to restore the objects to their previous state.

  • During preservation, your app is responsible for:

    • Telling UIKit that it supports state preservation.

    • Telling UIKit which view controllers and views should be preserved.

    • Encoding relevant data for any preserved objects.

  • During restoration, your app is responsible for:

    • Telling UIKit that it supports state restoration.

    • Providing (or creating) the objects that are requested by UIKit.

    • Decoding the state of your preserved objects and using it to return the object to its previous state.

UIKit preserves only those objects that have a restoration identifier. A restoration identifier is a string that identifies the view or view controller to UIKit and your app.

During the preservation process, UIKit walks your app’s view controller hierarchy and preserves all objects that have a restoration identifier.

For each view controller you choose to preserve, you also need to decide on how you want to restore it later. UIKit offers two ways to recreate objects. You can let your app delegate recreate it or you can assign a restoration class to the view controller and let that class recreate it. A restoration class implements theUIViewControllerRestoration protocol and is responsible for finding or creating a designated object at restore time:

  • If the view controller is always loaded from your app’s main storyboard file at launch time, do not assign a restoration class. Instead, let your app delegate find the object or take advantage of UIKit’s support for implicitly finding restored objects.

  • For view controllers that are not loaded from your main storyboard file at launch time, assign a restoration class. The simplest option is to make each view controller its own restoration class.

Flow of the Preservation Process

Flow of the Restoration Process

App-Related Resources

App Store Required Resources

  • Your app must have an Info.plist file. This file contains information that the system needs to interact with your app. Xcode creates a version of this file automatically but most apps need to modify this file in some way. 

  • Your app’s Info.plist file must include the UIRequiredDeviceCapabilities key. The App Store uses this key to determine whether or not a user can run your app on a specific device. 

  • You must include one or more icons in your app bundle. The system uses these icons when presenting your app on the device’s home screen. 

  • Your app must include at least one image to be displayed while your app is launching. The system displays this image to provide the user with immediate feedback that your app is launching.

App Icons

Icon Dimension for iOS 7 & iOS 6 devices   ---   Table 5-2 & 5-3

Regardless of how many different icons your app has, you specify them using theCFBundleIcons key in the Info.plist file. The value of that key is an array of strings, each of which contains the filename of one of your icons. The filenames can be anything you want, but all image files must be in the PNG format and must reside in the top level of your app bundle. (Avoid using interlaced PNGs.) When the system needs an icon, it choose the image file whose size most closely matches the intended usage.

For apps that run on devices with Retina displays, two versions of each icon should be provided, with the second one being a high-resolution version of the original. The names of the two icons should be the same except for the inclusion of the string@2x in the filename of the high-resolution image.

App Launch (Default) Images

Every app must provide at least one launch image. This image is typically in a file namedDefault.png that displays your app’s initial screen in a portrait orientation.

Use Asset Catelog to manage App Icon & Launch Images   ---  Xcode Overview

The Settings Bundle

Apps that want to display preferences in the Settings app must include a Settings bundle resource. A Settings bundle is a specially formatted bundle that sits at the top of your app’s bundle directory and contains the data needed to display your app’s preferences.

Preferences and Settings Programming Guide.

Localized Resource Files

For information about the internationalization and localization process, see Internationalization Programming Topics. For information about the proper way to use resource files in your app, see Resource Programming Guide.

Loading Resources Into Your App

Resource management is broken down basically into two steps:
  1. Locate the resource.

  2. Load the resource.

  3. Use the resource.

To locate resources, you use an NSBundle object. A bundle object understands the structure of your app’s bundle and knows how to locate resources inside it. Bundle objects even use the current language settings to choose an appropriately localized version of the resource. The pathForResource:ofType: method is one of several NSBundle methods that you can use to retrieve the location of resource files.

Once you have the location of a resource file, you have to decide the most appropriate way to load it into memory. Common resource types usually have a corresponding class that you use to load the resource:

  • To load view controllers (and their corresponding views) from a storyboard, use theUIStoryboard class.

  • To load an image, use the methods of the UIImage class.

  • To load string resources, use the NSLocalizedString and related macros defined in Foundation framework.

  • To load the contents of a property list, use the dictionaryWithContentsOfURL: method of NSDictionary, or use the NSPropertyListSerialization class.

  • To load binary data files, use the methods of the NSData class.

  • To load audio and video resources, use the classes of the Assets Library, Media Player, or AV Foundation frameworks.

NSString* imagePath = [[NSBundle mainBundle] pathForResource:@"sun" ofType:@"png"];

UIImage* sunImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
源码直接下载地址: https://pan.quark.cn/s/d280357b18e5 在网页构建领域中,HTML5被视为当代网页工程的基础规范,其问世显著增强了页面的视觉表现力与用户互动性。本工程致力于运用HTML5技术开发一个电视剧信息展示页面,目的是呈现诸如剧名、演员构成、故事梗概等电视剧关键资料。接下来将深入阐释如何借助HTML5的结构化组件和样式管理功能达成此项目目标。 我们必须掌握HTML5的核心框架。一个规范的HTML5文档一般包含`<!DOCTYPE html>`声明、`<html>`根标记、`<head>`头部标记和`<body>`主体标记。在头部区域,可以配置网页的基本元数据,例如字符集设定、页面标题等。在主体部分,将具体构建电视剧信息列表的内容。 电视剧展示页面通常包含多个条目,每个条目对应一部电视剧。HTML5中的`<section>`标记用于内容模块化,适合表示单个电视剧的详细信息区域。每个`<section>`内部,可使用`<h2>`标题标记显示剧名,`<img>`图像标记插入宣传剧照,`<p>`段落标记呈现剧情介绍,而`<ul>`无序列表与`<li>`列表项标记则用于罗列演员阵容。 为了优化页面布局,需要借助CSS(层叠样式表)进行样式管理。HTML5引入了创新的CSS选择器与布局模型,例如Flexbox和Grid,使页面布局更加灵活多变。在此场景下,可以利用Flexbox为电视剧信息列表实现自适应布局,保障在不同设备尺寸下均能呈现理想视觉效果。具体操作时,可将`<section>`标记设定为Flex容器,通过`display: flex;`属性,并运用`justify-content`和`align-items`属性调整子元素的对...
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值