iPhone 通过文件共享保存文件时出现问题
我有一个应用程序,允许用户单击按钮将铃声保存到文档目录中。我的 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。任何帮助将不胜感激。
I have an app that allows the user to click a button to save a ringtone to the documents directory. I have the iPad version working appropriately, however, using the same method for the iPhone doesn't seem to be working. Here is the code I'm using to save:
- (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];
}
}
I know its a problem with the file manager. As posted, it always goes into the if statement as if the file exists (which it doesn't). When I comment the if statement out to force it to save, the app crashes. What am I doing wrong? Is there something different you have to do with the iPhone? Also, I don't think its a problem, but I am testing on an iPod touch (it is updated to the latest iOS 4.2.1. I don't have access to an iPhone. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上面的代码是正确的。如果有人遇到同样的问题,请确保将按钮连接到 Interface Builder 中的正确插座。
The code above is correct. In case someone is having the same problem, make sure you connect your buttons to the right outlets in Interface Builder.