problema iPhone salvataggio di file con la condivisione di file
-
30-09-2019 - |
Domanda
Ho un app che permette all'utente di fare clic su un pulsante per salvare una suoneria alla directory documenti. Ho la versione iPad di lavoro in modo appropriato, tuttavia, utilizzando lo stesso metodo per l'iPhone non sembra funzionare. Ecco il codice che sto usando per salvare:
- (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];
}
}
So che il suo problema di una con il file manager. Come scritto, va sempre nella if come se il file esiste (che non è così). Quando io commento l'istruzione if fuori per forzarlo per salvare, l'applicazione si blocca. Che cosa sto facendo di sbagliato? C'è qualcosa di diverso si ha a che fare con l'iPhone? Inoltre, non credo che la sua un problema, ma sto testando su un iPod touch (è aggiornato alle più recenti iOS 4.2.1. Non ho accesso a un iPhone. Qualsiasi aiuto sarebbe apprezzato.
Soluzione
Il codice di cui sopra è corretto. Nel caso in cui qualcuno sta avendo lo stesso problema, assicuratevi di collegare i pulsanti per le prese giuste Interface Builder.