Objective c get file from bundle


















Glorfindel TheAmateurProgrammer TheAmateurProgrammer 9, 8 8 gold badges 49 49 silver badges 71 71 bronze badges. I'm not only up voting your answer because it helped me, but also because of your awesome nickname — Yannis P. You are getting nil because your file is not present directly inside in the bundle. Thank you, kidsid49, that's what I was missing. I have now done that step, but still have nil pointer.

Make sure that you have added that text file to your current target. You can confirm this by clicking on that text file and then check that it is added to your current target. The checkbox in left side inspector must be checked for current target. Yes, target membership under inspector is already ticked on the left of the project name. Pointer of fileRoot is still nil. What about your file name is that have same name "foo.

Sign up or log in Sign up using Google. For comparison you could write the same in Objective-C as follows:. To load an image from a framework bundle we need the longer UIImage initializer - init?

The second parameter is the name of the bundle containing the image file. Using nil is the same as passing the main bundle. So these all load an image from the main bundle:.

With Objective-C it is common to do this in a more generic way:. How do we do this with Swift? For each attachment, the predicate evaluates the uniform type identifier UTI for each representation in the attachment.

If you link to an embedded framework from your containing app, you can still deploy it to versions of iOS older than 8. The mechanism that lets you do this is the dlopen command, which you use to conditionally link and load a framework bundle. You employ this command as an alternative to the build-time linking you can specify in the Xcode General or Build Phases target editor. The main idea is to link embedded frameworks into your containing app only when running in iOS 8.

You must use Objective-C, not Swift, in your code statements that conditionally load a framework bundle. The rest of your app can be written in either language, and the embedded framework itself can likewise be written in either language. After calling dlopen , access the embedded framework classes using the following type of statement:. To set up an app extension Xcode project to take advantage of conditional linking.

For each of your contained app extensions, set the deployment target to be iOS 8. For your containing app, set the deployment target to be the oldest version of iOS that you want to support. In your containing app, conditionalize calls to the dlopen command within a runtime check for the iOS version by using the systemVersion method. Call the dlopen command only if your containing app is running in iOS 8. Be sure to use Objective-C, not Swift, when making this call. You must conditionalize your use of these APIs just as you do when calling dlopen directly.

And from the NSBundle class:. In a containing app you are deploying to versions of iOS older than 8. Although these types are not directly interchangeable, they provide comparable features. And at least in Objective-C applications, you can use whichever type suits your needs. The following sections outline the different ways you can get a reference to one of these types.

The main bundle is the bundle that contains the code and resources for the running application. If you are an application developer, this is the most commonly used bundle. The main bundle is also the easiest to retrieve because it does not require you to provide any information.

To get the main bundle in a Cocoa application, call the mainBundle class method of the NSBundle class, as shown in Listing Listing Getting a reference to the main bundle using Cocoa. If you are writing a C-based application, you can use the CFBundleGetMainBundle function to retrieve the main bundle for your application, as shown in Listing Listing Getting a reference to the main bundle using Core Foundation. When getting the main bundle, it is still a good idea to make sure the value you get back represents a valid bundle.

When retrieving the main bundle from any application, the returned value might be NULL in the following situations:. If a program is not bundled, attempting to get the main bundle might return a NULL value. If the agent that launched the program did not specify the full path to the program's executable in the argv parameters, the main bundle value might be NULL.

Bundles rely on either the path to the executable being in argv[0] or the presence of the executable's path in the PATH environment variable. If neither of these is present, the bundle routines might not be able to find the main bundle directory. If you want to access a bundle other than the main bundle, you can create an appropriate bundle object if you know the path to the bundle directory.

Creating a bundle by path is useful in situations where you are defining frameworks or other loadable bundles and know in advance where those bundles will be located. You can also use the initWithPath: instance method to initialize a new bundle object. This method takes a string parameter representing the full path to the bundle directory. Listing shows an example that accesses a bundle in a local directory. Listing Locating a Cocoa bundle using its path.

Listing shows an example that takes the fixed directory from the preceding example, converts it to a URL, and uses that URL to access the bundle. Listing Locating a Core Foundation bundle using its path. Even if you do not know the exact path to a bundle, you can still search for it in some known location.

For example, an application with a PlugIns directory might want to get a list of all the bundles in that directory. Once you have the path to the directory, you can use the appropriate routines to iterate that directory and return any bundles. This function returns new CFBundleRef types for all of the bundles in a given directory. Listing Obtaining bundle references for a set of plug-ins. Locating bundles using a bundle identifier is an efficient way to locate bundles that were previously loaded into memory.

This string is typically formatted using reverse-DNS notation so as to prevent name space conflicts with developers in other companies. For example, a Finder plug-in from Apple might use the string com. MyGetInfoPlugin as its bundle identifier. Rather than passing a pointer to a bundle object around your code, clients that need a reference to a bundle can simply use the bundle identifier to retrieve it. To retrieve a bundle using a bundle identifier in Cocoa, call the bundleWithIdentifier: class method of the NSBundle class, as shown in Listing Listing Locating a bundle using its identifier in Cocoa.

Listing shows how to retrieve a bundle using its bundle identifier in Core Foundation.



0コメント

  • 1000 / 1000