Objective-C NSFileManager works fine on simulator but Operation not permitted error Domain=NSCocoaErrorDomain Code=257 on device when check file size

huangapple go评论99阅读模式
英文:

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:&amp;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:&amp;error byAccessor:^(NSURL *newURL) {
      fileAccessGranted = YES;
  }];

and added

&lt;key&gt;NSFileUsageDescription&lt;/key&gt;
&lt;string&gt;This app requires access to files.&lt;/string&gt;

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:@&quot;/private/var/mobile/Containers/Shared/AppGroup/61894F8B-0001-40C2-856E-F30D4663F7A7/File Provider Storage/Downloads/Instrukcja_Welcome_to.pdf&quot;];
BOOL isAccessGranted = 
; if (isAccessGranted) { NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:url.relativePath error:&amp;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

huangapple
  • 本文由 发表于 2023年6月15日 15:26:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480080.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定