iOS 在 public 文件夹中创建文件

iOS Creating a file in a public folder

我想让用户选择我要保存新文件的文件夹。

为此,我使用文档选择器,并将文档类型设置为 public.folder 和 inMode UIDocumentPickerModeOpen

用户打开文档选择器并选择所需的文件夹后,在 didPickDocumentsAtURLs 回调中我得到 NSUrl 对象,它有权修改 url 处的文件(在本例中,它是url 到文件夹)。

这是我的问题。我有 url 对文件夹的访问权限,但是,要创建一个文件,我通常需要在 url 中有 filename.extension。如果我要修改我从文档选择器收到的 NSUrl 对象,或将其转换为 NSString,我猜我会失去访问权限并且 createFileAtPath 方法总是失败。

我需要使用什么方法,或者我需要什么配置文档选择器,才能在用户选择的路径中创建一个新文件?我附上我当前的代码:

- (void)openDocumentPicker:(NSString*)pickerType
{
    //Find the current app window, and its view controller object
    UIApplication* app = [UIApplication sharedApplication];
    UIWindow* rootWindow = app.windows[0];
    UIViewController* rootViewController = rootWindow.rootViewController;
    
    //Initialize the document picker
    UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[pickerType] inMode:UIDocumentPickerModeOpen];

    //Assigning the delegate, connects the document picker object with callbacks, defined in this object
    documentPicker.delegate = self;

    documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;

    //Call the document picker, to the view controller that we've found before
    [rootViewController presentViewController:documentPicker animated:YES completion:nil];
}


- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls
{
    //If we come here, user successfully picked a file/folder

    [urls[0] startAccessingSecurityScopedResource]; //Let the os know we're going to use the file
        
    NSFileManager *fileManager = [NSFileManager defaultManager];

    NSString *documentsDirectory = urls[0].absoluteString;

    NSString *newFilePath = [documentsDirectory stringByAppendingPathComponent:@"test.txt"];
    NSError *error = nil;
        
    if ([fileManager createFileAtPath:newFilePath contents:[@"new file test" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil]){
        NSLog(@"Create Sucess");
    }
    else{
        NSLog(@"Create error: %@", error);
    }

    [urls[0] stopAccessingSecurityScopedResource]; //Let the os know we're done
}

如有任何线索,我们将不胜感激!

这是swift中的解决方案,如有问题请尝试告诉我

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]){
    
    var imgData: Data?
    if let url = urls.first{
        imgData = try? Data(contentsOf: url)
        do{
            let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString
            let destURLPath = documentDirectory.appendingPathComponent(url.lastPathComponent)
            try imgData?.write(to: URL(fileURLWithPath: destURLPath))
            print("FILES IS Writtern at DOcument Directory")
        }catch{
            
        }
        
        
    }
    
}

为了回答我自己的问题,我将在下面留下完整的工作代码。

我的主要问题是,当您使用“public.folder”文档类型时,您需要使用所选文件夹的 url 调用 startAccessingSecurityScopedResource,而不是已修改 link(用户选择的文件 + NewFileName.extension)

- (void)openDocumentPicker
{
    //This is needed, when using this code on QT!
    //Find the current app window, and its view controller object
    /*
    UIApplication* app = [UIApplication sharedApplication];
    UIWindow* rootWindow = app.windows[0];
    UIViewController* rootViewController = rootWindow.rootViewController;
    */

    //Initialize the document picker. Set appropriate document types
    //When reading: use document type of the file, that you're going to read
    //When writing into a new file: use @"public.folder" to select a folder, where your new file will be created
    UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.folder"] inMode:UIDocumentPickerModeOpen];

    //Assigning the delegate, connects the document picker object with callbacks, defined in this object
    documentPicker.delegate = self;

    documentPicker.modalPresentationStyle = UIModalPresentationFormSheet;

    //In this case we're using self. If using on QT, use the rootViewController we've found before
    [self presentViewController:documentPicker animated:YES completion:nil];
}

- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls
{
    //If we come here, user successfully picked a single file/folder

    //When selecting a folder, we need to start accessing the folder itself, instead of the specific file we're going to create
    if ( [urls[0] startAccessingSecurityScopedResource] ) //Let the os know we're going use this resource
    {
        //Write file case ---
    
        //Construct the url, that we're going to be using: folder the user chose + add the new FileName.extension
        NSURL *destURLPath = [urls[0] URLByAppendingPathComponent:@"Test.txt"];
    
        NSString *dataToWrite = @"This text is going into the file!";
    
        NSError *error = nil;
    
        //Write the data, thus creating a new file. Save the new path if operation succeeds
        if( ![dataToWrite writeToURL:destURLPath atomically:true encoding:NSUTF8StringEncoding error:&error] )
            NSLog(@"%@",[error localizedDescription]);

    
        //Read file case ---
        NSData *fileData = [NSData dataWithContentsOfURL:destURLPath options:NSDataReadingUncached error:&error];
    
        if( fileData == nil )
            NSLog(@"%@",[error localizedDescription]);
    
        [urls[0] stopAccessingSecurityScopedResource];
    }
    else
    {
        NSLog(@"startAccessingSecurityScopedResource failed");
    }
}

Apple 论坛也讨论了这个问题:

线程名称:“iOS 在 public 文件夹中创建文件”

话题link: https://developer.apple.com/forums/thread/685170?answerId=682427022#682427022