I want to add a simple one-page HTML page help to my Cocoa app. Can you tell me how to do it? I assume I just have to throw in one lousy .html (and maybe one .css?) file somewhere into my Cocoa project in Xcode?
Creating Apple Help documents that are opened in the Help viewer is straightforward but you must follow the directions in the documentation exactly.
Help files are HTML but you need to place a couple of special tags in the page and name the files in a particular way.
It's all explained in the documentation.
Today I've been facing the same problem. I found no up-to-date howto so here is one of my own. Help is nicely working with this Step by Step to create Apple Help in your Cocoa Xcode Application.
If you only want a single HTML page and not a proper help file, you could add an HTML document and CSS file to your project. These will be copied to your application's Resources directory inside the app bundle when you compile the project. To load the document, you'll need to get its address. This is actually quite easy:
NSString *helpFilePath = [[NSBundle mainBundle] pathForResource:#"YourHelpDocumentHere" ofType:#"html"];
NSURL *helpFileURL = [NSURL fileURLWithPath:helpFilePath];
The resulting URL will be a file URL that a WebView can display inside your application, or you can pass it off to the operating system using NSWorkspace and it will be opened in the user's default web browser.
Related
My question is about the application bundle in a project. I was reading about that and can understand some basic things (I'm not a native english speaker). I can understand that the resources folder is used to hold the files that will be used in the project, e.g. media files (images, audio, video, etc.) and should be in the application bundle to be identified.
So, what is the point if I want to use images and another resources in my project? In my other related question, I can't use them by referencing with NSImage imageNamed:.
I have used the following with no success loading my files:
NSBundle methods
imageNamed:#"string" with/without file extension
the images are in resources folder
I'm learning Cocoa and Objective-C, and of course this is different to C++ or Java when I want to create an ImageIcon or a QImage.
I may not have completely understood the issue, so correct me if I am wrong. I believe the problem has to do with your image's target membership or how you're retrieving the image in your code.
Adding an image to your project target will appropriately copy the resource at compile time. To add the image to your target, select it in the file navigator and then reveal the Utilities Panel. On the Utilities Panel, select the File Inspector Tab. Look for the Target Membership section and ensure that the image is selected for the desired targets:
Do you mean that you can't use the NSImage imageNamed: method to retrieve resources? If so, you can retrieve the resource like this (from the main resource bundle):
NSString *imageName = [[NSBundle mainBundle] pathForResource:#"image1" ofType:#"png"];
NSImage *imageObj = [[NSImage alloc] initWithContentsOfFile:imageName];
It also looks like you already have a good answer to your other related question.
I need some help for my OS X program.
I need the URL of a file inside the supporting files.
I have an array in which I save URLs from images and add them to a table view and if no images are chosen I want to add a question mark image (it is called "bild.jpg")
This bild.jpg is inside the supporting files but for later use I can't just save the name of the image because the array stores also URLs.
I need to have the URL of that image in the supporting file because it's easier to use the array for image initialization.
Is there a function to get the path or is there a standard path to the supporting files? I already search on the net but couldn't find anything that could help.
You seem to be talking about the application bundle and its resources directory rather than, say, a subdirectory in ~/Library/Application Support/..., in which case you probably want something like:
[[NSBundle mainBundle] URLForResource:#"bild" withExtension:#"jpg"]
(See the documentation for NSBundle.)
I want to have a WebView that displays some static files from the application bundle. Since I have a large number of small files, I'd like to pack them all into a compressed archive so the application doesn't take up too much space. What's the best way to make this happen?
This should help you out: http://code.google.com/p/ziparchive/
To display data in the WebView:
On Mac OS X use WebFrame's loadHTMLString:baseURL:
On iOS use UIWebView's loadHTMLString:baseURL:
What you probably want to do, is implement an NSURLProtocol subclass that will resolve relative URLs by reading them from the zip archive. That way, you only need to initially read the "main" HTML file from the zip into memory, and the others will be read in on demand. To get WebKit to use your custom URL protocols for resolving relative paths, you could instantiate the WebView like this:
[[web_view mainFrame] loadHTMLString:your_main_html baseURL:[[NSURL alloc] initWithString:#"zip:///"]];
Apple has a really good example of combining a custom URL protocol with a WebView here:
https://developer.apple.com/library/mac/#samplecode/SpecialPictureProtocol/Introduction/Intro.html#//apple_ref/doc/uid/DTS10003816
I have been working on making an application for editing game plugins for a game I made. I needed the files it saves to have a custom format for obvious reasons. Unfortunately, I cannot figure out how to give the file a custom icon.
I did some research last night into UTIs, mostly because they are mentioned wherever icons are talked about. I did what the Apple Documentation said, but it didn't work.
Here is my code so far:
[#"test" writeToFile:#"test.hsimg" atomically:YES encoding:NSUTF8StringEncoding error:NULL];
That works perfectly. The only problem I have now is that there is no icon. Could someone please tell me how I need to setup the UTIs to make it work?
Take a look at these questions:
How do I associate file types with an iPhone application?
Creating my own file extension based on plist
I'm having some trouble working with libraries and included xib files. Let me explain my situation first. I've got a project that's running pretty good. Now I need a part of my project as a library for another customer who want's to use some of the functionality in his app. I've created a library with help of that highly recommended article.
My customer should be able to initialize a main view controller like this:
LibraryMainViewController *lmvc = [[LibraryMainViewController alloc] initWithNibName:#"LibraryMainViewController.xib" bundle:foo];
That for sure leads to an error when I try to present that view controller modally. I'm not sure what bundle I have to use here. I tried something like
[NSBundle bundleForClass:[SomeClassInTheLibrary class]];
but that didn't solve the problem.
I would be glad if someone could tell me how to actually use a xib file in that kind of situation.
Thanks
–f
Update
Ok, I see I got it wrong somehow. Just for clarification: What I need is the library, the headers and the additional resources, right? Is there some kind of best practice for creating and shipping "a feature" with all it's parts above mentioned?
Static libraries can't include graphics, sounds, xibs or even headers. Only object code. Even if you added the xibs to Copy Bundle Resources, they won't become a part of the object file. Unfortunately, you can't use dynamic libraries or frameworks on the iPhone. See my answer here for a suggestion of how to create a separate assets bundle. You could also just send your customer the xib files separately, but then they have to replace them by hand if they change in the future.
Try without adding the extension to the XIB file. That's the way i usually do it. I'm also not sure if the XIB must be compiled to a NIB..
NSBundle *bundle = [NSBundle bundleForClass:[SomeClassInTheLibrary class]];
LibraryMainViewController *lmvc = [[LibraryMainViewController alloc] initWithNibName:#"LibraryMainViewController" bundle:bundle];