NSCoding Prebundle Data - objective-c

I am using NSCoding to save a serialized list of my object. This object's successfully saved in the path:
/var/mobile/Applications/F923C87-360D-4B429-B2E9-CAE121009ECE5/Documents/feed_file
And I can get the object successfully when the app starts.
I'm looking to deploy the app with some contents already loaded, so I want to have a primary version of feed_file in the apps.
How can I do that, copy it the first time the app starts but not the other times?

Put a bit of code in applicationDidFinishLaunching:, check whether there is any file at the destination path (Documents/feed_file), if there isn't then copy the default file out of the bundle. The default file should be part of the Xcode project and copied to the bundle during the build.

Related

How a macOS document-based app start itself?

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.

How to build with a custom eglfs cursor atlas?

I'm trying to change the eglfs mouse cursor graphics for my embedded linux QT application (QT5.5). I have the new cursor atlas PNG and the new JSON descriptor file, but the documentation is rather vague:
".. a custom cursor atlas can be provided by setting the QT_QPA_EGLFS_CURSOR environment variable to the name of a JSON file. The file can also be embedded into the application via Qt's resource system."
I'd prefer to keep everything within the resource system if possible but I can't work out how to do it.. do I need a specific qrc file containing the path to the JSON file? I assume that the PNG file would also need to be added as a resource so that it gets built into the application?
If adding it via the resource system is a bad idea where's the correct place to set the QT_QPA_EGLFS_CURSOR environment variable? I'm currently specifying the platform on the command line via "-platform eglfs"; will this be ok or will I need to set the platform to eglfs in the build?
After much trial, error and digging around I have found the solution that I was looking for within the resource system.
Create a new resource file called "cursor.qrc", the contents of which needs to be two lines:
path/to/your/custom-cursor-atlas.png
cursor.json
The first line (path to your cursor atlas) must be relative to your resource directory.
You then need to put the JSON file (contents as described in the documentation) in the root of your resource directory. It must be called "cursor.json", and its image location line must must match the location in your new resource file and be of the format:
"image": ":/path/to/your/custom-cursor-atlas.png",
This will then include your cursor atlas in resources, and Qt will find it when your application starts.
Run time solution example:
export XDG_RUNTIME_DIR=~
export QT_QPA_EGLFS_CURSOR=~/cursor.json
In the cursor.json:
"image": "cursor.png",
Put your custom cursor.png atlas into your home dir (~) then run the Qt app from there.

How do I get the XCode Project URL programmatically?

I'm curious if there is a way to programmatically get the location of the .xcodeproj package within an Objective-C (or Swift) class contained within that package. I'd like to make a simple utility that puts files directly into the containing folder based on various app events, but I would rather avoid hard coding the path.
Essentially I want to create a target (and a reusable class) that builds swift files for NSManagedObject subclasses based on the Core Data model present in the app.
I found out the trick here is to add an item to your plist file that contains value ${PROJECT_DIR}, then you can get the location in your code with
var projectPath = NSBundle.mainBundle().infoDictionary.objectForKey("com.myapp.project_dir") as String
This assumes the plist key is "com.myapp.project_dir", of course.

Save the modified Property file inside WAR deployed on server

We are trying to use Apache PropertiesConfiguration in our project using JSF and JBoss.
My property file is located inside a package say demo.prop by the name of Prop1.prop
Inside my WAR file the same is present under /WEB-INF/classes/demo/prop/Prop1.prop
Using
FacesContext.getCurrentInstance().getExternalContext().getResource("/WEB-INF/classes/demo/prop/Prop1.prop");
I am able to fetch the property file. So when I try to extract a string from the property file using
PropertiesConfiguration pc1=new PropertiesConfiguration(a);
String s1=(String)pc1.getProperty("User_Name");
I am able to get the proper string. Using set property method I am able to set the property also.
pc1.setProperty("User_Name", "hardcodedString");
But I am not able to save the FILE back to this location. No matter what I do it is not able to save the file using pc1.save.
Can anyone please try to tell me how can I save this file back to its original location so that the changes done in the property file remain as it is.
Modifying the WAR file is a bad idea (for example, some web servers may notice the file
modification an redeploy your app, there may be problems when the server explodes the war on deployment etc.)
I would suggest applying an alternative approach - some possibilities are:
Place the properties in a database table, so they can be easily modified
Use an external properties file for overriding your "Prop1.prop" file settings. You can pull this off for example as follows. Assume that the default property values are bundled in your WAR and the modified values are saved to some other path, which is defined using a system property - let say it's called CONFIG_LOCATION. Now after loading your properties from the bundle you read them also from this external "overrides.prop" file - this overrides your defaults from "Prop1.prop":
PropertiesConfiguration pc1=new PropertiesConfiguration(a);
try(FileReader propReader = new FileReader(System.getenv().get("CONFIG_FILES") +"/overrides.prop"){
pc1.load(propReader);
}
When you need to save changes, you do that to "overrides.prop" - this will save all
the properties, not only the changed ones, but that should have no negative effects.

Clearing .png files from a Xcode project

In my Xcode project, i added a bunch of images to use. I need to change out the images so i removed them from the project and deleted them from the folder structure. They should be gone, right?
No. when i launch the program, the images are still being used. I did a search on my computer for the file name and only found it in the code
(i used a constants file and put the file names into strings:
NSString * const PRESET_LIGHT_GRAY = #"preset-rect-16.png";
NSString * const PRESET_ASH_GRAY = #"preset-rect-17.png";
and this is the only place on my mac that i find the file name).
there is no file on my mac named "preset-rect-16.png" yet Xcode is still using the image.
where is Xcode getting the images? are they stored somewhere else in the project? are they saved in the simulator somewhere?
/puzzled
Go here. Xcode -> Preferences -> Locations -> Derived Data
Wherever your Derived Data Folder is, delete Build for this Project and then run again. Resetting the Simulator is also recommended.
The images may still be referenced in your target build phases under copy bundle resources. If so delete the references there as well.