Application of beam

Structure of application bundles Cocoa applications store executable files or the necessary resource formats in a delegate structure called application bundles, application wrappers, or application packages.

It is quite common in Cocoa to save several files or directories in a definite form into a directory structure like this, and then process them as a collection. Such structures are generally called bundles.

Nib files and GUI definitions generated using Xcode (Interface Builder) for each language resource are written to niB files. Nib files are suffixed “NIb”, which stands for Next Interface Builder. Information such as the configuration of the application’s menu and window components is archived and read in dynamically at execution time.

The file name extension of a NIB file is NIb or xib. Xib is an XML file with the same content. During application construction, the niB file is saved in the application bundle as a resource file with the nib suffix.

The Storyboard builds the GUI around the scene movement, so it’s a good architecture for applications like display and processing. Within each scenario, the relationship between objects is the same as the NIB file. While the application is being built, the Storyboard itself becomes a NIB file resource.

Language-independent shared Resources are placed in the Resources directory on Mac OS and in the App bundle directory on iOS. Resources that switch based on the selected language are placed in the language.lproj directory. The suffix comes from the Language Project and has been in use since the NeXTstep era.

At runtime, the application looks for the selected (or high-priority) language subdirectory in the runtime environment and uses the resources in it. The NSBundle class performs resource retrieval, so the program does not need to record the encoding of the specified language.

The process of making an application correspond to a particular language is called localize. In Cocoa applications, if you can program them to multiple languages, then you can easily localize them by appending language-specific resources to the application bundle.

When the application bundle is used, the string specified by the file keyword CFBundleIdentifier in the information is called the application identifier. The identifier is often used to find the corresponding application in the system. To avoid duplication of names, the Java package definition is recommended. For example, com.apple.iphoto.

Access resources through NSBundle NSBundle is a class that provides an interface for various bundles. You can search for GUI defined NIB files, images, sounds, loading code, and so on from the specified bundle.

  • (NSBundle *)mainBundle // the application bundle contained in the program is also called the mainBundle. This method returns the object corresponding to the path of the fish mainBundle. Returns nil if unrecognized.

  • (NSBundle *)bundleWithPath:(NSString *)path // returns the bundle object of the path specified, nil if there is no path accessible bundle. There is also a method that uses NSURL to specify the location BundleWithURL:

  • (NSString *)bundleIdentifier // The application name specified in the return information file (info.plist)

  • (NSDictionary *)infoDictionary // returns the contents of the information file (info.plist) as dictionary objects

  • (id) objectForInfoDictionaryKey: (nsstrings *) key / / returns to the key parameters in the information files for key/value object. Localize it and return it if possible

  • (NSString *)pathForResource:(NSString *)name ofType:(NSString *)extension // in the receiver’s bundle, return the name specified by name and the suffix of the resource pathname specified by extension. Specify extension as nil or @ “” when the file does not have a suffix. Returns nil if file cannot be found. There are also using NSURL returns the location method URLForResource: withExtension:. You can use the resourcePath method to obtain the path for saving resources.

Additionally, on the Mac OS platform, the image path can be obtained using the pathForImageResource: method. It is a method in the Application framework that is added as a category to NSBundle. For details on how to obtain other NIB files or sound files, see NSBundle Additions.

As mentioned earlier, you can also access resources in iOS using the NSBundle method. However, because iOS has special suffixes, here’s the summary.

In iOS, the content and image sizes of niB files must be changed to accommodate all devices. Here is a way to specify resources or parameters automatically based on the device being used.

1. Prepare any resource by device in iOS when the retrieval request for the bundle is specified as “filename”. Files with the suffix “will be searched for the following files. File name to device name suffix

2. Prepare the file name @2x. File name extension @2x~ device. Suffix name (compatible with the device specified above) the UIKit framework uses the class UIImage to process images. This class can retrieve and load image files in the main bundle using the following methods to generate instances that contain images.

  • (UIImage)imageNamed:(NSString *)name

3. Specify the device using the key value string of the information file to match the device, you can specify the information read in the information file. The suffix “~ device” is appended to the general key value string to indicate that only the device is applicable. For details about iOS resources, see iOS Application Programming Guide, iPad Programming Guide, and Resource Programming Guide.

A universal binary is apple’s technical term for a form of executable file created to run on different cpus of multiple operating systems.

Load the NIB file

Nib file instantiation NIB files define communication messages between GUI components, which must have an object called the owner. The owner is the bridge between the group of objects in the NIB file and the outside world. Access to NIB files is basically communicated through the owner.

The NIB file is an archive of the object graph, and the owner acts as the root object. But NIB files can also generate unpointed pointer objects.

Load niB files in Mac OS

  • (BOOL)loadNibNamed:(NSString *)aNibName owner:(id)owner // finds the nib file specified in the string in the bundle and instantiates the owner object owner. Return NO on failure.

