简体   繁体   English

将文件从iOS 11文件应用复制到沙箱

[英]Copy file from iOS 11 Files app to sandbox

I want to copy a file from the iOS 11 Files app to my local app sandbox. 我想将文件从iOS 11文件应用复制到本地应用沙箱。 For testing purposes it is assumed that the file is locally available in the Files app (downloaded from iCloud to the local storage). 为了进行测试,假定该文件在“文件”应用程序中本地可用(从iCloud下载到本地存储)。 The file extension is registered with my app and when a file is pressed in the Files app then my app receives the file URL from the Files app: 文件扩展名已在我的应用程序中注册,并且在“文件”应用程序中按下文件时,我的应用程序会从“文件”应用程序接收文件URL:

NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];

NSURL *nsUrl; // comes from Files app. For instance "file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/test.rar"
NSURL *targetUrl; // file in my app's document directory

NSError *coordinatorError = nil;
[fileCoordinator coordinateReadingItemAtURL:nsUrl options:NSFileCoordinatorReadingWithoutChanges error:&coordinatorError byAccessor:^(NSURL *newURL) 
{   
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //if ([fileManager fileExistsAtPath: [nsUrl path]])
    {
        NSLog(@"Copy from %@ to %@", newURL, targetUrl);

        NSError *copyError = nil;
        [fileManager copyItemAtURL:newURL toURL:targetUrl error:&copyError];
        if (!copyError)
        {
            // OK
        }
        else
        {
            NSLog(@"Files app error: %@", copyError);
        }
    }
}];

But the operation fails with this output: 但是操作失败,显示以下输出:

2017-11-22 09:30:28.685127+0100 test[434:40101] Copy from file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/test.rar
 to file:///var/mobile/Containers/Data/Application/01BB33E6-2790-0FD0-8270-000/Documents/test.rar
2017-11-22 09:30:28.687174+0100 test[434:40101] Files app error: Error Domain=NSCocoaErrorDomain Code=257 "The file “test.rar” couldn’t be 
opened because you don’t have permission to view it." 
UserInfo={NSFilePath=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/test.rar, 
NSUnderlyingError=0x1c084abf0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

Is there something special required to get read access to the external file? 获得对外部文件的读取访问权是否有特殊要求?

Regards, 问候,

Here is how you can access the files and stop when are done with it. 这是您可以访问文件并在完成后停止的方式。

//To gain access
[nsUrl startAccessingSecurityScopedResource]

and

//To stop access
[nsUrl stopAccessingSecurityScopedResource]

i have the same issue to Copy file from iOS 11 Files app to sandbox . 我有将iOS 11 Files应用程序中的文件复制到沙箱的相同问题。 finally i solved my issue this link check here 终于我解决了我的问题,这个链接在这里检查

and the sample code . 和示例代码。

[fileURL startAccessingSecurityScopedResource];//fileURL ---> Which FileURL you want to copy

                NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];

                NSFileAccessIntent *readingIntent = [NSFileAccessIntent readingIntentWithURL:fileURL options:NSFileCoordinatorReadingWithoutChanges];

                [fileCoordinator coordinateAccessWithIntents:@[readingIntent] queue:[NSOperationQueue mainQueue] byAccessor:^(NSError *error) {

                    NSData *filePathData;

                    if (!error)
                    {
                        // Always get URL from access intent. It might have changed.
                        NSURL *safeURL = readingIntent.URL;

                     // here your code to do what you want with this 

                    }

                    [fileURL stopAccessingSecurityScopedResource];

                }];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM