Creating a Multiwindowed Cocoa Program - Launching Procedure Suggestions? - objective-c

I'm porting an application I developed in Visual Studio 2008 over to Cocoa. I'm currently doing a 'learn-as-you-go' approach to Cocoa, so I can experiment with different ideas and techniques in smaller, simpler projects and eventually combine them into one big application.
My program logic is as follows (in a dumbed-down sense). Items in the list are mandated by my boss.
Application is started
1a. Verify CD program is in drive.
Verify license. If found and is valid, skip to step 7
Display license agreement.
Display serial number prompt.
Verify and save serial number.
Hide all prior windows.
Load main application window
Intercept requests and commands from main application window, including making a duplicate main application window
Exit program when requested by user
What would the best bet be for this type of application? From another question I asked, I found out that I should keep the 'main application' window in a separate XIB file from the rest, because I might need to clone and interact with it.
I know that since Cocoa and Objective-C is based off of C, there is a Main method somewhere. But what would you all suggest as a starting place for an application like this?

So some of this comes down to organization. Like Julien mentioned, you'll want a YouappnameApplicationDelegate class - in fact, Xcode will create and set this up for you when you create a project.
implement the applicationDidFinishLaunching method (which also should be provided by what Xcode gave you), and implement your logic for steps 1 and 2 there (and steps 7, 8, 9).
Steps 3 thru 5 you'll probably want to implement in another class. RegistrationWindowController, or something like that. You may even want to create this window in another nib file (not inside the MainMenu.nib file that Xcode gives you). In the YouappnameApplicationDelegation's applicationDidFinishLaunching method you'd load this nib (see code sample below). That way your code is well organized - which is what Cocoa really guides you to do.
Ok, so now how to load that new nib file:
myInstanceVariable = [[RegistrationWindowController alloc] initWithWindowNibName: #"MyNibName"];
[myInstanceVariable showWindow: self];
RegistrationWindowController should be a subclass of NSWindowController.
That should take you a pretty long way into your research, hope it does help!

Related

Mac OS: How can I run a Cocoa application from within another Cocoa application?

I've written an Objective C runtime debugging tool (in Swift) to help me in the development of another application. Currently, this tool is a standalone Cocoa application that launches normally, with it's own instance of the Objective C runtime, so it's not of much use.
I would like to modify it so that it can be opened from a method call in a target application, and so that uses the target application's runtime. This requirement precludes the use of solutions I found which depend on NSTask to start a brand new process.
How can I achieve this?
Edit:
My debugging tool is a GUI application. Normally, it starts up via a call to NSAppliationMain from the main function. This sets up the application instance from the main bundle's info.plist file. Clearly this won't work when trying to call NSApplicationMain from within another application's code, as the info.plist of the main bundle is that of the target application, and not the debugged application.
So to paraphrase: How can I manually start up my application from its main nib file, opening its window, menu bar, etc. from within a target application?
One last guess (after comments) and in rough outline only:
Step 1: Load your windows from their own NIB
Windows do not need to be loaded at startup from the main NIB, there are methods to load them programmatically as needed. E.g. look in NSWindowController (load a single window) and NSBundle (load a whole NIB-full of things).
Step 1B: Dynamically add menus/menu items
If you wish to have menu items for your code you can programmatically add those rather than pre-defining them in the main NIB. You can also load whole menus from a NIB just like with windows.
Step 2: Write an API to start & control your debugger
This is the API a client will call. Modify your debugger app's startup to call this API to test it.
Step 3: Convert project to a framework.
The framework will contain both the code and NIBs.
Step 4: Use framework in another app, call the API.
HTH

When should I subclass NSDocumentController for OSX app?

I'm building a document-based app with core data support (so my document is NSPersistentDocument) and I'm wondering if I need to subclass NSDocumentController as well as NSWindowController (for every window in my app since I have multiple).
I went over apple documentation, but haven't found a good explanation on NSDocumentController except that it says that "it is unlikely that you'll need to subclass it".
Can someone give me some hints and scenarios on how this all works?!
Any kind of help is highly appreciated!
An excellent example of subclassing NSDocumentController (though it doesn't use CoreData) can be found in the TextEdit source code. See their DocumentController class. Specifically, they subclass NSDocumentController to support transient untitled documents. Newly opened documents will take the place of empty untitled documents provided they have not been edited.
Another example use would be changing how opened documents map to NSDocument subclasses within your application. By default, it is one-to-one, but suppose you were developing an IDE in the same vein as Xcode. You might have a project document subclass as well as a file document subclass. If the user opens a file that is already present in an open project, you could force the document to open within the project and its related windows rather than create a new file document and window for it.
Hope that helps!

How can I make a copy of an already created window?

So I am making an xcode application for OS X, and so far things have gone well. However, right now I am at a roadblock. My app is mostly one window, and I need that window to be created multiple times, in the way that apps like Safari, TextEdit, and all those ones have the File>New function. I am new at Xcode, and I was wondering if there was an easy way (hopefully 3-4 lines of code) to do this. If someone could explain that to me that would be great.
Thanks!
Start a new OS X project and choose "Create Document Based Application" . This will create a NSDocument subclass.
If you want to use Core Data , choose that also. This will create a NSPersistentDocument subclass.
This will give you the basis of your application with all the functionality you ask for.
Have a look at Document Based App Programming guide and NSPersistentDocument Class Reference
Whatever you do, dont try to bake your own. There lies madness.
The project template sets up the info.plist correctly to make this work.
Whatever I tell you, it won't involve only "3-4 lines of code," unfortunately.
If you're working from a non-document-based application, you'll want to factor out the code that creates your one window. If you're not using a window controller in concert with your window, you should implement one. Then you can implement an action that will create a new instance of your window controller, and so a new window. Of course, you'll need to add more infrastructure if you're interested in saving information associated with a window to a file.
Which leads me to your other option, which is to create a document-based application. The big advantage here is that, if you're interested in saving the window contents to a file, the infrastructure for doing so is provided. This involves starting over to some extent, but since you've already got the guts of your application working, you'd simply transfer those guts to the document subclass that is provided for you. You'd also need to provide to your project meta information describing the kind of document it is, including the file extension describing it.
Best wishes to you in your endeavors.

Can you attach a drawer to another application in Cocoa?

Is there a way for one Cocoa application to attach drawer-like windows to another application? We might for example want a terminal drawer that followed around a particular Finder window.
There is a program called DTerm that opens little transparent windows over Finder windows, but one might prefer persistence.
You may want to checkout SIMBL. It allows you to write nifty bundles that are loaded into the application your targeting. If you go along with it I'd reccomend using class dump to gather more information on the application your working with (although Im not sure it would work with Finder)

Alternative to NSCollectionView in pre-OSX10.5, Cocotron?

NSCollectionView was introduced in OS X 10.5, and is not yet implemented in Cocotron.
I am trying to implement a small app that will allow creating properly packaged data files for an online service, which will then be uploaded by an administrator. More specifically, the user will create a collection of input and output data pairs, by dragging input and output files onto the window.
Currently the idea is that user drags a file, from the filename it's detected if it's the input or output filename (by default, input), and a view with icon and filename for input and output is added to collection view. Then, the second file is dropped on the "other" icon.
However, NSCollectionView does not appear in pre-10.5, and most of my users don't have Macs so I'll have to provide a Cocotron-built application. Not only that; I still don't fully understand KVC/KVO, and I really should understand everything that my code does. Hence, I need an alternative to NSCollectionView.
What alternative do I have to using NSCollectionView? (Any intuitive solution is appreciated, don't feel limited by the above description of my idea.)
To work with NSCollectionView, you need to not only understand KVC and KVO, but also Bindings.
There's code for an NSCollectionView clone that works on Tiger here.
I still don't fully understand KVC/KVO…
That's what the docs are for:
Key-Value Coding Programming Guide
Key-Value Observing Programming Guide
What alternative do I have to using NSCollectionView?
Make your own.