When memory is managed using reference counting, all objects in the NIB file (except the owner) are set to 1 by reference counting and instantiated. Next, references between objects are constructed, and objects held by other objects are released via autoRelease. If there are unreferenced objects in the NIB file, only that object will eventually not be freed, so you need to use other methods to free memory.

When garbage collection is used, objects that are strongly referenced by the owner have no problems because they are accessible. If there is an unreferenced object in the NIB file, the object is in danger of being freed after instantiation.

Load the NIB file in iOS

  • (NSArray *)loadNibNamed:(NSString *)name owner:(id)owner options:(NSDictionary *)options And instantiate the owner object, Owner. Options specifies options, usually set to nil. In a NIB file, objects that are not contained by other objects (top-level objects) are stored in an array and returned. In iOS, all objects in a NIB file (except the owner) are set to a reference count of 1 and instantiated. Next, a reference relationship between objects is constructed, and objects held by other objects are released via autoRelease. Use the key-encoded setValue:forKey: method to set the exit of the object. When you need to specify the reference to the instance variable used by the exit, store it in a way that is ready to set or declare properties.

Include loops in NIB files When using ARC to manage memory, it is easy to create include loops between objects in NIB files, which must be avoided.

Custom objects contain exits that should in principle be used through weak references.

Be careful not to have null Pointers. Weak or not, you need to assign the instance variable that refers to another object to nil before releasing it.

Initialization of objects in niB files Objects contained in NIB files that implement the following method will run after instantiation.

  • (void)awakeFromNib // Read from the NIB file and call the method after instantiating, exporting, and accessing the connection. This method is declared as an informal protocol.

Starting an application The niB file that is first read by an application stores important information needed to run the application, such as menus. This is called the main NIB file. The information file also contains a file name.

Only one INSTANCE of NSApplication is generated after the application is started. In addition to the running loop, it is also responsible for managing various resources of the application.

Mac OS application: int NSApplicationMain(int argc, char *argv[]) {[NSApplication sharedApplication]; // Generate an NSApplication instance

If ([NSBundle loadNibName: owner:NSApp]) // load the primary NIB file [NSApp run]; // Start the loopCopy the code

}

In addition, the various initialization Settings inherent in the application do not need to be placed in the main function, but should be written to the delegate objects of NSApplication and UIApplication. After the application is started, a message notifying the delegate that the startup is complete should be written inside the delegate. For details, see the NSApplicationDelegate and UIApplicationDelegate protocol reference documentation

The location where iOS files are saved

The main directory and its functions are restricted in iOS for security reasons, such as switching between apps. The location of file storage is limited to the specific location assigned by each application. The location is determined when the application is installed and is called the home directory. Application packages and generated files are saved in the home directory and deleted when the application is deleted.

The home directory of the application contains the main directories. Each directory has different functions and all pathnames are fixed. Files created in these folders must be managed by the application. Unneeded files will be deleted.

1. Home/App name app/App bundle itself is stored here. It can’t be changed because of the signature. 2. Home/Documents/Where the application generates and saves files. Start backup when connecting to iTunes. If the key value UIFileSharingEnabled is set to YES, the file can be synchronized with the PC through iTunes. 3. The home/Library/Preferences/application Settings are written. Backed up when connected to iTunes. 4. Home/Library/Caches/Stores temporary application information as a file here. Load available information such as operation history or operation process at the next startup. However, it may disappear at any time when the device is restored, etc. ITunes doesn’t back up. 5. Home/TMP/Stores temporary application information as a file. The application may be released by the system when the application is not running.

The directory path In the above list of Documents and Caches paths, it is recommended to use function of Foundation framework NSSearchPathForDirectoriesInDomains (). Functions and arguments to use are declared in Foundation/nspathutilities.h. NSArray *NSSearchPathForDirectoriesInDomains( NSSearchDirectory directory, NSSearchPathDomainMask domainMask, BOOL expandTilde )

This function was originally intended for Mac OS only, and the combination of parameters may not make sense in iOS. Also, since the save in the returned array has a path, use objectAtIndex: to retrieve the element at the head. Specify the type of directory in the first parameter. To get the path to Documents and Caches, specify the parameters NSDocumentDirectory and NSCachesDirectory, respectively. In either case, specify the second parameter as NSUserDomainMask and the third parameter as YES. The TMP directory path can be obtained using the following function: NSString *NSTemporaryDirectory(void).

User the default

Saving Settings In Cocoa applications, a common structure, user Defaults, or default database, is used to hold user-set values. The NSUserDefaults class provides the interfaces required to access user defaults. Please refer to the official documentation for details.

