簡體   English   中英

iPhone通過文件共享保存文件時出現問題

[英]iPhone problem saving files with file sharing

我有一個允許用戶單擊按鈕以將鈴聲保存到文檔目錄的應用程序。 我的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。我們將提供任何幫助。

上面的代碼是正確的。 如果有人遇到相同的問題,請確保將按鈕連接到Interface Builder中的正確插座。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM