我有一个应用程序,允许用户单击按钮将铃声保存到文档目录。我有适当的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。任何帮助将不胜感激。
答案 0 :(得分:0)
上面的代码是正确的。如果有人遇到同样的问题,请确保将按钮连接到Interface Builder中的正确插座。