1

Is there any way to save my .xml file to another directory other than "/Users/student/Library/Application Support/iPhone/Simulator/User/Applications/..."

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"accUserNames.xml"];
 BOOL ok = [content writeToFile:appFile atomically:YES encoding:NSUnicodeStringEncoding error:nil];
if (!ok) {
    NSLog(@"Error writing file !");
}

i wish to writeToFile: my .xml file to the desktop , any idea on how?

Steve Jabs
  • 209
  • 5
  • 21

4 Answers4

2

May the below code help,

NSString *documentsDirectory = @"/Users/student/Desktop/";
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"accUserNames.xml"];
BOOL ok = [content writeToFile:appFile atomically:YES encoding:NSUnicodeStringEncoding error:nil];
if (!ok) {
    NSLog(@"Error writing file !");
}
Fox32
  • 13,126
  • 9
  • 50
  • 71
Hiren Gujarati
  • 1,039
  • 14
  • 32
1

From within the iPhone Simulator, you should be able to successfully use @"/Users/student/Desktop/accUserNames.xml" as the path to write to. However, you can't do this on an iOS device (you'll be restricted to the application's sandbox directory — it's recommended you write to the Documents folder or other folders in there, depending what type of data you're storing).


Edit: I think I understand your problem. This part of your code:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

effectively finds the path to "/Users/student/Library/Application Support/iPhone/Simulator/User/Applications/..." (this is the normal place you want to save things). But if you just want to save to the desktop temporarily, you should just use appFile = @"/Users/student/Desktop/accUserNames.xml".

Note: I don't advocate this as a long-term solution, but if you just want to see the output of your program temporarily, it works fine.

jtbandes
  • 115,675
  • 35
  • 233
  • 266
  • yeah i tried using that @"/Users/student/Desktop/accUserNames.xml" but the outcome is "/Users/student/Library/Application Support/iPhone/Simulator/User/Applications/**Users/student/Desktop/accUserNames.xml**" – Steve Jabs Jun 23 '11 at 06:16
  • @Steve Jabs: updated my answer because I think I understand what you want better. – jtbandes Jun 23 '11 at 06:23
  • yeah just trying to test out if the output can be placed in other directory, if this works then maybe i'm able to write the xml file to the server , thanks ! – Steve Jabs Jun 23 '11 at 07:06
0

Probably not with code, but you could try this: Open "Automator" (in the Utilities folder) and chose "Folder Action". As Input folder, you specify the directory of the documents (/Users/student/Library/Application Support/iPhone/Simulator/User/Applications/...) and then you select, from "Files & Folders", "Duplicate Finder Items" and "Move Finder Items" and select the Desktop. Hit "Save", give it a name, and all files in the documents folder should be copied to the desktop.

Automator

Community
  • 1
  • 1
fabian789
  • 8,348
  • 4
  • 45
  • 91
0

If you want to upload your file to a server ("www.blahblah.com" in your example), then using the write to file methods is not the correct approach. This is only for writing data to the local file system (or on a network share, but that doesn't apply to iPhones).

If you want to transfer data to a webserver, you will need to have something on the server that will listen for a connection request, then it will need to accept the data which is transferred from your app. You cannot just write a file to "www.blahblah.com" as you would to a file system

Nick Bull
  • 4,276
  • 1
  • 18
  • 25