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];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值