Default domain Users can be considered into multiple groups by default, called domains or default domains. In a domain, some keep content in a file and continue to use it later, while others only exist when the application starts. Details will be given below:

  1. An application domain uses a collection of Settings inherent in the application managed by property lists. The set of these values is called the application domain, and the application id is used in the domain name.
  2. The global user uses the account to set a shared value for each application. The domain name is NSGlobalDomain. This field also works in iOS, where you can get the specified language or keyboard information.
  3. The set of values specified by the parameter field parameter is called the argument domain. Domain names are represented by NSArgumentDomain and cannot be saved in a file. This domain is not used in iOS.
  4. Language domain Specifies a temporary domain (without saving files) that generates the language name (for example, CH) as the domain name when the language is used.
  5. A set of default values to use when the landing domain user has not set the various Settings for the application. The domain name is NSRegistrationDomain and is not saved in a file. When an application uses the NSUserDefaults class to find the corresponding set values from the key values, it looks for the five fields in the following order and uses the value found first. Parameter field Application field Global language field Landing field If default values are required for various set values, you can organize the collection of keys and values in a dictionary and log in to the landing field when the application is initialized. Because the landing domain lookup is performed at the end, default values are used when no other domain can be logged in.

On the Mac OS, users can directly edit the property list or use the command-line interface (CLI) tool Defaults to access the property list. To see how to use it, simply enter defaults on the terminal, or use the man command to view the help manual.

NSUserDefaults profile

  1. Get instance object
  • (NSUserDefaults *)standardUserDefaults returns when the instance is initialized on the initial call. From the second call, the same instance is returned.
  1. To retrieve values for keys, use the following method in an NSUserDefaults instance.
  • (id)objectForKey:(NSString *)defaultName

  • (NSString *)stringForKey:(NSString *)defaultName

  • (NSArray *)stringArrayForKey:(NSString *)defaultName

  • (NSInteger)integerForKey:(NSString *)defaultName

  1. Specify the key and set delete
  • (void)setObject:(nullable id)value forKey:(NSString *)defaultName;

  • (void)setInteger:(NSInteger)value forKey:(NSString *)defaultName;

  • (void)setURL:(nullable NSURL *)url forKey:(NSString *)defaultName;

  1. Set defaults
  • (void)registerDefaults:(NSDictionary *)registrationDictionary;
  1. Fetching domain contents
  • (NSDictionary *)dictionaryRepresentation;
  1. Display Settings in a file
  • (BOOL)synchronize;

Localization of applications

Localization of messages Using the bundle structure allows programs to display messages corresponding to various languages. To do this, you first need to generate the Localizable. Strings file and save it to the corresponding directory for each language (language.lprog). The file name can also be another name, using Localizable. Strings if not specified.

The Localizable. Strings file is a pair of key strings and key value strings joined by an equal sign, with a semicolon at the end. Both keys and values are enclosed in “”. You can omit the value, but also remove the equal sign. Even if the value is omitted, the key itself is still treated as an entry value. Alternatively, commands can be written in the form / ** /.

The ASCII code is used for key strings and the target language (Chinese, etc.) is used for value strings. Files must be encoded in UTF-16. If only characters in the ASCII range can be used to record all the characters, keep the ASCII encoding.

The NSBundle object of the application’s own bundle (main bundle) can retrieve the key value string via the following message.

  • (NSString *)localizedStringForKey:(NSString *)key value:(nullable NSString *)value table:(nullable NSString *)tableName

NSLocalizedString(key, comment); // In oc mode, no comment is used

NSLocalizedStringFromTable(key, tbl, comment); // In oc mode, no comment is used

Localization Pointers in Japanese differ from English in that display messages cannot be encoded in Japanese. Localizable. Strings is required even when building only Japanese applications. This file, along with niB files that define the GUI, help, etc., and Japanese files should all be saved in the ja.lproj directory.

If it’s just an English app, you don’t need to worry about localization. Subdirectories for each language, including English, and Localizable. Strings are basically not required. However, such applications are not considered internationalized. Conversely, it is very difficult to internationalize or localize such an application after the fact. Because the message is written inside the source program, it must be modified using NSLocalizedString () and so on.

A locale is a collection of habits or units of use and so forth when editing text. For example, the way dates are represented, the character used as a decimal point, and so on, varies from country to country and language to language. In order to produce software that can be used in any language environment, you need to prepare these fonts in advance and then select the appropriate display when the software is used.

To represent localization, the Cocoa environment provides NSLocale classes, which can be used when creating applications that can deal with a variety of language or geographic conventions. The localization information selected by the user based on the use environment is called the current localization, and the application operates with the current localization.

Word order in a message: In general, format characters (%s, etc.) in a format string can correspond to subsequent arguments. By specifying “number” + “$” after the format %, you can transform the numeric argument in the specified order at that position. For example, the following printf will print “Good: 100”. printf(“%2$s: %1$d\n”, 100, “Good”);

Dynamic loading of modules

This feature is not available on iOS. It is available only on Mac OS.

The App Sandbox, sold in the App Store, is used by Mac OS apps to enhance their security. A sandbox is an application execution environment that can only run under preset permissions. When developing and using an App SandBox, Xcode sets what qualifications it should have. For example, whether you can read and write files, whether you can access the Internet, whether you can use the system camera, etc. Because these are put into the code signature, they cannot perform actions that are not predefined, even if maliciously affected. And, like iOS, there are restrictions on where apps can read and write files.