我有一个应用程序,允许用户单击按钮将铃声保存到文档目录。我的 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。任何帮助将不胜感激。