У меня есть приложение, которое позволяет пользователю нажимать кнопку, чтобы сохранить мелодию звонка в каталоге документов. У меня версия для iPad работает должным образом, однако использование того же метода для iPhone, похоже, не работает. Вот код, который я использую для сохранения:
- (IBAction) saveFile:(id)sender {
// Get the path to the Documents Directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// Detect which button was double tapped and set up the file to save
NSString *filePath;
NSString *fileName;
if (sender == downloadRingtone1){
filePath = [[NSBundle mainBundle] pathForResource:@"RingtoneTest" ofType:@"m4r"];
fileName = @"RingtoneTest.m4r";
} else if (sender == downloadRingtone2){
filePath = [[NSBundle mainBundle] pathForResource:@"RingtoneTest2" ofType:@"m4r"];
fileName = @"RingtoneTest2.m4r";
NSString *m4rPath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
// Detect if the file already exists
BOOL fileExists = [fileManager fileExistsAtPath:m4rPath];
if (fileExists) {
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"File Already Saved" message:@"The selected file already exists. Sync to iTunes to finish loading the file." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[alert show];
} else {
// Save the file
[fileManager copyItemAtPath:filePath toPath:m4rPath error:nil];
// Notify of the save
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"File Saved" message:@"You must sync your device to iTunes to finish loading. For complete instructions, visit the \"Download Instructions\" page." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[alert show];
}
}
Я знаю, что это проблема с файловым менеджером. После публикации он всегда входит в оператор if, как будто файл существует (чего не существует). Когда я комментирую оператор if для сохранения, приложение вылетает. Что я делаю неправильно? Есть ли что-то другое, что вы должны сделать с iPhone? Кроме того, я не думаю, что это проблема, но я тестирую на iPod touch (он обновлен до последней версии iOS 4.2.1. У меня нет доступа к iPhone. Любая помощь будет признательна.