有人可以解释并提供示例代码,以便在我的iPad应用中执行以下步骤:
我该怎么做?
答案 0 :(得分:3)
要在文件中写入字符串,请使用此
NSString *str = @"your string";
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* filePath = [documentsDirectory stringByAppendingPathComponent:@"data.txt"];
NSError *error;
[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
然后在plist文件中将Application supports iTunes file sharing
键设置为YES
。
当您从那里将设备与iTunes连接时,可以保存data.txt
Here is the video how to get files from iTunes
根据要求编辑。
NSUserDefaults *deflt = [NSUserDefaults standardUserDefaults];
//this number file is saved, I'm not saving the file name as data0.txt.
//The first file to be saved is data1.txt
int num = [deflt integerForKey:@"fileNameNum"];
num++;
NSString *fileName = [NSString stringWithFormat:@"data%d.txt",num];;
NSString *str = @"your string";
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSError *error = nil;
[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (error == nil) {
//Save the number of the file that you have save in doc directory
[deflt setInteger:num forKey:@"fileNameNum"];
}