Issue with AVAsset access in iOS 18 beta

NSString *filePath = @"/var/mobile/Media/DCIM/100APPLE/IMG_0800.MP4";
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:fileURL options:nil];

Before iOS 18, you could access AVAsset using the method mentioned above, but starting from the iOS 18 beta version, the following error appears

Error Domain=NSCocoaErrorDomain Code=257 "未能打开文件“IMG_0800.MP4”,因为你没有查看它的权限。" UserInfo={NSURL=file:///var/mobile/Media/DCIM/100APPLE/IMG_0800.MP4, AVErrorFailedDependenciesKey=(
    "assetProperty_AssetType"
), NSUnderlyingError=0x30c497f60 {Error Domain=NSOSStatusErrorDomain Code=-12203 "(null)"}}
Answered by DTS Engineer in 794185022

iOS has a sandbox that prevents your app from accessing arbitrary files on the file system. Your example uses a hard-coded path and I wouldn’t expect that to work. If you want to access files outside of your app’s sandbox, you must go through the relevant system API to gain access to those files. Those APIs extend your sandbox to grant you the access you need. In the case of the user’s photo library, that API is PhotoKit.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Accepted Answer

iOS has a sandbox that prevents your app from accessing arbitrary files on the file system. Your example uses a hard-coded path and I wouldn’t expect that to work. If you want to access files outside of your app’s sandbox, you must go through the relevant system API to gain access to those files. Those APIs extend your sandbox to grant you the access you need. In the case of the user’s photo library, that API is PhotoKit.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

I got it. Let me clarify the example. It's only a demo to show how to access the asset of the photo library through the file path. if I need to edit this asset, I can access it through the PhotoKit API and then call the method below to write it to my app's sandbox.

[[PHAssetResourceManager defaultManager] writeDataForAssetResource:targetResource
                                                                toFile:url
                                                               options:options
                                                     completionHandler:^(NSError * _Nullable error) {
       
    }];

OK. Does mean that things are now working? Or is there still a problem here?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Everything is good. Thank you for the reply~

Hi @yugu May i know how did you resolve this error. Because i am still facing the same issue.

[[PHImageManager defaultManager]
requestAVAssetForVideo:asset
       options:videoOptions
 resultHandler:^(AVAsset *_Nullable avAsset,
                 AVAudioMix *_Nullable audioMix,
                 NSDictionary *_Nullable info) {
   if ([avAsset isKindOfClass:[AVURLAsset class]]) {
     AVURLAsset *urlAsset = (AVURLAsset *)avAsset;

     NSURL *videoURL = urlAsset.URL;

     mediaInfo[@"path"] = videoURL.absoluteString;

   } else {
     // Failed to get video asset

     completion(nil);
   }
 }];

Also @DTS Engineer please let me know if something needs to be added in my above code to resolve this error, as i can see i am using the desired API to access the video url

I am getting this error @DTS Engineer @yugu

'You don’t have permission. - The AVPlayerItem instance has failed with the error code 257 and domain "NSCocoaErrorDomain".'

Issue with AVAsset access in iOS 18 beta
 
 
Q