Вопрос

У меня есть приложение, которое позволяет пользователю нажать кнопку, чтобы сохранить рингтон в каталог документов. Однако у меня версия 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];
    }
}

Я знаю, что это проблема с файловым менеджером. Как указано, он всегда входит в утверждение, как если бы файл существует (который он не). Когда я комментирую, если выясните, чтобы заставить его сохранить, приложение вылетает. Что я делаю неправильно? Есть что-то другое, что вы должны сделать с iPhone? Кроме того, я не думаю, что это проблема, но я проверяю на iPod Touch (он обновляется до последних iOS 4.2.1. У меня нет доступа к iPhone. Любая помощь будет оценена.

Это было полезно?

Решение

Код выше правильный. Если у кого-то есть та же проблема, убедитесь, что вы подключаете кнопки к правильным выходам в интерфейсе Builder.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top