Using a System Plug-in (.saver bundle) inside a Cocoa Application as a Resource - objective-c

I'm new with Cocoa / Objective-C development and I have a question.
Last week I had to create a SWF based Screensaver for Mac, and as I didn't find a free-compatible solution for Mac OS X Snow Leopard / Lion, I created a .saver bundle with Xcode 4. It creates inside a webview and loads inside the SWF file.
You must place the SWF file inside the Resources folder inside the bundle to make it work with different SWFs.
And now, I'm trying to code a Cocoa Application to do it automatically.
It has a simple user interface so as the user can select a SWF file. Then the code makes a copy of my previously build .saver file (I have the path hardcoded), places inside it a copy of the SWF file, and saves it where the user indicates in a save panel.
And here comes my question. Now I have the path of the .saver file hardcoded, but I need to have it as a Resource inside my app. Would it be possible? How could I use/access it?
Thanks for your help and time!

Your application already has at least one resource, assuming you didn't delete the MainMenu nib. Add your .saver bundle to that build phase. In the app's code, get the URL to the screen-saver bundle the usual way.

Related

Modify plist file in xcode

I developed an application that has a Data.plist file, created by me.
The idea is that by UISwitch Data.plist, which works in the simulator iPhone Xcode, amend does not work on an actual device.
Why is that ?
How I can make it work?
Stay tuned to your answer,
Greetings.
Your bundle is read-only on a device.
The best solution would be to load your plist, save it in a directory of your chosing using the NSFileManager, and then work with the file in that directory.
Your plist inside the bundle would ONLY be used the very first time your user uses the app, as a blueprint if you will, and then you would work with the .plist you saved in the documents directory.
This small code snippet will explain how to copy a file from your bundle safely. He uses it with a database but it's really the same for any kind of file.

Files removed from Xcode project, but still available in game

My cocos2d-iphone project has tiled maps (.tmx files along with .png textures).
Recently, I deleted all the .png textures in my project (in Xcode, you hit the delete key and then choose to send to trash).
Then, I ran the project, and it tried to load up the CCTMXTiledMap. I was expected it to crash (there shouldn't be any textures in the project, anyway), but it didn't. In fact, it ran just fine, and the maps did load. No idea why - is Xcode keeping some kind of reference to such files somehow?
I checked the .tmx file, and this is the line that defines the source image:
<image source="myMap-hd.png" width="973" height="1024"/>
There definitely is no myMap-hd.png nor myMap.png file in my project folder. Not even Xcode's search toolbar shows it!
Oh, and I also cleaned the project.
As I wrote in question's comment. Problem was not in your Xcode project. Deleted resource files will most likely be still available on device/simulator if you had run application with these resources previously. The reason is that Xcode doesn't track files that are in application bundle, they're just simply copied. That's why they land in Copy Bundle Resources build phase.
You should delete application from device/simulator and run it again. Or if you have precious data already in application, you could navigate to application bundle and delete file manually. Files won't appear again.
In the simulator, iOS Simulator (menu item) > Reset Content and Settings. Clean your project, then run again. Same thing happens without cocos2D.
I've found this to be an issue when nib files are automagically loaded, and while not in the referenced files seem to still be loaded (or attempted). Can cause some brain pain bc the files don't appear in the Xcode IDE though they do exist in the file system.

data file location on iPad

I've been teaching myself over the last couple of weeks by typing in programs from the iPad books I bought (Backlin's, SAM's, Apps for Dummies, etc.) and YouTube tutorials. Still, there are a couple of things I haven't grasped intuitively. Do you mind helping me out?
I got a program working that saves and retrieves names and phone numbers from pList files. I looked on the Mac's HD and couldn't find the file (Contacts.plist), even though the program was working. I finally discovered it at
~/Library/Application Support/iPhone Simulator
I'm not sure why Finder didn't locate it. My own app needs to load a data file when it starts (questions and answers, for example). Do I write a program to create the Q & A file, (I could modify that phone program to do that) then copy the file into the simulator's virtual directory for my own app? Or do I copy that file into the XCODE Resources folder? Would the data files then travel with the finished executable program?
Sorry to sound so clueless. Thanks for any info. -Rob
If you want to deliver a file with your finished application, you must add it to your Xcode project. It doesn't matter whether you place it under Resources or in another group in Xcode as Xcode will by default copy all non-code files that are in your project into your finished app bundle.
To access this file from your code, you need to retrieve its path:
NSString *fullPathToContactsFile = [[NSBundle mainBundle] pathForResource:#"Contacts" ofType:#"plist"];
Note that unlike on the iOS Simulator, your app bundle on the device is read only, so if you want your app to make changes to the file, you cannot save it in the bundle itself. In that case, your app should copy the file from your bundle to your app's Documents or Library directory on first launch and then open/save it from/to that location.

Working Directory in Objective-C and Xcode: debug mode vs. executable

I am writing a program in Objective-C using Xcode. My program creates a file as follows:
[#"" writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:NULL];
I would like the file to be created in the same directory as the executable. When I run the program from Xcode, the file is created in the debug directory as expected.
However, when I run the .app file, the file is created in the root directory. How can I get the program to create a file in the directory where the .app file is located.
Thanks a lot.
EDIT: This is a MacOS application
EDIT2: Well, it seems that I shouldn't be writing to the .app directory. Thanks bbum and Paul R. What is the proper way to do it? To be more concrete, here's what I am doing: each time the user clicks a button in the application, a piece of hardware connected to a serial port will send a bunch data which will be written to a new file. This can happen any number of times while the application is running, so numerous files may be created. I would like them all created in the same folder.
You must never make any assumptions about the initial working directory for your application, as this will depend on what method was used to launch it (e.g. Finder, Terminal (via open), Xcode, gdb, third party utility, etc). You should use an appropriate API to find a suitable directory to store temporary files or user-specific files or whatever it is you need to do. This should never be within the app's bundle and never at a path that is relative to the initial working directory.
You do not want the file to be created inside the .app wrapper. That is never the right answer; your application may easily be installed somewhere where the current user does not have write access to the YourApp.app wrapper.
(For example, my main user account is non-admin and all applications are installed admin-write-only. If an app ever fails to work because it can't write to its app wrapper, the app goes in the trash.)
See this question for an outline of where files should be stored. Depends on the role of the file.

NSTask setLaunchPath Objective-C Cocca

This is a very simple question. I have a script in the same folder as the Cocoa app. I can't seem to set the path to it properly for setLaunchPath for NSTask. Help please.
I have a folder called Project. Inside of it, exist multiple folders but only two we care about: Classes (the source files for the Cocoa app are here) and Ruby (which is a ruby server folder). I am trying to call Ruby/script/server. I assumed it would be something like ./Ruby/script/server or Ruby/script/server but both are wrong.
Thanks.
EDIT: I guess what I'm actually asking is if there is a way to access the folder where the source files or the project is by using a special character or shortcut because by default it goes to the /tmp folder.
The current directory (in the unix sense) in an app is not guaranteed to be anything. The path to the app itself can be obtained by getting the main app bundle by
NSBundle*mainBundle=[NSBundle mainBundle];
and then getting its path
NSString*path=[mainBundle bundlePath];
However please don't do that; you won't be able to distribute your app without extra instructions of putting files here and there.
Instead, put your Ruby code inside yourApp.app/Contents/Resources/. This can be done by including the Ruby code in the XCode, and making sure it's set to be copied into the app. The files inside this Resources directory can be obtained as follows:
NSString*path=[mainBundle pathForResource:#"rubyServer" ofType:#"rb"];
To learn more about the bundle structure, read Bundle Programming Guide.