I am able to record a .caf file in my app and play it back to the user. I want to be able save it permanently so I want to move it to my resources folder. I am not sure if this is the correct move or whether it would be better to put it in the documents directory but I access all my other sound files from resource so I assumed this would be the right choice. Does anyone know how to do this? Thanks!
How I save the recording to temp:
NSURL *soundFileURL=[NSURL fileURLWithPath:
[NSTemporaryDirectory()
stringByAppendingString:#"soundFile1.caf"]];
You cannot add, remove or modify files in your application bundle, which means you can't add items to the resources directory.
If the file is something the user will want to keep in the app, it should be stored in the documents directory. You may want to prevent it being backed up to iCloud, which you can do by following Apple's instructions here.
To get a path to the documents directory, or the path for a given filename in the documents directory, you can use methods like these:
+ (NSString *)pathForDocumentsDirectory
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [documentPaths objectAtIndex:0];
}
+ (NSString *)pathForFileInDocumentsDirectory:(NSString *)filename
{
return [[self pathForDocumentsDirectory] stringByAppendingPathComponent:filename];
}
You cannot add anything into your application bundle of your application while using application so add them in document or library folder of application
Related
Hey folks I have been stuck into the situation where I wanted to know the File type of few files who has no extension. I know till now that every file is associated with UTI Identifier and Extension.
1. In my case file extension is not available so no questions.
2. File has no extension.
Apple documentation over file system over O.S
One of stack overflow reference
Article over dynamic UTI
I have been here too but guess there is something I am missing or going in the anonymous direction.
Your help must be appreciated.
UTIs are designed to allow the passing of typed data between applications primarily e.g. the sending of images over the clipboard. As long as the data is tagged with the appropriate UTI, it will be available for consumers who can deal with that type of data.
The application placing the data on the pasteboard is responsible for determining the appropriate UTI(s) that are placed onto the pasteboard.
When you determine the UTI for a file, the operating system gets this from the extension that is used. This is the only source that the operating system uses to determine the UTI when it looks at a file. If there is no extension for the file, it is given the UTI public.data.
Instead, Mac OS X derives the UTI from other information, primarily—and tragically—from the file name extension.
The UTI is not stored/cached/retained in any form on the file system, except in the form of the file's extension.
The only mechanism that is available other than using the file extension is to use a tool like file, or it's library libmagic, that is part of the file program's distribution to interrogate the file to determine it's probable file type.
you can try this :
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSString *fileName = #"YOUR_FILE_NAME";
for (NSString *file in directoryContent )
{
NSString *fileNameWithOutExt= [file stringByDeletingPathExtension];
if([fileName isEqualToString:fileNameWithOutExt])
{
NSLog(#"Found");
}
}
I'm trying to get list of files in Library folder in cocoa, i use this code and NSHomeDrirectory() function without sandboxing and work well.
TempArray = [[NSFileManager defaultManager] directoryContentsAtPath:FolderURL]
but when I checked sandbox is code not work, Is it any entitlements should i add? or what code can replace?
I haven't tried if this will get all the files but it should since you can save and read files from the library.
NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *libraryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:libraryPath error:nil];
If you add below entitlement you can access ~/Library or /Library folders
com.apple.security.temporary-exception.files.home-relative-path.read-write: /Library/
But after this you will get reply from apple is
2.31:Apps that are not sandboxed appropriately may be rejected
I didn't get answer of this. If anyone knows please post here.
I have a problem accessing my files in my app.
I am currently using
//Directly from TileMap example from WWDC2010
NSString *tileDirectory = [[[NSBundle mainBundle] resourcePath]
stringByAppendingPathComponent:#"Tiles"];
to access my tiles for my MKOverlay. This gives me this directory
/Users/xxxx/Library/Application Support/iPhone Simulator/4.2/Applications/9D62025C-C53B-472C-8309-xxxx/xxxx.app/Tiles
The x's is only for privacy reasons
I have my tiles in a folder called Tiles in the root of my application which is in Xcode in a group called Tiles which is in directly in the Resources group.
When I run my app, I get a simple error saying that it could not find my tiles at the generated directory (the one quote above) If I replace that piece of code and make it:
NSString *tileDirectory = #"/Users/xxxx/Documents/xxxx/Tiles";
Then my app works fine. This is obviously because it finds my tiles in its direct location on my Mac. This is fine for testing, but I need it to work on my iPhone/iPad.
This problem might be occurring due to:
The generated directory is incorrect.
The tile images aren't getting included in the builded .app file.
Either way, I have no clue of what to do to solve it.
How can I solve this problem?
[EDIT]
I changed that piece of code to:
NSString *tileDirectory = [[NSBundle mainBundle] resourcePath];
Now it works in simulator, because all files are in the apps root folder and I don't ask for it to enter another directory called "Tiles".
This runs with no error on the simulator, but when on my iPhone it gives the original error (just a different file path but also ending with /xxxx.app
How can I ensure a directory in my app file such as xxxx.app/Tiles - TileMap does this.
Since it is your files in your app bundle, I think you can use pathForResource:ofType: to get the full pathname of your file.
Here is an example:
NSString* filePath = [[NSBundle mainBundle] pathForResource:#"your_file_name"
ofType:#"the_file_extension"];
Remember that the "folders/groups" you make in xcode, those which are yellowish are not reflected as real folders in your iPhone app. They are just there to structure your XCode project. You can nest as many yellow group as you want and they still only serve the purpose of organizing code in XCode.
EDIT
Make a folder outside of XCode then drag it over, and select "Create folder references for any added folders" instead of "Create groups for any added folders" in the popup.
If your tiles are not in your bundle, either copied from the bundle or downloaded from the internet you can get the directory like this
NSString *documentdir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *tileDirectory = [documentdir stringByAppendingPathComponent:#"xxxx/Tiles"];
NSLog(#"Tile Directory: %#", tileDirectory);
You need to use the URL for the link, such as this:
NSURL *path = [[NSBundle mainBundle] URLForResource:#"imagename" withExtension:#"jpg"];
It will give you a proper URL ref.
You need to add your tiles into your resource bundle. I mean add all those files to your project make sure to copy all files to project directory option checked.
I am creating a sample application where it downloads 100 images from
server and stores it in iPad.
In XCode, I am using the NSURL to retrieve the image file and using
NSData to save it into my local folder.
I am able to save the images in my mac air desktop folder. But I want
my application to be deployed in iPad.
So here is my question:
Where do I store the file in my iPad, so that my application can
retrieve the images when needed?
If possible can someone give me the code for saving the images in
your iPad resource directory (or whatever directory needed). Just the
code where you build the path will do good.
I know this is kind of a basic question, since I am new to objective-C, I am kind of struggling with it.
Here is an example function how to retrieve the applications documents folder. In there you can create your own folder structure. This folder is also backed up by iTunes and will be preserved when doing application updates.
NSString* GetApplicationDocumentsDirectory() {
static NSString* documentsDirectory = nil;
if (documentsDirectory == nil) {
documentsDirectory = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES)
objectAtIndex:0] retain];
}
return documentsDirectory;
}
In my humble opinion, using a Core Data store would be the best approach for this.
My question is stated above in the title but I'll write it here again
Is it possible to copy a different plist into the application bundle code wise??
This is actually a bit related on my previous question ;) Create a dictionary property list programmatically
But since I can write out the plist (and test if I'm doing something wrong ) I was wondering if I can copy my written file
[testBook writeTofile:#/Users/jack/testbook2.plist" atomically:NO];
to my application bundle in code (so that it can read from here with
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *dataPath = [path stringbyAppendingPathcomponent:#"testbook2.plist"];
Another alternative is good as well for instance if I can read my plist directly from another path instead of the mainBundle
Physically, yes it is possible sometimes.
NO, DON'T DO THAT!!
Okay... copying stuff into your App Bundle is bad, your app bundle is not where you keep settings or data. You should think of your app bundle as readonly. You should do this for several reasons:
While most of the time a user on a Mac OS X system has permission to write to an app sometimes they don't. Think about children who are not using admin accounts, or lab deployments at schools.
It will invalidate codesigning. If you sign your application this will change the signature and break the codesign, causing all sorts of weird issues (losing any firewall permissions the app has, telling the user the app has changed and asking them for keychain permission again, etc). In the future this may also pop up all sorts of warnings since tampering with an app is a big security red flag.
It won't work on iPhone. That may not be an issue, but if you every intend to use this code on an iPhone the apps bundles are sandboxed readonly
The correct place to put this sort of stuff is into a sub folder of the Application Support folder
I don't know exactly what you're doing, but depending on your application, you might want to use the Application Support directory:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
if ([paths count] > 0) {
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:#"App Name"];
// Create the directory if it doesn't exist, etc.
} else {
NSLog(#"Fail!");
// ...
}