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)"}}

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) {
       
    }];
Issue with AVAsset access in iOS 18 beta
 
 
Q