iPhone 通过文件共享保存文件时出现问题

发布于 2024-10-11 01:43:43 字数 2091 浏览 3 评论 0原文

我有一个应用程序,允许用户单击按钮将铃声保存到文档目录中。我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦明 2024-10-18 01:43:43

上面的代码是正确的。如果有人遇到同样的问题,请确保将按钮连接到 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文