Question

J'ai une application qui permet à l'utilisateur de cliquer sur un bouton pour enregistrer une sonnerie sur le répertoire des documents. J'ai la version iPad de travail appropriée, cependant, en utilisant la même méthode pour l'iPhone ne semble pas fonctionner. Voici le code que je utilise pour enregistrer:

- (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];
    }
}

Je sais que ce problème avec un gestionnaire de fichiers. Comme affiché, il va toujours dans l'instruction if comme si le fichier existe (ce qui ne fonctionne pas). Quand je commente l'instruction if out pour le forcer à sauver, l'application se bloque. Qu'est-ce que je fais mal? Est-il différent de quelque chose que vous avez à faire avec l'iPhone? En outre, je ne pense pas que son problème, mais je teste sur un iPod touch (il est mis à jour les dernières iOS 4.2.1. Je n'ai pas accès à un iPhone. Toute aide serait appréciée.

Était-ce utile?

La solution

Le code ci-dessus est correct. Au cas où quelqu'un est le même problème, assurez-vous de connecter vos boutons aux sorties droite dans Interface Builder.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top