How a macOS document-based app start itself? - objective-c

I am learning the document-based app architecture of macOS app development, but am confused about it.
I created a document-based app in Xcode. The app template created a simple document-based app. It can run, and will create a new document automatically when it is run.
My question is: How does this app start the document architecture? I suppose that there should be some code like the following:
NSDocumentController *docController;
docController = [NSDocumentController sharedDocumentController];
[docController newDocument:self];
But I can't find any such code in the created app. The - (void)applicationDidFinishLaunching: method in the app delegate is empty. Also, there is not any NSDocumentController object in the main nib file.
So, how does this app know that it should use the document architecture, initialize a NSDocumentController, and then create a new document?

how does this app know that it should use the document architecture, initialize a NSDocumentController, and then create a new document?
I’m unclear about the inner workings of Xcode, but it appears that in the app’s info.plist CocoaNSDocumentClass has to be set to Document and Role has to be set to Editor (or Viewer, None, or Shell) in order for the document to be created. In your demo try deleting either one or both of these lines in the plist and see if the app still works. Both settings are located on the second level of Document types. In my experience, deleting either of these lines breaks the app and restoring the correct setting fixes it. In a programmatically created document-based app (no nib) and compiling with Terminal the following errors are generated when I go inside the bundle and run the executable after deleting one or the other info.plist settings mentioned above.
2020-02-11 19:31:48.294 nsdoc_demo[1606:658926] NSDocumentController Info.plist warning: The values of CFBundleTypeRole entries must be 'Editor', 'Viewer', 'None', or 'Shell'.
2020-02-11 19:38:46.355 nsdoc_demo[1748:680819] -[NSWindowController loadWindow]: failed to load window nib file 'Document'.
Xcode (with Document.xib) generates the following error when CocoaNSDocumentClass is not set:
2020-02-11 16:22:03.022602-0600 docExpt[1166:428702] The DocumentType type doesn't map to any NSDocumentClass.
In my experience the source code can be ok, but if the info.plist is not set correctly a document-based app won’t work as expected.

Related

Xcode 8 Beta #import unexpected behavior

So I'm just coming back to programming for macOS after a few years hiatus, and have discovered the #import syntax. I created a sample program to test it out, but it's not working as my googling seems to suggest that it should.
Test project setup:
Open Xcode
File>New>Project...
Select the Cocoa Application template for macOS. Click Next
Name the project "ModuleTest", add organization name and organization Identifier. Select Objective-C as the language, check Use Storyboards, make sure "Create Document-Based Application", "Use Core Data", "Include Unit Tests" and "Include UI Tests" are all unchecked. Click Next and choose a save location. Don't create Git repository, and don't add to any project or workspace. Click Create.
In the Navigator, click on Main.storyboard.
From the Object Library, drag an AVKit Player View object onto the View of the View Controller Scene in the Storyboard.
Now at this point, we can see the symptom, which is that if you build the project, a window will pop open, but the player is missing from it, and the xcode output window shows a message such as
2016-08-16 09:07:46.465 ModuleTest[19778:4097187] Failed to set
(contentViewController) user defined inspected property on (NSWindow):
*** -[NSKeyedUnarchiver decodeObjectForKey:]: cannot decode object of class (AVPlayerView) for key (NSDestination); the class may be defined
in source code or a library that is not linked
This error occurs regardless of whether I put an #import AVKit; statement into AppDelegate.h, AppDelegate.m, ViewController.h, ViewController.m, or main.m. However, simply linking the AVKit.framework in the "Link Binary With Libraries" section of the project's Build Phases, solves the issue, no #import needed anywhere.
So what's going on? I thought #import was supposed to eliminate the need to manually link frameworks? Am I going through the process incorrectly? Is this happening because I'm using storyboards? Any ideas?
The problem is that there is no actual code that references the AVPlayerView. Try adding a single line of code that uses AVPlayerView and you'll notice that the error is gone.

How do I create a file with the iOS\Cocoa Touch\Objective-C class template in Xcode

Forgive how simple this might sound but I'm stuck and need help. I am putting In App Purchases into may first app, which is not yet released. (So I'm very new)
I have been using a tutorial by Ray Wenderlich. I have just finished starting the Master Detail Application Template. Now it says I need to create a IAPHelper class which will be my class for the IAP the step I am on says to "create a file with the iOS\Cocoa Touch\Objective-C class template". However when I open Xcode and I select new project, I don't see this option. It has Single View Application etc.. the closest thing I see is Cocoa Touch Static Library.
Can someone tell me how I "create a file with the iOS\Cocoa Touch\Objective-C class template". I'm lost, do I do it from inside the Master Application Template? Or do I use some other template name like utility or empty application or Cocoa Touch Static Library? Or do I not have this in my version of Xcode? Or am I totally coming at this the wrong way? ahhhh!lol
I have Xcode 4.3.2
I know this is probably a no-brainer for some but not for me :(
Thanks for the help :)
In the current project you are working in, right click on a file and click "New File..." or go to File>New>File and then you will probably want a Objective-c class objection under Cocoa Touch
You need to do this in the same project which you have done till now. What you are trying to do is to create new project where as the tutorial is asking you to create new file. You have to do this in the same project which you are working on. In order to do that there is an option in file menu to add a new file to project.

