英文:
Objective-C NSFileManager works fine on simulator but Operation not permitted error Domain=NSCocoaErrorDomain Code=257 on device when check file size
问题
我需要检查Objective-C中的外部文件大小,该文件被共享到我的React Native应用程序中。
我已经获得了一个URL,在没有获取文件属性的情况下正常工作:
file:///private/var/mobile/Containers/Shared/AppGroup/61894F8B-0001-40C2-856E-F30D4663F7A7/File%20Provider%20Storage/Instrukcja_Welcome_to.pdf
以及
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:url.relativePath error:&error];
NSNumber *fileSize = fileAttributes[NSFileSize];
在模拟器上完美运行,但当我使用物理设备时,我收到了错误:
Error Domain=NSCocoaErrorDomain Code=257 "The file
“Instrukcja_Welcome_to.pdf” couldn’t be opened because you don’t have
permission to view it."
UserInfo={NSFilePath=/private/var/mobile/Containers/Shared/AppGroup/61894F8B-0001-40C2-856E-F30D4663F7A7/File
Provider Storage/Instrukcja_Welcome_to.pdf,
NSUnderlyingError=0x280eeb240 {Error Domain=NSPOSIXErrorDomain Code=1
“Operation not permitted”}}
我尝试了以下操作:
NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
[fileCoordinator coordinateReadingItemAtURL:url options:NSFileCoordinatorReadingWithoutChanges error:&error byAccessor:^(NSURL *newURL) {
fileAccessGranted = YES;
}];
并且在info.plist中添加了:
<key>NSFileUsageDescription</key>
<string>This app requires access to files.</string>
英文:
I need to check size with Objective-C of external file which is shared to my react native app.
I got url which works fine without getting file attributes:
file:///private/var/mobile/Containers/Shared/AppGroup/61894F8B-0001-40C2-856E-F30D4663F7A7/File%20Provider%20Storage/Instrukcja_Welcome_to.pdf
and
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:url.relativePath error:&error];
NSNumber *fileSize = fileAttributes[NSFileSize];
works perfectly on simulator but when I use physical device I got error:
> Error Domain=NSCocoaErrorDomain Code=257 "The file
> “Instrukcja_Welcome_to.pdf” couldn’t be opened because you don’t have
> permission to view it."
> UserInfo={NSFilePath=/private/var/mobile/Containers/Shared/AppGroup/61894F8B-0001-40C2-856E-F30D4663F7A7/File
> Provider Storage/Instrukcja_Welcome_to.pdf,
> NSUnderlyingError=0x280eeb240 {Error Domain=NSPOSIXErrorDomain Code=1
> "Operation not permitted"}}
I tried:
NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
[fileCoordinator coordinateReadingItemAtURL:url options:NSFileCoordinatorReadingWithoutChanges error:&error byAccessor:^(NSURL *newURL) {
fileAccessGranted = YES;
}];
and added
<key>NSFileUsageDescription</key>
<string>This app requires access to files.</string>
to info.plist
答案1
得分: 1
以下是翻译好的部分:
解决方案原来非常简单:
NSURL *url = [NSURL fileURLWithPath:@"/private/var/mobile/Containers/Shared/AppGroup/61894F8B-0001-40C2-856E-F30D4663F7A7/File Provider Storage/Downloads/Instrukcja_Welcome_to.pdf"];
BOOL isAccessGranted = ;
if (isAccessGranted) {
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:url.relativePath error:&error];
NSNumber *fileSize = fileAttributes[NSFileSize];
}
;
关键是在使用文件之前在 NSURL *url
上使用 startAccessingSecurityScopedResource
方法,在完成使用后使用 stopAccessingSecurityScopedResource
方法。
英文:
The solution turned out to be quite simple:
NSURL *url = = [NSURL fileURLWithPath:@"/private/var/mobile/Containers/Shared/AppGroup/61894F8B-0001-40C2-856E-F30D4663F7A7/File Provider Storage/Downloads/Instrukcja_Welcome_to.pdf"];
BOOL isAccessGranted = ;
if (isAccessGranted) {
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:url.relativePath error:&error];
NSNumber *fileSize = fileAttributes[NSFileSize];
}
;
The key thing is to use startAccessingSecurityScopedResource
method on NSURL *url
before using file and after finishing to use stopAccessingSecurityScopedResource
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论