将文件从iOS 11文件应用复制到沙箱 [英] Copy file from iOS 11 Files app to sandbox

查看:383
本文介绍了将文件从iOS 11文件应用复制到沙箱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文件从iOS 11文件应用复制到本地应用沙箱.为了进行测试,假定该文件在文件"应用程序中本地可用(从iCloud下载到本地存储).文件扩展名已在我的应用程序中注册,并且在文件"应用程序中按下文件时,我的应用程序会从文件"应用程序接收文件URL:

I want to copy a file from the iOS 11 Files app to my local app sandbox. For testing purposes it is assumed that the file is locally available in the Files app (downloaded from iCloud to the local storage). 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:

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?

此致

推荐答案

在这里,您可以访问文件并在完成操作后停止.

Here is how you can access the files and stop when are done with it.

//To gain access
[nsUrl startAccessingSecurityScopedResource]

//To stop access
[nsUrl stopAccessingSecurityScopedResource]

这篇关于将文件从iOS 11文件应用复制到沙箱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