iOS - Execute precompiled app from a server

I'm looking for executing a .xib (with its own controllers and libraries) precompiled on a server, downloading it on runtime.
Is it possible?
Thanks!
EDIT:
So could somebody give me an example of a program that uses NSBundle that executes other app?
And how do I create the bundled application?
I don't think you can import a xib into the application's bundle at run-time (which you would have to in order for this to happen). Others may know more and correct me!
I can think of a couple of ways you could try to do this, but are you aiming to get it in to the store?
This is expressly prohibited by Apple Developer Guidelines.
A .xib file is just a data file, so there shouldn't be any problem loading one that's outside your app's bundle. I can't say I've ever tried it, but as long as it's in a bundle, you should be able to:
Create an instance of NSBundle using the path to the bundle containing the .xib you want to load. See +[NSBundle bundleWithPath:] for that.
Load the .xib using the bundle you created in the previous step with any of the normal .xib-loading methods, such as -[UIViewController initWithNibNamed:bundle:] or +[UINib nibWithNibName:bundle:].
with it's own controllers and libraries
That part won't work. iOS doesn't allow dynamic linking to frameworks other than the ones provided by the system, so there's no way to load your code. If you can build all the code you need into your app, though, you should still be able to use downloaded .xib's as described above. That would let you do things like update the way your views are laid out or what targets and actions your controls are connected to.

How can I setup a project to operate like the 'Window-Based Application' project template from XCode 4.0, in 4.2+?

I'm using XCode 4.2, and I'm struggling with how to create a Single or MultiView application from an Empty Application project.
Previously, XCode had a Window-Based Application template, and it has been removed. After a bit of Googling, I found this highly indexed article that walks through the process of creating an Empty Application project, and manually setting up like the previous Window Based application template.
The problem that I am having is that the books that I own (that aren't to old) reference the Window-Based application, and even after following the instructions on the above linked tutorial, I can't get my projects to work. I'm missing something that bridges the gap between this web tutorial and the projects that are defined in the books.
Could some one either point me to a source or give a decent, high-level walkthrough on how to define a Single-View application, starting from an Empty Application project?
The "Empty project" already creates all window stuff for you. All you need to add is a UIViewController and link it in the app delegate.
To set the UIViewController you should use -[UIWindow setRootViewController:]. The UIViewController is the only part missing from the empty project that needs to be added for the application to work.

Renamed project failing

I have taken a copy of an existing project (Door), with the expectation of using some of the core features for a supervisor application (Mgmt).
I have renamed the project from Door to Mgmt.
I have visited every visible file and changed references to the old application name.
I have done a global search and replace for the old app name.
I have also opened project.pbxproj and found over 10 references to the old project name and changed them. I have also edited xcschemes .xcscheme file name and the cxschememanagement.plist file. As I wish to use push, I have a new certificate for the new app
Everything appears fine, however.......
When I run the application it fails because it cannot find the view controller under the old applications name. (DoorViewController). For the life of me, I cannot see where this is referenced from. the error message is shown below....
[Switching to process 7171 thread 0x1c03]
Re-enabling shared library breakpoint 1
2011-11-30 12:18:17.128 mgmt[9706:707] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </var/mobile/Applications/EE73FFD1-5392-41F6-A8CB-706D042EF134/mgmt.app> (loaded)' with name 'DoorViewController''
Obviously there is one more thing missing, however it is not something I can see. Any suggestions?
I wonder if you tried this:
Open MainWindow.xib
On left column, under objects:
Do you see "Door View Controller"?
What is its class as per the identity inspector? (alt command 3)
If it's still DoorViewController, change it to the new one.
If this doesn't work, delete "Door View Controller", drag & drop a new one from the library and then change its identity.
./mgmt.xcodeproj/project.xcworkspace/xcuserdata/user.xcuserdatad/UserInterfaceState.xcuserstate and buried in the mainwindow.xib
<object class="IBUIViewController" id="578626022">
<string key="IBUINibName">DoorViewController</string>
Manually edited the xib file using a text editor and deleted the xcuserstate file got the app started. I am surprised that the IBUIViewController setting was not visible with a global search, nor within IB, however.......
Thanks sanjay.