Is it possible to copy file to Main bundle? - ios

I want to copy file(image,mp3...) from main bundle to main bundle. To use
[[NSBundle mainBundle]pathForResource:#"copyname" ofType:#"mp3"]
from path = /var/mobile/Applications/62734FAF-1E94-4792-9978-exam/myproject.app/file.mp3
to path = /var/mobile/Applications/62734FAF-1E94-4792-9978-exam/myproject.app/copyname.mp3
function = copyItemAtPath
But i got the error "The operation couldn’t be completed. (Cocoa error 513.)"
Can anyone help me?

Bundles are read-only, it's not possible to write to them.
There are, however, many places where you can store your data.
You can use the Application Support Folder, or for temporary data you can also use the NSTemporaryDirectory() function to get a temporary directory.
This will also work in sandboxed applications.

Use
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager copyItemAtPath:[[NSBundle mainBundle]pathForResource:#"FileNameToBeCopied" ofType:#"mp3"]
toPath:[[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:#"NewFileName.mp3"] error:nil];

Related

Xcode Getting a Consistent Documents Directory Path

I've looked through the Apple documentation on this point and other questions here, but cannot find a means of getting a consistent path to the documents directory.
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *urls = [fm URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
NSURL *directory = [urls lastObject];
This produces a different path each time due to one component.
Example:
file:///var/mobile/Containers/Data/Application/CA708CF5-0E1B-414D-A795-31A8BB884BA5/Documents
Next run:
file:///var/mobile/Containers/Data/Application/2C96E341-85EF-485D-AC19-F8844B0880C3/Documents
I realize I need some kind of relative path here but I cannot figure out how to get it. How can I get to the Documents directory consistently to both write and read a file my app will produce?
The path is determined on installation. Each time you run your app in the simulator, it will be removed and reinstalled. Hence the differernt path. So you don't need to worry about this.

Has the Core Data database persistent storage location changed

I was getting the persistent storage error that has been reported by many others. However in my case it was not due to changing the database model. I chased that rabbit for a couple of hours. Finally one post lead me to try and delete the database itself. I went to the directory my URL pointed to
url=/Users/ccox/Library/Application Support/iPhone Simulator/7.1/Applications/8A231E05-B3A2-4E83-8C81-6B3989C262A5/Library/Documentation/DBEViewDocumentand
it didn't exist.
url=/Users/ccox/Library/Application Support/iPhone Simulator/7.1/Applications/8A231E05-B3A2-4E83-8C81-6B3989C262A5/Library/
was there but the Documentation directory was not there.
Here is the code I am using to get the URL
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *documentsDirectory =[[fileManager URLsForDirectory:NSDocumentationDirectory inDomains:NSUserDomainMask] firstObject];
NSString *documentName = #"DBEViewDocument";
self.url = [documentsDirectory URLByAppendingPathComponent:documentName];
self.document = [[UIManagedDocument alloc] initWithFileURL:self.url];
NSLog(#"url=%#",[self.url path]);
The output from the NSLog is the output URL above.
Is there a setting somewhere that sets the destination location for that documents directory? Apparently the Documentation directory is a not a standard directory since it's not created as part of the application install.
I went ahead and created the Documentation directory and now everything is working fine (at least that far), but I can't believe this is by design and that everyone is having to do this.
Any thoughts?
Thanks
Chip
You're using the wrong directory. NSDocumentationDirectory is not a standard location for documents, and is not created by default. You want NSDocumentDirectory, which is created by default.

Terminated due to memory error iOS?

I am working on a project where I need to manage videos. I need to rename or delete video. For that we need to hold the video in NSDATA and then manage it.
But I am getting an error message as Terminated due to memory error on below statement.
Edited
NSData *data=[NSData dataWithContentsOfFile:self.path];
if (data){
BOOL success = [data writeToFile:videopath atomically:NO];
}
self.path contains the path of video file. It works in small size video (of 4-10 mins) But it crashes in large size video (bigger than 20-30 mins).
Please advice.
Use this code instead loading the video file to memory, Your code will work with small files but u gonna fail with big files.
if ( [[NSFileManager defaultManager] isReadableFileAtPath:source] ){
[[NSFileManager defaultManager] copyItemAtURL:source toURL:destination error:nil];}
You are maintaing the full video as NSData inside the application. Instead of using a Video file as NSData, copy the video to some where (e.g NSTempoaryDirectoy). You can delete or rename the Old video.
I am not sure what the requirement for you. from your question I understood that you need to rename your video file. for renaming, why we need to go for reading it as NSdata and again writing the same. for Renaming try the below code.
NSFileManager *filemanager = [NSFileManager defaultManager];
if ([filemanager fileExistsAtPath:filePath])
{
NSString *target = [[filePath stringByDeletingLastPathComponent] stringByAppendingPathComponent:newnameofthefile];
[filemanager moveItemAtPath:filePath toPath:target error:nil];
}
I hope this may help you..

NSBundle "not yet loaded" for existing path

I'm trying to implement a way to change language inside the application.
I think I have it all figured it out, but for some reason the folder is not laded as a bundle
I have a folder called kh.lproj
NSString *path = [[NSBundle mainBundle] pathForResource:newLanguage ofType:#"lproj"];
if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
bundle = [NSBundle bundleWithPath:path];
DLOG(#"Language: %# from path %# (%#)", newLanguage, path, bundle);
}
The output of my dlog is
Language: kh from path ......../F1AA1E74-A014-4331-BD1B-D05D0E54AFF3/console.app/kh.lproj
(NSBundle <......./F1AA1E74-A014-4331-BD1B-D05D0E54AFF3/console.app/kh.lproj>
(not yet loaded))
On both the iPhone and in the simulator (with different paths of course).
I have checked in the .app folder and the kh.lproj folder is there. (In lower case).
Does any one have an idea of why this is happening? If i try to load a file that doesn't exist it just ignores it and don't try to load it.
EDIT
If its to any help, when I'm using loadAndReturnError:(NSError) i get the message:
NSLocalizedFailureReason=The bundle’s executable couldn’t be located.,
NSLocalizedDescription=The bundle “kh.lproj” couldn’t be loaded
because its executable couldn’t be located.,
NSBundlePath=......./A65E8399-6CDB-4CAE-9074-803125E78BBA/storeconsole.app/kh.lproj
What does this error message mean
Looks like I get what I want anyway when calling
[bundle localizedStringForKey:key value:NULL table:nil];
a possible pitfall is also, when the string file has a bug, like a missing semicolon or incomplete comment marker.
Xcode dont check the file for syntax errors.

NSFileManager contentsOfDirectoryPath returning empty

I have an images group inside my xCode project. I am trying to get all the files from inside that folder into my app.
NSFileManager *fm = [NSFileManager defaultManager];
NSArray *dirContents = [fm contentsOfDirectoryAtPath:#"/Images/" error:nil];
The above code always returns an empty array. I added the group in xCode 4.2 by right click and then "New Group".
The path you are using is probably not the correct one. /Images/ will refer to images in the root of your sandbox. If the images in your folder are added as a resource in your project, they will be stored in the Resources directory in your app, probably not even in the Images directory.
You also want to read the error to see what's wrong. That's what it is for:
NSErorr *error = nil;
NSArray *dirContents = [fm contentsOfDirectoryAtPath:#"/Images/" error:&error];
if (!dirContents) {
// inspect error
}
Use -[NSBundle resourcePath] to get a path to your applications resources directory.
Understand that your project structure is not equal to the location your resources will end up in your application. Resources are all copied to the Resources directory using your targets
Copy Bundle Resources buid phase.
If you really want to add certain files in a different directory in your resources directory add a new Copy Files phase, with a destination of Resources and a Subpath named with your folder, like Images
Then you can loop over those images using your file manager and the path you get using:
[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Images"];

Resources