Permissions under IOS for deletefile

rick ruhl 61 Reputation points
2021-11-23T19:52:05.52+00:00

Have the following code...

  try
            {
                if (File.Exists(videobase64))
                {
                    File.SetAttributes(videobase64, FileAttributes.Normal);
                    System.IO.File.Delete(videobase64);
                }
                else
                {
                    Debug.WriteLine("File does not exist.");
                }
            }
            catch (Exception f)
            {
                Console.WriteLine(f);
            }

And get the following error message

ystem.UnauthorizedAccessException: Access to the path '/var/mobile/Media/DCIM/113APPLE/IMG_3138.MOV' is denied. ---> System.IO.IOException: Operation not permitted
--- End of inner exception stack trace ---
at System.IO.FileSystem.DeleteFile (System.String fullPath) [0x0006e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/System.IO.FileSystem/src/System/IO/FileSystem.Unix.cs:218
at System.IO.File.Delete (System.String path) [0x0000e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/System.IO.FileSystem/src/System/IO/File.cs:107
at dealerpreviews.cuttingroom.OnSend (System.Object sender, System.EventArgs e) [0x000cc] in F:\mdev\dealerpreviews\dealerpreviews\dealerpreviews\cuttingroom.xaml.cs:162 }

I can only find on the web something about permissions for android. Nothing about this for IOS.

This file is created by

cameraView.Shutter();

the file does exist.. and it just wont delete due to some permission needed.

Rick

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,261 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 24,531 Reputation points Microsoft Vendor
    2021-11-24T08:10:45.433+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    Access to the path '/var/mobile/Media/DCIM/113APPLE/IMG_3138.MOV' is denied

    /var/mobile/Media/DCIM/113APPLE/IMG_3138.MOV this path means the video is saved in the local photo album. If you debug your app by real device, you could open the Photos in your device, and you can see the file in Recents album. It can't be deleted by System.IO .
    If you want to delete this file , you need to use PHPhotoLibrary API in iOS.

    First, you need add NSPhotoLibraryUsageDescription, NSCameraUsageDescription, NSMicrophoneUsageDescription to info.plist if you call this method cameraView.Shutter();

     <key>NSPhotoLibraryUsageDescription</key>     
    <string>xxxxxxxxxxx</string>     
    <key>NSCameraUsageDescription</key>     
    <string>xxxxxxxxxxx</string>     
    <key>NSMicrophoneUsageDescription</key>     
    <string>xxxxxxxxxxx</string>   
    

    Second, using the DependencyService to invoke native iOS functionality.
    Interface

    public interface IPHPhotoService  
        {  
            void DeleteItem();  
        }   
    

    iOS Implement

     [assembly: Dependency(typeof(PHPhotoService_iOS))]  
        namespace ToolkitDemo.iOS  
        {  
            public class PHPhotoService_iOS : IPHPhotoService  
            {  
                public void DeleteItem()  
                {  
                    var allPhotosOptions = new PHFetchOptions  
                    {  
                        SortDescriptors = new NSSortDescriptor[]  
                        {  
                            new NSSortDescriptor("creationDate",true)  
                        }  
                    };  
                    PHFetchResult collectonResuts = PHAsset.FetchAssets(PHAssetMediaType.Video,allPhotosOptions);  
                    PHAsset Asset = (PHAsset)collectonResuts.LastObject;  
                    Console.WriteLine("-----------");  
                    PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(  
                         () => {  
                             PHAssetChangeRequest.DeleteAssets(new PHAsset[] { Asset });  
                         },  
                         (bool success, NSError error) => {  
          
                             if (success)  
                             {  
                                 Console.WriteLine("deleted successfully");  
                             }  
                             else  
                             {  
                                 Console.WriteLine("deleted failed");  
                             }  
                }  
                     );  
                }  
          
        }  
        }  
    

    Then, you could delete the video/photo in Forms.

    private void CameraView_MediaCaptured(object sender, Xamarin.CommunityToolkit.UI.Views.MediaCapturedEventArgs e)  
            {  
                FileMediaSource file = e.Video.File;  
                string videomedia = file.ToString();  
                videobase64 = videomedia;  
                videobase64 = videobase64.Replace("File: ", "");  
                try  
                {  
                    if (File.Exists(videobase64))  
                    {  
                        File.SetAttributes(videobase64, FileAttributes.Normal);  
                        //  System.IO.File.Delete(videobase64);  
                        DependencyService.Get<IPHPhotoService>().DeleteItem();  
                    }  
                    else  
                    {  
                        Debug.WriteLine("File does not exist.");  
                    }  
                }  
                catch (Exception f)  
                {  
                    Console.WriteLine(f);  
                }  
            }  
    

    or click the button to delete

    private  void Button_Clicked_Delete(object sender, EventArgs e)  
            {  
                DependencyService.Get<IPHPhotoService>().DeleteItem();  
            }  
    

    When you delete a video, a system pop-up will be displayed asking whether to allow deletion, you can only delete it after you agree to the deletion.
    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.