I am trying to upload audio file like session1.mp3 from document directory to iCloud using the following code
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"session1.mp3"];
NSURL* sourceURL = [[NSURL alloc] initFileURLWithPath:path];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:path]) {
NSLog(#"File found!");
}
else
{
NSLog(#"File not found!");
}
NSURL *ubiq = [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];
if (ubiq)
{
NSLog(#"iCloud access at %#", ubiq);
NSError* error = nil;
NSURL *destinationURL = [[ubiq URLByAppendingPathComponent:#"Documents"] URLByAppendingPathComponent:#"session1.mp3"];
[[NSFileManager defaultManager] setUbiquitous:YES
itemAtURL:sourceURL
destinationURL:destinationURL
error:&error];
if(error != nil)
{
NSLog(#"Error while uploading the file: %#", error);
}
else
{
NSLog(#"File ulpoaded successfully");
}
}
else
{
NSLog(#"No iCloud access");
}
The file i am trying to upload exists (the "File found" is printed), but the uploading it to iCloud generates the following error
Error while uploading the file: Error Domain=NSCocoaErrorDomain Code=513 "The operation couldn’t be completed. (Cocoa error 513.)" UserInfo=0x1f03d9f0 {NSURL=file://localhost/var/mobile/Applications/20D82CDA-021E-4067-B9AB-C0197A6FA834/dox.app/session1.mp3, NSUnderlyingError=0x1f03d990 "The operation couldn’t be completed. Operation not permitted"}
Nothing inherently wrong with your code.
Are you checking the ubiquityIdentityToken anywhere to make sure it is available?
[ [ NSFileManager defaultManager ] ubiquityIdentityToken ];
Also, in your logging of a successful setUbiquitous, "upload successful" is not exact: the item was simply moved to the ubiquity container, and will be uploaded when the demon feels like it. You can check on the upload status through NSMetadataQuery.
You are not supposed to use iCloud to store this media.
Please review the Guidelines given by Apple.
Related
I need to copy a text file from an URL and place / overwrite it in my app's document folder and then read it back to a data variable.
I have the following code:
NSData *data;
//get docsDir
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsDir=[paths objectAtIndex:0];
//get path to text.txt
NSString *filePath=[docsDir stringByAppendingPathComponent:#"text.txt"];
//copy file
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
if([fileManager fileExistsAtPath:filePath]==YES){
[fileManager removeItemAtPath:filePath error:&error];
}
NSString *urlText = #"http://www.abc.com/text.txt";
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSFileManager *fileManager=[NSFileManager defaultManager];
[fileManager copyItemAtPath:urlText toPath:filePath error:NULL];
}
//Load from file
NSString *myString=[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];
//convert string to data
data=[myString dataUsingEncoding:NSUTF8StringEncoding];
It builds and complies in good way but I cannot create the text.txt file in my document folder and then pass anything to my data variable.
I'm a newbie to both IOS and Xcode, any clues will be highly appreciated. Thanks!!
NSFileManager can only handle local paths. It won't do anything useful if you give it a URL.
copyItemAtPath:toPath:error: takes an error parameter. Use it, like this:
NSError *error;
if (![fileManager copyItemAtPath:urlText toPath:filePath error:&error]) {
NSLog(#"Error %#", error);
}
You would then get this error:
Error Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be
completed. (Cocoa error 260.)" UserInfo=0x9a83c00 {NSFilePath=http://www.abc.com/text.txt,
NSUnderlyingError=0x9a83b80 "The operation couldn’t be completed.
No such file or directory"}
It can't read the file at http://www.abc.com/text.txt, because it is not a valid path.
as Sunny Shah stated without explanation you have to fetch the object at the URL first:
NSString *urlText = #"http://www.abc.com/text.txt";
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath])
{
NSURL *url = [NSURL URLWithString:urlText];
NSError *error;
NSData *data = [[NSData alloc] initWithContentsOfURL:url options:0 error:&error];
if (!data) { // check if download has failed
NSLog(#"Error fetching file %#", error);
}
else {
// successful download
if (![data writeToFile:filePath options:NSDataWritingAtomic error:&error]) { // check if writing failed
NSLog(#"Error writing file %#", error);
}
else {
NSLog(#"File saved.");
}
}
}
Always check for errors!
You should get the data from the URL and use WriteToFile
NSData *urlData = [NSData dataWithContentsOfURL: [NSURL URLWithString:urlText]];
[urlData writeToFile:filePath atomically:YES];
I am facing problem to move image file in Cache Directory. My requirement is when I am deleting one file for example image3.png then all other files (suppose there is 9 files like image1.png,image2.png,.....image9.png)after that position (image4.png,....,image9.png) will move to their previous position. I want to rename that files like image4.png to image 3.png.
Below is my code
NSError *error=nil;
NSString *imgName = [NSString stringWithFormat:#"image%d.png",3];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:imgName];
[fileManager removeItemAtPath: fullPath error:NULL];
for (int i=3; i<=9 ; i++) {
NSString *imgName1 = [NSString stringWithFormat:#"image%d.png",i];
NSString *getImagePath1 = [documentsDirectory stringByAppendingPathComponent:imgName1];
NSString *imgName2 = [NSString stringWithFormat:#"image%d.png",i+1];
NSString *getImagePath2 = [documentsDirectory stringByAppendingPathComponent:imgName2];
if ([fileManager moveItemAtPath:getImagePath2 toPath:getImagePath1 error:&error] != YES)
NSLog(#"Unable to move file: %#", [error localizedDescription]);
// Show contents of Documents directory
NSLog(#"Documents directory: %#",
[fileManager contentsOfDirectoryAtPath:documentsDirectory error:&error]);
}
But its showing following error
Unable to move file: The operation couldn’t be completed. (Cocoa error 516.)
Please help me .
Thanks in advance.
From NSFileManager docs on moveItemAtPath:toPath:error::
If an item with the same name already exists at dstPath, this method
aborts the move attempt and returns an appropriate error
Delete the file you want to overwrite first.
I am getting a file error when i am trying to write a string into a file:
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths objectAtIndex: 0];
NSString *docFile = [docDir stringByAppendingPathComponent:fileName];
if ([file fileExistsAtPath: docFile] == YES)
{ if(![formatedlogStr writeToFile:LOG_FILE_V1 atomically:YES encoding:NSUTF8StringEncoding error:&error])
{
NSLog(#"Error: %#", [error userInfo]);
}
}
this gives me an error :
Error: {
NSFilePath = "error_log_V1.txt";
NSUnderlyingError = "Error Domain=NSPOSIXErrorDomain Code=13 \"The operation couldn\U2019t be completed. Permission denied\"";
NSUserStringVariant = Folder;
How to solve the problem I don't know.
Can anybody help me?
You're correctly finding a path within your documents directory, but you are trying to log the information to a bare path. You should follow the same procedure to build a path to a file within your sandbox for the log file and use that instead of LOG_FILE_V1.
I am trying to write a very basic text string to my documents directory and work from there to later save other files etc.
I am currently stuck with it not writing anything into my Documents directory
(In my viewDidLoad)
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
NSString *documentsDirectory = [pathArray objectAtIndex:0];
NSString *textPath = [documentsDirectory stringByAppendingPathComponent:#"file1.txt"];
NSString *text = #"My cool text message";
[[NSFileManager defaultManager] createFileAtPath:textPath contents:nil attributes:nil];
[text writeToFile:textPath atomically:NO encoding:NSUTF8StringEncoding error:NULL];
NSLog(#"Text file data: %#",[[NSFileManager defaultManager] contentsAtPath:textPath]);
This is what gets printed out:
2011-06-27 19:04:43.485 MyApp[5731:707] Text file data: (null)
If I try this, it also prints out null:
NSLog(#"My Documents: %#", [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:NULL]);
What have I missed or am I doing wrong while writing to this file?
Might it be something I need to change in my plist or some frameworks/imports needed?
Thanks
[EDIT]
I passed a NSError object through the writeToFile and got this error:
Error: Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0x12aa00 {NSFilePath=/var/mobile/Applications/887F4691-3B75-448F-9384-31EBF4E3B63E/Documents/file1.txt, NSUnderlyingError=0x14f6b0 "The operation couldn’t be completed. Not a directory"}
[EDIT 2]
This works fine on the simulator but not on my phone :/
Instead of using NSFileManager to get the contents of that file, try using NSString as such:
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
NSString *documentsDirectory = [pathArray objectAtIndex:0];
NSString *textPath = [documentsDirectory stringByAppendingPathComponent:#"file1.txt"];
NSError *error = nil;
NSString *str = [NSString stringWithContentsOfFile:textPath encoding:NSUTF8StringEncoding error:&error];
if (error != nil) {
NSLog(#"There was an error: %#", [error description]);
} else {
NSLog(#"Text file data: %#", str);
}
Edit: Added error checking code.
How are you getting documentsDirectory?
You should be using something like this:
NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)
NSString *documentsDirectory = [pathArray lastObject];
Put an NSLog statement after every line where you are setting a variable's value, so that you can inspect those values. This should help you quickly pinpoint where things start to go wrong.
The problem got solved by setting a non standard Bundle ID in die info.plist
I used the Bundle ID from iTunes Connect for this specific app. Now everything works perfectly.
You can also use NSFileHandle for writing data in file and save to document directory:
Create a variable of NSFileHandle
NSFileHandle *outputFileHandle;
use the prepareDataWrittenHandle function with passing file name in parameter with file extension
-(void)prepareDataWrittenHandle:(NSString *)filename
{
//Create Path of file in document directory.
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *outputFilePath = [documentsDirectory stringByAppendingPathComponent:filename];
//Check file with above name is already exits or not.
if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath] == NO) {
NSLog(#"Create the new file at outputFilePath: %#", outputFilePath);
//Create file at path.
BOOL suc = [[NSFileManager defaultManager] createFileAtPath:outputFilePath
contents:nil
attributes:nil];
NSLog(#"Create file successful?: %u", suc);
}
outputFileHandle = [NSFileHandle fileHandleForWritingAtPath:outputFilePath];
}
then write the string value to file as:
//Create a file
[self prepareDataWrittenHandle:#"file1.txt"];
//String to save in file
NSString *text = #"My cool text message";
//Convert NSString to NSData to save data in file.
NSData* data = [text dataUsingEncoding:NSUTF8StringEncoding]
//Write NSData to file
[_outputFileHandle writeData:data];
//close the file if written complete
[_outputFileHandle closeFile];
at the end of file written you should close the file.
You can also check the content written in file as NSString for debug point of view as mention above by #Glenn Smith:
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *outputFilePath = [documentsDirectory stringByAppendingPathComponent:#"file1.txt"];
NSError *error;
NSString *str = [NSString stringWithContentsOfFile:outputFilePath encoding:NSUTF8StringEncoding error:&error];
if (error != nil) {
NSLog(#"There was an error: %#", [error description]);
} else {
NSLog(#"Text file data: %#", str);
}
In my app I want to copy a file from my bundle to documents directory in order to modify it.
Here's my code:
+(BOOL) copyDB: (NSString*) pdbName {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES) objectAtIndex: 0];
dbPath = [dbPath stringByAppendingPathComponent: #"myApp"];
dbPath = [dbPath stringByAppendingPathComponent: #"Profiles"];
dbPath = [dbPath stringByAppendingPathComponent: #"ES-EN"];
dbPath = [dbPath stringByAppendingPathExtension: #"prof"];
if([fileManager fileExistsAtPath: dbPath]) {
DebugLog(#"file exists at path %#", dbPath);
return FALSE;
}
NSString *defaultDBPath = [ [ [NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: #"ES-EN"];
defaultDBPath = [defaultDBPath stringByAppendingPathExtension: #"prof"];
DebugLog(#"dbPath: %#", dbPath);
DebugLog(#"defaultDBPath: %#", defaultDBPath);
if(![fileManager fileExistsAtPath: defaultDBPath]) {
DebugLog(#"Cannot find resource file");
return FALSE;
}
BOOL success = [fileManager copyItemAtPath: defaultDBPath toPath: dbPath error: &error];
if (!success) {
DebugLog(#"!success: Failed to create writable database file with message '%#'.", [error localizedDescription]);
UIAlertView *msg = [[UIAlertView alloc]
initWithTitle:#"Error"
message: [NSString stringWithFormat: #"Failed to create writable database file with message '%#'.", [error localizedDescription] ]
delegate:self cancelButtonTitle:#"OK"
otherButtonTitles:nil, nil];
[msg show];
[msg release];
return FALSE;
}
[ [UserProfile sharedUserProfile] writeInitialData: pdbName];
//Creating fruits folder
NSString *dicDir = [Fruits_Dir stringByAppendingPathComponent: pdbName];
//Does a directory exist?
if ( ![fileManager fileExistsAtPath: dicDir] )
{
//Create a directory
DebugLog(#"Creating userName dir");
if (![fileManager createDirectoryAtPath: dicDir attributes:nil])
{
DebugLog(#"failed to create dicDir");
}
}
return TRUE;
}
I suppose that this code should work. Somehow it works as it should on the simulator, but it doesn't work on the device. I get a log that the resource file cannot be found.
What may be the problem?
Thanks.
One way that this can happen is for the resource file to have been accidentally removed as a reference from your XCode project.
If the file was once there then likely it has been installed into the simulator app directory. Even after removing it from XCode the file will stay there and your app will work in the simulator.
But as soon as you install it clean on the device the file will be missing.
Confirm that the file is listed in your XCode project resources and completely remove the app from the simulator and try it there again to get everything in sync.