Relative and absolute paths to iOS

Source: Internet
Author: User

iOS programs have fixed file access restrictions, only within their own sandbox.

UIImage *img=[uiimage imagenamed:@ "Cellicon.png"];

This code loads a PNG image resource from a relative path as a UIImage object without any problems. Because it has secretly had to help you to convert the path to a relative path, you do not know it!

Next, if you want to load some custom-formatted files or data, you will have a problem with relative paths and absolute paths. Like what

Nsfilehandle *filehandle=[nsfilehandlefilehandleforreadingatpath: NewPath];

NewPath is a string path, and the path here is not as you would like above

Nsfilehandle *filehandle=[nsfilehandlefilehandleforreadingatpath: @ "test.txt"];

The system cannot find the file because the absolute path to the file is required, and @ "Test.txt" is completely invalid.

This time need NSBundle help, this class is dedicated to responsible for the path transformation and so on. As you know, iOS projects can create a folder structure under Xcode, but in fact there is no concept of file structure, so if you want to load the Test.txt file in the project directory, the complete code should be

Method 1:

NSString *newpath=[nsstringstringwithformat:@ "%@%@%@", [[ NSBundlemainbundle]resourcepath],@ "/", path];

NSLog (@ "url=%@", NewPath);

nsfilehandle *filehandle=[nsfilehandle filehandleforreadingatpath: NewPath];

Here [nsbundle mainbundle] is a singleton object to get NSBundle, the secondary singleton object has set the default ResourcePath, that is, your app after the packaging of the path, [nsbundlemainbundle]ResourcePath] is to get this complete packaged app path, But your test.txt file is not in this directory, but inside the app, you need to stitch the path string,[nsstringstringwithformat: @"%@%@%@" , [[nsbundlemainbundle]resourcepath], @"/" , path]; So simple string concatenation, I won't say more,

Console output app path ResourcePath:

/users/zouzouyanghong/library/application Support/iphone simulator/5.0/applications/ 76116a6a-572d-43ae-ae78-17d7a88dcafc/jfitnessproduct.app

Full file absolute path after console output stitching:

/users/zouzouyanghong/library/application Support/iphone simulator/5.0/applications/ 76116a6a-572d-43ae-ae78-17d7a88dcafc/jfitnessproduct.app/test.jat

Method 2:

NSString *newpath=[[nsbundle Mainbundle] pathforresource:@ "test" oftype:@ "TXT"];

nsfilehandle *filehandle=[nsfilehandle filehandleforreadingatpath: NewPath];

[[NSBundle Mainbundle] pathforresource:@ "test" oftype:@ "Jat"]; The preceding is no longer introduced, thePathforresource method has two parameters, the preceding is the file name, the latter oftype is the type of files, that is, the file suffix. Here the file name and suffix are separated, if this is what you get is a full file name such as: @ "Test.txt", then you have to find a way to separate the file name and suffix.

in particular, the file suffix here is not required "." , if it is wrong to write @ ". Jat", the load file will fail .

//get the Program documents directory path
Nsarray*Paths=nssearchpathfordirectoriesindomains (Nsdocumentdirectory,nsuserdomainmask, YES);
NSString*documentsdirectory=[Paths Objectatindex:0];

//get the directory path where the program app files are located
nshomedirectory ();

//Get program tmp directory path
Nstemporarydirectory ();

//Get program App package path
[[NSBundle Mainbundle] resourcepath];
Or
[[NSBundle Mainbundle] Pathforresource:@"Info"OfType:@"txt"];Copy Code

Beginner iOS Development students, whether it is written by themselves, or pasted code, more or less have written the following code

[[NSBundle Mainbundle] Pathforresource:@ "somefilename" ofType:@ " Yourfileextension"];

[Yourviewcontroller initwithnibname:"yourviewcontroller" Bundle:nil];

But you know what the bundle here is?!

The bundle is simply a special catalogue of internal structures organized in accordance with standard Rules.


iOS applications are encapsulated through bundles, the corresponding bundle type is the application type, usually we compile the target through Xcode (that is, we develop the application), is actually a application type bundle, That's a folder! But the finder will show this bundle as a file to us, in fact because the bundle itself is a package, and the Mac system treats all the package as a file and displays it to the user, thus preventing Misuse of the user causes the program file to be corrupted or lost. As to the difference between bundles and package, it is not here to explain that the bundles described later in this article will be considered by the MAC system as the package.

Now that we know that the application we developed, and eventually become a bundle, it is not difficult to understand why many of the resource files are loaded through bundles, and the file load from the bundle is actually loaded by the application's own folder.

But then we have several questions:

1 Why not use the file path directly to read the resource file?

2 Why still exist Pathforresource:oftype: This method, we are not in the development of the time to determine where these files?



In fact, we can use the path of the file system to access the resource files, but when we want to do the internationalization of the app, we need to implement different languages, regions to load the corresponding files, and in some of the resource files common, some need internationalization, for developers, Code maintenance is especially confusing.

and bundle for iOS developers, the biggest convenience is to be very simple to let their own application internationalization, in different languages different regions, loading different resource files, display different language text, and achieve these only need we strictly according to the requirements of the bundle of resources file storage can be, Rather than writing a lot of code to judge the locale language. Follow the method is also very simple, just need us to create the corresponding "Localization folder", for example, we want to let the picture "Pic.png" in Chinese and English display different content, only need to create two localization folders Zh.lproj and En.lproj, respectively, put the same name but different content " Pic.png "can.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.